mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-25 01:00:16 -05:00
Refactor task stage retrieval in cloud backend
Previously all of the logic to retrieve Task Stages was in the backend_plan.go file. This commit: * Moves the logic to the backend_taskStages.go file. * Replaces using an array to a map indexed by the Task Stage's stage. This makes it easier to find the relevant stage in the list and means the getTaskStageIDByName function is no longer required.
This commit is contained in:
@@ -293,22 +293,13 @@ in order to capture the filesystem context the remote workspace expects:
|
||||
|
||||
// Retrieve the run to get task stages.
|
||||
// Task Stages are calculated upfront so we only need to call this once for the run.
|
||||
taskStages := make([]*tfe.TaskStage, 0)
|
||||
result, err := b.client.Runs.ReadWithOptions(stopCtx, r.ID, &tfe.RunReadOptions{
|
||||
Include: []tfe.RunIncludeOpt{tfe.RunTaskStages},
|
||||
})
|
||||
if err == nil {
|
||||
taskStages = result.TaskStages
|
||||
} else {
|
||||
// This error would be expected for older versions of TFE that do not allow
|
||||
// fetching task_stages.
|
||||
if !strings.HasSuffix(err.Error(), "Invalid include parameter") {
|
||||
return r, generalError("Failed to retrieve run", err)
|
||||
}
|
||||
taskStages, err := b.runTaskStages(stopCtx, b.client, r.ID)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
|
||||
if stageID := getTaskStageIDByName(taskStages, tfe.PrePlan); stageID != nil {
|
||||
if err := b.waitTaskStage(stopCtx, cancelCtx, op, r, *stageID, "Pre-plan Tasks"); err != nil {
|
||||
if stage, ok := taskStages[tfe.PrePlan]; ok {
|
||||
if err := b.waitTaskStage(stopCtx, cancelCtx, op, r, stage.ID, "Pre-plan Tasks"); err != nil {
|
||||
return r, err
|
||||
}
|
||||
}
|
||||
@@ -357,8 +348,8 @@ in order to capture the filesystem context the remote workspace expects:
|
||||
// status of the run will be "errored", but there is still policy
|
||||
// information which should be shown.
|
||||
|
||||
if stageID := getTaskStageIDByName(taskStages, tfe.PostPlan); stageID != nil {
|
||||
if err := b.waitTaskStage(stopCtx, cancelCtx, op, r, *stageID, "Post-plan Tasks"); err != nil {
|
||||
if stage, ok := taskStages[tfe.PostPlan]; ok {
|
||||
if err := b.waitTaskStage(stopCtx, cancelCtx, op, r, stage.ID, "Post-plan Tasks"); err != nil {
|
||||
return r, err
|
||||
}
|
||||
}
|
||||
@@ -382,19 +373,6 @@ in order to capture the filesystem context the remote workspace expects:
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func getTaskStageIDByName(stages []*tfe.TaskStage, stageName tfe.Stage) *string {
|
||||
if len(stages) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, stage := range stages {
|
||||
if stage.Stage == stageName {
|
||||
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]
|
||||
|
||||
32
internal/cloud/backend_taskStages.go
Normal file
32
internal/cloud/backend_taskStages.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package cloud
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
tfe "github.com/hashicorp/go-tfe"
|
||||
)
|
||||
|
||||
type taskStages map[tfe.Stage]*tfe.TaskStage
|
||||
|
||||
func (b *Cloud) runTaskStages(ctx context.Context, client *tfe.Client, runId string) (taskStages, error) {
|
||||
taskStages := make(taskStages, 0)
|
||||
result, err := client.Runs.ReadWithOptions(ctx, runId, &tfe.RunReadOptions{
|
||||
Include: []tfe.RunIncludeOpt{tfe.RunTaskStages},
|
||||
})
|
||||
if err == nil {
|
||||
for _, t := range result.TaskStages {
|
||||
if t != nil {
|
||||
taskStages[t.Stage] = t
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// This error would be expected for older versions of TFE that do not allow
|
||||
// fetching task_stages.
|
||||
if !strings.HasSuffix(err.Error(), "Invalid include parameter") {
|
||||
return taskStages, generalError("Failed to retrieve run", err)
|
||||
}
|
||||
}
|
||||
|
||||
return taskStages, nil
|
||||
}
|
||||
Reference in New Issue
Block a user