Cloud Scheduler to stop a Cloud SQL database

preview_player
Показать описание
In this video Pablo Inigo will show how to stop and start a Cloud SQL database in a daily basis with a Cloud Scheduler calling a Pub/Sub Topic, calling a Step Function. MAGIC!!!!

#GCP #googlecloud #cloudscheduler #cloudfunctions #cloudsq #pub/sub #pubsub #databases

package p

import (
"context"
"encoding/json"
"log"

)

type PubSubMessage struct {
Data []byte `json:"data"`
}

type MessagePayload struct {
Instance string
Project string
Action string
}

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)

hc, err := google.DefaultClient(ctx, sqladmin.CloudPlatformScope)
if err != nil {
return err
}

service, err := sqladmin.New(hc)
if err != nil {
return err
}

action := "UNDEFINED"
switch psData.Action {
case "start":
action = "ALWAYS"
case "stop":
action = "NEVER"
default:
log.Fatal("No valid action provided.")
}

// See more examples at:
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
}
Рекомендации по теме
Комментарии
Автор

Hi, bro. Very valueble content. By the way, now 2023, I am stuck at Deploy process. It failed there. What can i do?

CforChillandCheese
visit shbcf.ru