in reference-architectures/cloud_deploy_flow/CloudFunctions/cloudDeployOperations/main.go [71:113]
func cloudDeployOperations(ctx context.Context, e event.Event) error {
log.Printf("Deploy Operations function invoked")
// Parse event data into Message struct
var msg Message
err := json.Unmarshal(e.Data(), &msg)
if err != nil {
// Acknowledge the message by returning nil, even if it’s bad, to prevent reprocessing.
_ = fmt.Errorf("errored unmarshalling data: %v", err)
return nil
}
// Extract attributes from the message for validation and processing.
var a = msg.Message.Attributes
// Check if the message indicates a successful release event for further processing.
if a.ResourceType == "Release" && a.Action == "Succeed" {
log.Printf("Creating Rollout and sending to pubsub")
// Define a rollout command message with details from OperationsData.
var command = CommandMessage{
Commmand: "CreateRollout",
CreateRollout: deploypb.CreateRolloutRequest{
Parent: a.Resource, // The deployment resource to associate with this rollout
RolloutId: a.ReleaseId, // The ID of the release
Rollout: &deploypb.Rollout{
// TODO: TargetId should ideally come from the Pub/Sub message rather than hardcoded.
TargetId: "random-date-service",
},
},
}
// Send the command to Pub/Sub and log any errors that occur.
err = sendCommandPubSub(ctx, &command)
if err != nil {
_ = fmt.Errorf("failed to send pubsub command: %v", err)
// Acknowledge the message even if there's a failure to prevent repeated processing.
return nil
}
log.Printf("Deployment triggered successfully")
}
return nil
}