Last active
April 22, 2021 18:24
-
-
Save jsimonweb/d66fbcd33e6ec9d04b2479f4d07d0bc1 to your computer and use it in GitHub Desktop.
Google Cloud Function triggered by Pub/Sub message to start or stop a Cloud SQL instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Package p contains a Pub/Sub Cloud Function. | |
package p | |
import ( | |
"context" | |
"encoding/json" | |
"log" | |
"golang.org/x/oauth2/google" | |
sqladmin "google.golang.org/api/sqladmin/v1beta4" | |
) | |
// PubSubMessage is the payload of a Pub/Sub event. | |
// See the documentation for more details: | |
// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage | |
type PubSubMessage struct { | |
Data []byte `json:"data"` | |
} | |
type MessagePayload struct { | |
Instance string | |
Project string | |
Action string | |
} | |
// ProcessPubSub consumes and processes a Pub/Sub message. | |
func ProcessPubSub(ctx context.Context, m PubSubMessage) error { | |
var psData MessagePayload | |
err := json.Unmarshal(m.Data, &psData) | |
if err != nil { | |
log.Println(err) | |
} | |
log.Printf("Request received for Cloud SQL instance %s action: %s, %s", psData.Action, psData.Instance, psData.Project) | |
// Create an http.Client that uses Application Default Credentials. | |
hc, err := google.DefaultClient(ctx, sqladmin.CloudPlatformScope) | |
if err != nil { | |
return err | |
} | |
// Create the Google Cloud SQL service. | |
service, err := sqladmin.New(hc) | |
if err != nil { | |
return err | |
} | |
// Get the requested start or stop Action. | |
action := "UNDEFINED" | |
switch psData.Action { | |
case "start": | |
action = "ALWAYS" | |
case "stop": | |
action = "NEVER" | |
default: | |
log.Fatal("No valid action provided.") | |
} | |
// See more examples at: | |
// https://cloud.google.com/sql/docs/sqlserver/admin-api/rest/v1beta4/instances/patch | |
rb := &sqladmin.DatabaseInstance{ | |
Settings: &sqladmin.Settings{ | |
ActivationPolicy: action, | |
}, | |
} | |
resp, err := service.Instances.Patch(psData.Project, psData.Instance, rb).Context(ctx).Do() | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("%#v\n", resp) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment