Browse Source

add deployment target id to porter app events api call on cli (#3536)

Co-authored-by: David Townley <davidtownley@Davids-MacBook-Air.local>
d-g-town 2 years ago
parent
commit
1e958b34a1
2 changed files with 17 additions and 14 deletions
  1. 12 9
      cli/cmd/v2/app_events.go
  2. 5 5
      cli/cmd/v2/apply.go

+ 12 - 9
cli/cmd/v2/app_events.go

@@ -13,7 +13,7 @@ import (
 	"github.com/porter-dev/porter/internal/telemetry"
 )
 
-func createBuildEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint) (string, error) {
+func createBuildEvent(ctx context.Context, client api.Client, applicationName string, projectId uint, clusterId uint, deploymentTargetID string) (string, error) {
 	ctx, span := telemetry.NewSpan(ctx, "create-build-event")
 	defer span.End()
 
@@ -22,6 +22,7 @@ func createBuildEvent(ctx context.Context, client api.Client, applicationName st
 		Type:               types.PorterAppEventType_Build,
 		TypeExternalSource: "GITHUB",
 		Metadata:           make(map[string]interface{}),
+		DeploymentTargetID: deploymentTargetID,
 	}
 
 	actionRunID := os.Getenv("GITHUB_RUN_ID")
@@ -61,14 +62,15 @@ func createBuildEvent(ctx context.Context, client api.Client, applicationName st
 	return event.ID, nil
 }
 
-func createPredeployEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, createdAt time.Time) (string, error) {
+func createPredeployEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, deploymentTargetID string, createdAt time.Time) (string, error) {
 	ctx, span := telemetry.NewSpan(ctx, "create-predeploy-event")
 	defer span.End()
 
 	req := &types.CreateOrUpdatePorterAppEventRequest{
-		Status:   types.PorterAppEventStatus_Progressing,
-		Type:     types.PorterAppEventType_PreDeploy,
-		Metadata: make(map[string]interface{}),
+		Status:             types.PorterAppEventStatus_Progressing,
+		Type:               types.PorterAppEventType_PreDeploy,
+		Metadata:           make(map[string]interface{}),
+		DeploymentTargetID: deploymentTargetID,
 	}
 	req.Metadata["start_time"] = createdAt
 
@@ -80,14 +82,15 @@ func createPredeployEvent(ctx context.Context, client api.Client, applicationNam
 	return event.ID, nil
 }
 
-func updateExistingEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, eventID string, status types.PorterAppEventStatus, metadata map[string]interface{}) error {
+func updateExistingEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, deploymentTargetID string, eventID string, status types.PorterAppEventStatus, metadata map[string]interface{}) error {
 	ctx, span := telemetry.NewSpan(ctx, "update-existing-event")
 	defer span.End()
 
 	req := &types.CreateOrUpdatePorterAppEventRequest{
-		ID:       eventID,
-		Status:   status,
-		Metadata: metadata,
+		ID:                 eventID,
+		Status:             status,
+		Metadata:           metadata,
+		DeploymentTargetID: deploymentTargetID,
 	}
 
 	_, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)

+ 5 - 5
cli/cmd/v2/apply.go

@@ -107,7 +107,7 @@ func Apply(ctx context.Context, cliConf config.CLIConfig, client api.Client, por
 	if applyResp.CLIAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_BUILD {
 		color.New(color.FgGreen).Printf("Building new image...\n") // nolint:errcheck,gosec
 
-		eventID, _ := createBuildEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster)
+		eventID, _ := createBuildEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID)
 
 		if commitSHA == "" {
 			return errors.New("Build is required but commit SHA cannot be identified. Please set the PORTER_COMMIT_SHA environment variable or run apply in git repository with access to the git CLI.")
@@ -142,13 +142,13 @@ func Apply(ctx context.Context, cliConf config.CLIConfig, client api.Client, por
 
 		err = build(ctx, client, buildSettings)
 		if err != nil {
-			_ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, eventID, types.PorterAppEventStatus_Failed, nil)
+			_ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, types.PorterAppEventStatus_Failed, nil)
 			return fmt.Errorf("error building app: %w", err)
 		}
 
 		color.New(color.FgGreen).Printf("Successfully built image (tag: %s)\n", buildSettings.ImageTag) // nolint:errcheck,gosec
 
-		_ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, eventID, types.PorterAppEventStatus_Success, nil)
+		_ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, types.PorterAppEventStatus_Success, nil)
 
 		applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId)
 		if err != nil {
@@ -160,7 +160,7 @@ func Apply(ctx context.Context, cliConf config.CLIConfig, client api.Client, por
 		color.New(color.FgGreen).Printf("Waiting for predeploy to complete...\n") // nolint:errcheck,gosec
 
 		now := time.Now().UTC()
-		eventID, _ := createPredeployEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, now)
+		eventID, _ := createPredeployEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, now)
 
 		eventStatus := types.PorterAppEventStatus_Success
 		for {
@@ -186,7 +186,7 @@ func Apply(ctx context.Context, cliConf config.CLIConfig, client api.Client, por
 
 		metadata := make(map[string]interface{})
 		metadata["end_time"] = time.Now().UTC()
-		_ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, eventID, eventStatus, metadata)
+		_ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, eventStatus, metadata)
 
 		applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId)
 		if err != nil {