func cloudDeployApprovals()

in reference-architectures/cloud_deploy_flow/CloudFunctions/cloudDeployApprovals/main.go [82:124]


func cloudDeployApprovals(ctx context.Context, e event.Event) error {
	log.Printf("Deploy Approvals function invoked") // Logs function invocation

	var msg Message                       // Struct to hold the decoded event data
	err := json.Unmarshal(e.Data(), &msg) // Decodes the JSON event data into msg
	if err != nil {
		// If data is malformed, log the error and continue to avoid reprocessing
		_ = fmt.Errorf("errored unmarshalling data: %v", err)
		return nil
	}

	// Wait for 3 seconds as a demo delay before proceeding
	log.Printf("Waiting 3 seconds to approve for demo")
	time.Sleep(3 * time.Second)

	// Extract the message attributes for easier access
	var a = msg.Message.Attributes
	log.Printf("A is: %v", a) // Logs the extracted attributes

	// Check conditions: Action required, a valid Rollout ID, and manual approval
	if a.Action == "Required" && a.Rollout != "" && strings.ToLower(a.ManualApproval) == "true" {
		log.Printf("Creating Rollout and sending to pubsub") // Logs the action

		// Constructs a command message to approve the rollout
		var command = CommandMessage{
			Commmand: "ApproveRollout",
			ApproveRollout: deploypb.ApproveRolloutRequest{
				Name:     a.Rollout, // Rollout name as per received message
				Approved: true,      // Approves the rollout
			},
		}

		// Sends the command to Pub/Sub
		err = sendCommandPubSub(ctx, &command)
		if err != nil {
			_ = fmt.Errorf("failed to send pubsub command: %v", err) // Logs error if Pub/Sub message fails to send
			return nil                                               // Returns nil to acknowledge message even if there's an error
		}
		log.Printf("Deployment triggered successfully") // Logs success
	}

	return nil // Acknowledges successful message processing
}