From e479bd5dc30ca109af490777f9643b68e61653b4 Mon Sep 17 00:00:00 2001 From: Paul Thrasher Date: Mon, 15 Apr 2019 17:46:49 -0700 Subject: [PATCH] update to latest go-tfe same version number but pointing to a new sha Signed-off-by: Paul Thrasher --- backend/remote/backend_common.go | 54 ++++++++++++++++++++++++++++++++ backend/remote/backend_plan.go | 8 +++++ 2 files changed, 62 insertions(+) diff --git a/backend/remote/backend_common.go b/backend/remote/backend_common.go index 4e64830f2..623403a22 100644 --- a/backend/remote/backend_common.go +++ b/backend/remote/backend_common.go @@ -227,6 +227,60 @@ func (b *Remote) parseVariableValues(op *backend.Operation) (terraform.InputValu return result, diags } +func (b *Remote) costEstimation(stopCtx, cancelCtx context.Context, op *backend.Operation, r *tfe.Run) error { + if b.CLI != nil { + b.CLI.Output("\n------------------------------------------------------------------------\n") + } + var ce = r.CostEstimation + + logs, err := b.client.CostEstimation.Logs(stopCtx, ce.ID) + if err != nil { + return generalError("Failed to retrieve cost estimation logs", err) + } + scanner := bufio.NewScanner(logs) + + // Retrieve the cost estimation to get its current status. + ce, err := b.client.CostEstimation.Read(stopCtx, ce.ID) + if err != nil { + return generalError("Failed to retrieve cost estimation", err) + } + + var msgPrefix = "Cost estimation" + + if b.CLI != nil { + b.CLI.Output(b.Colorize().Color(msgPrefix + ":\n")) + } + + for scanner.Scan() { + if b.CLI != nil { + b.CLI.Output(b.Colorize().Color(scanner.Text())) + } + } + if err := scanner.Err(); err != nil { + return generalError("Failed to read logs", err) + } + + switch ce.Status { + case tfe.CostEstimationFinished: + if r.HasChanges && op.Type == backend.OperationTypeApply || b.CLI != nil { + b.CLI.Output("\n------------------------------------------------------------------------") + } + return nil + case tfe.CostEstimationErrored: + return fmt.Errorf(msgPrefix + " errored.") + case tfe.CostEstimationCanceled: + return fmt.Errorf(msgPrefix + " canceled.") + default: + return fmt.Errorf("Unknown or unexpected cost estimation state: %s", ce.Status) + } + + if b.CLI != nil { + b.CLI.Output("------------------------------------------------------------------------") + } + + return nil +} + func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Operation, r *tfe.Run) error { if b.CLI != nil { b.CLI.Output("\n------------------------------------------------------------------------\n") diff --git a/backend/remote/backend_plan.go b/backend/remote/backend_plan.go index 200f7fd8d..07e7422d0 100644 --- a/backend/remote/backend_plan.go +++ b/backend/remote/backend_plan.go @@ -290,6 +290,14 @@ func (b *Remote) plan(stopCtx, cancelCtx context.Context, op *backend.Operation, return r, nil } + // Show Cost Estimation + if r.CostEstimations != nil { + err = b.costEstimation(stopCtx, cancelCtx, op, r) + if err != nil { + generalError("Cost Estimation error", err) + } + } + // Check any configured sentinel policies. if len(r.PolicyChecks) > 0 { err = b.checkPolicy(stopCtx, cancelCtx, op, r)