diff --git a/.changelog/1058.txt b/.changelog/1058.txt new file mode 100644 index 0000000000..fbba04d609 --- /dev/null +++ b/.changelog/1058.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +helper/resource: Added `terraform plan` output to `TRACE` logging +``` diff --git a/internal/logging/keys.go b/internal/logging/keys.go index 03931b0247..86700506a9 100644 --- a/internal/logging/keys.go +++ b/internal/logging/keys.go @@ -49,6 +49,9 @@ const ( // The path to the Terraform CLI used for an acceptance test. KeyTestTerraformPath = "test_terraform_path" + // Terraform plan output generated during a TestStep. + KeyTestTerraformPlan = "test_terraform_plan" + // The working directory of the acceptance test. KeyTestWorkingDirectory = "test_working_directory" ) diff --git a/internal/plugintest/working_dir.go b/internal/plugintest/working_dir.go index ecbf5aac1c..f9f0e5923d 100644 --- a/internal/plugintest/working_dir.go +++ b/internal/plugintest/working_dir.go @@ -174,11 +174,29 @@ func (wd *WorkingDir) planFilename() string { func (wd *WorkingDir) CreatePlan(ctx context.Context) error { logging.HelperResourceTrace(ctx, "Calling Terraform CLI plan command") - _, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out(PlanFileName)) + hasChanges, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out(PlanFileName)) logging.HelperResourceTrace(ctx, "Called Terraform CLI plan command") - return err + if err != nil { + return err + } + + if !hasChanges { + logging.HelperResourceTrace(ctx, "Created plan with no changes") + + return nil + } + + stdout, err := wd.SavedPlanRawStdout(ctx) + + if err != nil { + return fmt.Errorf("error retrieving formatted plan output: %w", err) + } + + logging.HelperResourceTrace(ctx, "Created plan with changes", map[string]any{logging.KeyTestTerraformPlan: stdout}) + + return nil } // CreateDestroyPlan runs "terraform plan -destroy" to create a saved plan @@ -186,11 +204,29 @@ func (wd *WorkingDir) CreatePlan(ctx context.Context) error { func (wd *WorkingDir) CreateDestroyPlan(ctx context.Context) error { logging.HelperResourceTrace(ctx, "Calling Terraform CLI plan -destroy command") - _, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out(PlanFileName), tfexec.Destroy(true)) + hasChanges, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out(PlanFileName), tfexec.Destroy(true)) logging.HelperResourceTrace(ctx, "Called Terraform CLI plan -destroy command") - return err + if err != nil { + return err + } + + if !hasChanges { + logging.HelperResourceTrace(ctx, "Created destroy plan with no changes") + + return nil + } + + stdout, err := wd.SavedPlanRawStdout(ctx) + + if err != nil { + return fmt.Errorf("error retrieving formatted plan output: %w", err) + } + + logging.HelperResourceTrace(ctx, "Created destroy plan with changes", map[string]any{logging.KeyTestTerraformPlan: stdout}) + + return nil } // Apply runs "terraform apply". If CreatePlan has previously completed