From 791c36c5045940cad626f66dd04a217d2af5ae16 Mon Sep 17 00:00:00 2001 From: Brandon Croft Date: Wed, 15 Dec 2021 14:43:02 -0700 Subject: [PATCH] [cloud] report run tasks by name instead of assuming pre_apply --- internal/cloud/backend_plan.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/internal/cloud/backend_plan.go b/internal/cloud/backend_plan.go index 0a7975d64..658b0191c 100644 --- a/internal/cloud/backend_plan.go +++ b/internal/cloud/backend_plan.go @@ -324,7 +324,9 @@ in order to capture the filesystem context the remote workspace expects: } // Retrieve the run to get its current status. - r, err = b.client.Runs.Read(stopCtx, r.ID) + r, err = b.client.Runs.ReadWithOptions(stopCtx, r.ID, &tfe.RunReadOptions{ + Include: "task_stages", + }) if err != nil { return r, generalError("Failed to retrieve run", err) } @@ -354,11 +356,12 @@ in order to capture the filesystem context the remote workspace expects: // Await pre-apply run tasks if len(r.TaskStage) > 0 { context := NewIntegrationContext(stopCtx, cancelCtx, b, op, r) - // TODO: this only works while there is exactly 1 task stage - err = b.runTasks(context, context.BeginOutput("Run Tasks (pre-apply)"), r.TaskStage[0].ID) - if err != nil { - return r, err + if stageID := getTaskStageIDByName(r.TaskStage, "pre_apply"); stageID != nil { + err = b.runTasks(context, context.BeginOutput("Run Tasks (pre-apply)"), *stageID) + if err != nil { + return r, err + } } } @@ -370,6 +373,15 @@ func String(v string) *string { return &v } +func getTaskStageIDByName(stages []*tfe.TaskStage, name string) *string { + for _, stage := range stages { + if stage.Stage == name { + return &stage.ID + } + } + return nil +} + const planDefaultHeader = ` [reset][yellow]Running plan in Terraform Cloud. Output will stream here. Pressing Ctrl-C will stop streaming the logs, but will not stop the plan running remotely.[reset]