Skip to content

Commit 003b5d8

Browse files
committed
Add Pre-Plan Run Tasks to Terraform CLI
Prevously the cloud backend would only render post-plan run tasks. Now that pre-plan tasks are in beta, this commit updates the plan phase to render pre-plan run tasks. This commit also moves some common code to the common backend as it will be used by other task stages in the future.
1 parent e52adf6 commit 003b5d8

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ENHANCEMENTS:
3434
* The AzureRM Backend now only supports MSAL (and Microsoft Graph) and no longer makes use of ADAL (and Azure Active Directory Graph) for authentication ([#31070](https://github.com/hashicorp/terraform/issues/31070))
3535
* The COS backend now supports global acceleration. ([#31425](https://github.com/hashicorp/terraform/issues/31425))
3636
* providercache: include host in provider installation error ([#31524](https://github.com/hashicorp/terraform/issues/31524))
37+
* When showing the progress of a remote operation running in Terraform Cloud, Terraform CLI will include information about pre-plan run tasks ([#31617](https://github.com/hashicorp/terraform/issues/31617))
3738
3839
BUG FIXES:
3940
@@ -52,7 +53,7 @@ EXPERIMENTS:
5253
* The built-in `defaults` function, previously used to meet the use-case of replacing null values with default values, will not graduate to stable and has been removed. Use the second argument of `optional` inline in your type constraint to declare default values instead.
5354
5455
If you have any experimental modules that were participating in this experiment, you will need to remove the experiment opt-in and adopt the new syntax for declaring default values in order to migrate your existing module to the stablized version of this feature. If you are writing a shared module for others to use, we recommend declaring that your module requires Terraform v1.3.0 or later to give specific feedback when using the new feature on older Terraform versions, in place of the previous declaration to use the experimental form of this feature:
55-
56+
5657
```hcl
5758
terraform {
5859
required_version = ">= 1.3.0"

internal/cloud/backend_common.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ func (b *Cloud) waitForRun(stopCtx, cancelCtx context.Context, op *backend.Opera
199199
}
200200
}
201201

202+
func (b *Cloud) waitTaskStage(stopCtx, cancelCtx context.Context, op *backend.Operation, r *tfe.Run, stageID string, outputTitle string) error {
203+
integration := &IntegrationContext{
204+
B: b,
205+
StopContext: stopCtx,
206+
CancelContext: cancelCtx,
207+
Op: op,
208+
Run: r,
209+
}
210+
return b.runTasks(integration, integration.BeginOutput(outputTitle), stageID)
211+
}
212+
202213
func (b *Cloud) costEstimate(stopCtx, cancelCtx context.Context, op *backend.Operation, r *tfe.Run) error {
203214
if r.CostEstimate == nil {
204215
return nil

internal/cloud/backend_plan.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,27 @@ in order to capture the filesystem context the remote workspace expects:
291291
runHeader, b.hostname, b.organization, op.Workspace, r.ID)) + "\n"))
292292
}
293293

294+
// Retrieve the run to get task stages.
295+
// Task Stages are calculated upfront so we only need to call this once for the run.
296+
taskStages := make([]*tfe.TaskStage, 0)
297+
if result, err := b.client.Runs.ReadWithOptions(stopCtx, r.ID, &tfe.RunReadOptions{
298+
Include: []tfe.RunIncludeOpt{tfe.RunTaskStages},
299+
}); err == nil {
300+
taskStages = result.TaskStages
301+
} else {
302+
// This error would be expected for older versions of TFE that do not allow
303+
// fetching task_stages.
304+
if !strings.HasSuffix(err.Error(), "Invalid include parameter") {
305+
return r, generalError("Failed to retrieve run", err)
306+
}
307+
}
308+
309+
if stageID := getTaskStageIDByName(taskStages, tfe.PrePlan); stageID != nil {
310+
if err := b.waitTaskStage(stopCtx, cancelCtx, op, r, *stageID, "Pre-plan Tasks"); err != nil {
311+
return r, err
312+
}
313+
}
314+
294315
r, err = b.waitForRun(stopCtx, cancelCtx, op, "plan", r, w)
295316
if err != nil {
296317
return r, err
@@ -324,20 +345,9 @@ in order to capture the filesystem context the remote workspace expects:
324345
}
325346

326347
// Retrieve the run to get its current status.
327-
runID := r.ID
328-
r, err = b.client.Runs.ReadWithOptions(stopCtx, runID, &tfe.RunReadOptions{
329-
Include: []tfe.RunIncludeOpt{tfe.RunTaskStages},
330-
})
348+
r, err = b.client.Runs.Read(stopCtx, r.ID)
331349
if err != nil {
332-
// This error would be expected for older versions of TFE that do not allow
333-
// fetching task_stages.
334-
if strings.HasSuffix(err.Error(), "Invalid include parameter") {
335-
r, err = b.client.Runs.Read(stopCtx, runID)
336-
}
337-
338-
if err != nil {
339-
return r, generalError("Failed to retrieve run", err)
340-
}
350+
return r, generalError("Failed to retrieve run", err)
341351
}
342352

343353
// If the run is canceled or errored, we still continue to the
@@ -346,18 +356,8 @@ in order to capture the filesystem context the remote workspace expects:
346356
// status of the run will be "errored", but there is still policy
347357
// information which should be shown.
348358

349-
// Await post-plan run tasks
350-
integration := &IntegrationContext{
351-
B: b,
352-
StopContext: stopCtx,
353-
CancelContext: cancelCtx,
354-
Op: op,
355-
Run: r,
356-
}
357-
358-
if stageID := getTaskStageIDByName(r.TaskStages, tfe.PostPlan); stageID != nil {
359-
err = b.runTasks(integration, integration.BeginOutput("Run Tasks (post-plan)"), *stageID)
360-
if err != nil {
359+
if stageID := getTaskStageIDByName(taskStages, tfe.PostPlan); stageID != nil {
360+
if err := b.waitTaskStage(stopCtx, cancelCtx, op, r, *stageID, "Post-plan Tasks"); err != nil {
361361
return r, err
362362
}
363363
}

internal/cloud/backend_runTasks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (b *Cloud) runTasksWithTaskResults(context *IntegrationContext, output Inte
5252
stage, err := fetchTaskStage(b, context.StopContext)
5353

5454
if err != nil {
55-
return false, generalError("Failed to retrieve pre-apply task stage", err)
55+
return false, generalError("Failed to retrieve task stage", err)
5656
}
5757

5858
summary := summarizeTaskResults(stage.TaskResults)

0 commit comments

Comments
 (0)