From 71dac9bf5edeb56c5e446a45359afd7133aef269 Mon Sep 17 00:00:00 2001 From: Sebastian Rivera Date: Fri, 10 Feb 2023 16:58:14 -0500 Subject: [PATCH] Add token field to cloud backend --- internal/cloud/backend.go | 10 +++++++--- internal/cloud/backend_plan.go | 6 +----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/cloud/backend.go b/internal/cloud/backend.go index 5c2c045396..4625a4d431 100644 --- a/internal/cloud/backend.go +++ b/internal/cloud/backend.go @@ -66,6 +66,9 @@ type Cloud struct { // hostname of Terraform Cloud or Terraform Enterprise hostname string + // token for Terraform Cloud or Terraform Enterprise + token string + // organization is the organization that contains the target workspaces. organization string @@ -250,7 +253,7 @@ func (b *Cloud) Configure(obj cty.Value) tfdiags.Diagnostics { // Get the token from the CLI Config File in the credentials section // if no token was not set in the configuration if token == "" { - token, err = b.token() + token, err = b.cliConfigToken() if err != nil { diags = diags.Append(tfdiags.AttributeValue( tfdiags.Error, @@ -280,6 +283,7 @@ func (b *Cloud) Configure(obj cty.Value) tfdiags.Diagnostics { return diags } + b.token = token b.configureGenericHostname() if b.client == nil { @@ -458,10 +462,10 @@ func (b *Cloud) discover() (*url.URL, error) { return service, err } -// token returns the token for this host as configured in the credentials +// cliConfigToken returns the token for this host as configured in the credentials // section of the CLI Config File. If no token was configured, an empty // string will be returned instead. -func (b *Cloud) token() (string, error) { +func (b *Cloud) cliConfigToken() (string, error) { hostname, err := svchost.ForComparison(b.hostname) if err != nil { return "", err diff --git a/internal/cloud/backend_plan.go b/internal/cloud/backend_plan.go index e3a11bfd73..8854d104ec 100644 --- a/internal/cloud/backend_plan.go +++ b/internal/cloud/backend_plan.go @@ -432,12 +432,8 @@ func (b *Cloud) renderPlanLogs(ctx context.Context, op *backend.Operation, run * // enabled. The plan output will have already been rendered when the logs // were read if this wasn't the case. if run.Workspace.StructuredRunOutputEnabled && b.renderer != nil { - token, err := b.token() - if err != nil { - return err - } // Fetch the redacted plan. - redacted, err := readRedactedPlan(ctx, b.client.BaseURL(), token, run.Plan.ID) + redacted, err := readRedactedPlan(ctx, b.client.BaseURL(), b.token, run.Plan.ID) if err != nil { return err }