From b23cfaefe825a82baf7be05a210385483efcc58e Mon Sep 17 00:00:00 2001 From: Sebastian Rivera Date: Tue, 11 Apr 2023 16:47:56 -0400 Subject: [PATCH] Refactor SRO check to prevent duplicate plan output --- go.mod | 4 +-- go.sum | 10 +++---- internal/cloud/backend_plan.go | 54 +++++++++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index fa43307ed4..89fa59ca67 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,7 @@ require ( github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-plugin v1.4.3 github.com/hashicorp/go-retryablehttp v0.7.2 - github.com/hashicorp/go-tfe v1.18.0 + github.com/hashicorp/go-tfe v1.21.0 github.com/hashicorp/go-uuid v1.0.3 github.com/hashicorp/go-version v1.6.0 github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f @@ -147,7 +147,7 @@ require ( github.com/hashicorp/go-msgpack v0.5.4 // indirect github.com/hashicorp/go-rootcerts v1.0.2 // indirect github.com/hashicorp/go-safetemp v1.0.0 // indirect - github.com/hashicorp/go-slug v0.10.1 // indirect + github.com/hashicorp/go-slug v0.11.0 // indirect github.com/hashicorp/golang-lru v0.5.1 // indirect github.com/hashicorp/serf v0.9.5 // indirect github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect diff --git a/go.sum b/go.sum index 4d4453986b..76cf8b9182 100644 --- a/go.sum +++ b/go.sum @@ -537,13 +537,13 @@ github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5O github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8= github.com/hashicorp/go-safetemp v1.0.0 h1:2HR189eFNrjHQyENnQMMpCiBAsRxzbTMIgBhEyExpmo= github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoDHxNAB65b+Rj1I= -github.com/hashicorp/go-slug v0.10.1 h1:05SCRWCBpCxOeP7stQHvMgOz0raCBCekaytu8Rg/RZ4= -github.com/hashicorp/go-slug v0.10.1/go.mod h1:Ib+IWBYfEfJGI1ZyXMGNbu2BU+aa3Dzu41RKLH301v4= +github.com/hashicorp/go-slug v0.11.0 h1:l7cHWiBk8cnnskjheloW9h8PwXhihvwXbQiiFw2KqkY= +github.com/hashicorp/go-slug v0.11.0/go.mod h1:Ib+IWBYfEfJGI1ZyXMGNbu2BU+aa3Dzu41RKLH301v4= github.com/hashicorp/go-sockaddr v1.0.0 h1:GeH6tui99pF4NJgfnhp+L6+FfobzVW3Ah46sLo0ICXs= github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU= github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= -github.com/hashicorp/go-tfe v1.18.0 h1:AjyZe2KSAyGHD1kbGYlY64PVYQPnJJON24qr97IjIqA= -github.com/hashicorp/go-tfe v1.18.0/go.mod h1:T76X7dHKNEPEugPCZI3gDdaDdxUU4P4sqMZO60W57cQ= +github.com/hashicorp/go-tfe v1.21.0 h1:sTZXf/MaC/iQ8HxKwYSL0xJSEVDwY+h4ngh/+na8vdk= +github.com/hashicorp/go-tfe v1.21.0/go.mod h1:jedlLiHHiDeBKKpON4aIpTdsKbc2OaVbklEPI7XEHiY= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= @@ -762,7 +762,7 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.194/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.588 h1:DYtBXB7sVc3EOW5horg8j55cLZynhsLYhHrvQ/jXKKM= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.588/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y= diff --git a/internal/cloud/backend_plan.go b/internal/cloud/backend_plan.go index 8854d104ec..880233637d 100644 --- a/internal/cloud/backend_plan.go +++ b/internal/cloud/backend_plan.go @@ -11,6 +11,7 @@ import ( "log" "os" "path/filepath" + "strconv" "strings" "syscall" "time" @@ -428,10 +429,10 @@ func (b *Cloud) renderPlanLogs(ctx context.Context, op *backend.Operation, run * return nil } - // Only call the renderer if the remote workspace has structured run output - // 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 { + // Determine whether we should call the renderer to generate the plan output + // in human readable format. Otherwise we risk duplicate plan output since + // plan output may be contained in the streamed log file. + if ok, err := b.shouldRenderStructuredRunOutput(run); ok { // Fetch the redacted plan. redacted, err := readRedactedPlan(ctx, b.client.BaseURL(), b.token, run.Plan.ID) if err != nil { @@ -440,11 +441,56 @@ func (b *Cloud) renderPlanLogs(ctx context.Context, op *backend.Operation, run * // Render plan output. b.renderer.RenderHumanPlan(*redacted, op.PlanMode) + } else if err != nil { + return err } return nil } +// shouldRenderStructuredRunOutput ensures the remote workspace has structured +// run output enabled and, if using Terraform Enterprise, ensures it is a release +// that supports enabling SRO for CLI-driven runs. The plan output will have +// already been rendered when the logs were read if this wasn't the case. +func (b *Cloud) shouldRenderStructuredRunOutput(run *tfe.Run) (bool, error) { + if b.renderer == nil || !run.Workspace.StructuredRunOutputEnabled { + return false, nil + } + + // If the cloud backend is configured against TFC, we only require that + // the workspace has structured run output enabled. + if b.client.IsCloud() && run.Workspace.StructuredRunOutputEnabled { + fmt.Println("we should see this") + return true, nil + } + + // If the cloud backend is configured against TFE, ensure the release version + // supports enabling SRO for CLI runs. + if b.client.IsEnterprise() { + tfeVersion := b.client.RemoteTFEVersion() + if tfeVersion != "" { + v := strings.Split(tfeVersion[1:], "-") + releaseDate, err := strconv.Atoi(v[0]) + if err != nil { + return false, err + } + + fmt.Println(releaseDate) + + // Any release older than 202302-1 will not support enabling SRO for + // CLI-driven runs + if releaseDate < 202302 { + return false, nil + } else if run.Workspace.StructuredRunOutputEnabled { + return true, nil + } + } + } + + // Version of TFE is unknowable + return false, 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]