Skip to content

Commit dcbd835

Browse files
authored
Merge pull request #24655 from hashicorp/b-remote-policy-check-race
backend/remote: display cost estimate and policy check whenever available
2 parents 24ced6b + 105fcb3 commit dcbd835

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

backend/remote/backend_common.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ func (b *Remote) costEstimate(stopCtx, cancelCtx context.Context, op *backend.Op
269269
return generalError("Failed to retrieve cost estimate", err)
270270
}
271271

272+
// If the run is canceled or errored, but the cost-estimate still has
273+
// no result, there is nothing further to render.
274+
if ce.Status != tfe.CostEstimateFinished {
275+
if r.Status == tfe.RunCanceled || r.Status == tfe.RunErrored {
276+
return nil
277+
}
278+
}
279+
272280
switch ce.Status {
273281
case tfe.CostEstimateFinished:
274282
delta, err := strconv.ParseFloat(ce.DeltaMonthlyCost, 64)
@@ -324,6 +332,8 @@ func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Ope
324332
b.CLI.Output("\n------------------------------------------------------------------------\n")
325333
}
326334
for i, pc := range r.PolicyChecks {
335+
// Read the policy check logs. This is a blocking call that will only
336+
// return once the policy check is complete.
327337
logs, err := b.client.PolicyChecks.Logs(stopCtx, pc.ID)
328338
if err != nil {
329339
return generalError("Failed to retrieve policy check logs", err)
@@ -336,6 +346,15 @@ func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Ope
336346
return generalError("Failed to retrieve policy check", err)
337347
}
338348

349+
// If the run is canceled or errored, but the policy check still has
350+
// no result, there is nothing further to render.
351+
if r.Status == tfe.RunCanceled || r.Status == tfe.RunErrored {
352+
switch pc.Status {
353+
case tfe.PolicyPending, tfe.PolicyQueued, tfe.PolicyUnreachable:
354+
continue
355+
}
356+
}
357+
339358
var msgPrefix string
340359
switch pc.Scope {
341360
case tfe.PolicyScopeOrganization:

backend/remote/backend_plan.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,11 @@ in order to capture the filesystem context the remote workspace expects:
308308
return r, generalError("Failed to retrieve run", err)
309309
}
310310

311-
// Return if the run is canceled or errored. We return without
312-
// an error, even if the run errored, as the error is already
313-
// displayed by the output of the remote run.
314-
if r.Status == tfe.RunCanceled || r.Status == tfe.RunErrored {
315-
return r, nil
316-
}
311+
// If the run is canceled or errored, we still continue to the
312+
// cost-estimation and policy check phases to ensure we render any
313+
// results available. In the case of a hard-failed policy check, the
314+
// status of the run will be "errored", but there is still policy
315+
// information which should be shown.
317316

318317
// Show any cost estimation output.
319318
if r.CostEstimate != nil {

0 commit comments

Comments
 (0)