in reference-architectures/cloud_deploy_flow/CloudFunctions/cloudDeployInteractions/main.go [43:88]
func cloudDeployInteractions(ctx context.Context, e event.Event) error {
log.Printf("Deploy trigger function invoked")
// Parse the Pub/Sub message into MessagePublishedData structure
var msg MessagePublishedData
if err := e.DataAs(&msg); err != nil {
return fmt.Errorf("event.DataAs: %w", err)
}
// Unmarshal the Pub/Sub message data into the DeployCommand structure
log.Printf("Converting Byte to Struct Object")
var c DeployCommand
if err := json.Unmarshal(msg.Message.Data, &c); err != nil {
log.Printf("Failed to unmarshal to command, assuming bad command")
return nil // Returning nil acknowledges the message, preventing reprocessing
}
// Create a new Cloud Deploy client to interact with Cloud Deploy services
deployClient, err := deploy.NewCloudDeployClient(ctx,
option.WithUserAgent("cloud-solutions/platform-engineering-cloud-deploy-pipeline-code-v1"),
)
if err != nil {
return fmt.Errorf("error creating Cloud Deploy client: %v", err)
}
defer deployClient.Close() // Ensure client is closed after function completes
// Process the command based on its type (CreateRelease, CreateRollout, or ApproveRollout)
switch c.Commmand {
case "CreateRelease":
if err := cdCreateRelease(ctx, *deployClient, &c.CreateRelease); err != nil {
_ = fmt.Errorf("create release failed: %v", err)
return nil
}
case "CreateRollout":
if err := cdCreateRollout(ctx, *deployClient, &c.CreateRollout); err != nil {
_ = fmt.Errorf("create rollout failed: %v", err)
return nil
}
case "ApproveRollout":
if err := cdApproveRollout(ctx, *deployClient, &c.ApproveRollout); err != nil {
_ = fmt.Errorf("approve rollout failed: %v", err)
return nil
}
}
return nil
}