diff --git a/apps/checker/checker/update.go b/apps/checker/checker/update.go index 8f530e80..6a0f3e62 100644 --- a/apps/checker/checker/update.go +++ b/apps/checker/checker/update.go @@ -4,12 +4,14 @@ import ( "bytes" "context" "encoding/json" - "net/http" - "os" - "time" - + "fmt" "github.com/rs/zerolog/log" + "google.golang.org/api/option" + "os" + "cloud.google.com/go/auth" + cloudtasks "cloud.google.com/go/cloudtasks/apiv2" + taskspb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb" ) type UpdateData struct { @@ -22,24 +24,67 @@ type UpdateData struct { Latency int64 `json:"latency,omitempty"` } -func UpdateStatus(ctx context.Context, updateData UpdateData) { +func UpdateStatus(ctx context.Context, updateData UpdateData) error { + url := "https://openstatus-workflows.fly.dev/updateStatus" basic := "Basic " + os.Getenv("CRON_SECRET") payloadBuf := new(bytes.Buffer) + opts := &auth.Options2LO{ + Email: os.Getenv("GCP_CLIENT_EMAIL"), + PrivateKey: []byte(os.Getenv("GCP_PRIVATE_KEY")), + PrivateKeyID: os.Getenv("GCP_PRIVATE_KEY_ID"), + Scopes: []string{ + "https://www.googleapis.com/auth/cloud-platform", + }, + TokenURL: "https://oauth2.googleapis.com/token", + } + + tp, err := auth.New2LOTokenProvider(opts) + if err != nil { + log.Ctx(ctx).Error().Err(err).Msg("error while creating token provider") + return err + } + + creds := auth.NewCredentials(&auth.CredentialsOptions{ + TokenProvider: tp, + }) + + client, err := cloudtasks.NewClient(ctx, option.WithAuthCredentials(creds)) + if err != nil { + log.Ctx(ctx).Error().Err(err).Msg("error while creating cloud tasks client") + + } + defer client.Close() + if err := json.NewEncoder(payloadBuf).Encode(updateData); err != nil { log.Ctx(ctx).Error().Err(err).Msg("error while updating status") - return + return err + } + projectID := os.Getenv("GCP_PROJECT_ID") + queuePath := fmt.Sprintf("projects/%s/locations/europe-west1/queues/alerting", projectID) + req := &taskspb.CreateTaskRequest{ + Parent: queuePath, + Task: &taskspb.Task{ + // https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2#HttpRequest + MessageType: &taskspb.Task_HttpRequest{ + HttpRequest: &taskspb.HttpRequest{ + HttpMethod: taskspb.HttpMethod_POST, + Url: url, + Headers: map[string]string{"Authorization": basic, "Content-Type": "application/json"}, + }, + }, + }, } - req, _ := http.NewRequestWithContext(ctx, http.MethodPost, url, payloadBuf) - req.Header.Set("Authorization", basic) - req.Header.Set("Content-Type", "application/json") - client := &http.Client{Timeout: time.Second * 10} - if _, err := client.Do(req); err != nil { - log.Ctx(ctx).Error().Err(err).Msg("error while updating status") + // Add a payload message if one is present. + req.Task.GetHttpRequest().Body = payloadBuf.Bytes() + + _, err = client.CreateTask(ctx, req) + if err != nil { + log.Ctx(ctx).Error().Err(err).Msg("error while creating the cloud task") + return fmt.Errorf("cloudtasks.CreateTask: %w", err) } - defer req.Body.Close() - // Should we add a retry mechanism here? + return nil }