Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/resource: Add terraform plan output to TRACE logging #1058

Merged
merged 3 commits into from Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1058.txt
@@ -0,0 +1,3 @@
```release-note:enhancement
helper/resource: Added `terraform plan` output to `TRACE` logging
```
3 changes: 3 additions & 0 deletions internal/logging/keys.go
Expand Up @@ -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"
)
44 changes: 40 additions & 4 deletions internal/plugintest/working_dir.go
Expand Up @@ -174,23 +174,59 @@ 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
// file, which if successful will then be used for the next call to Apply.
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
Expand Down