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

implement plan-diff command #1134

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/terraform_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func getTerraformCommands() []*cobra.Command {
"nativeCommand": "true",
},
},
{
Use: "plan-diff",
Short: "Compare two Terraform plans",
Long: "Generate a visual diff between two Terraform plans. If the new plan is not provided, it will run 'terraform plan' to generate one.",
},
{
Use: "apply",
Short: "Apply changes to infrastructure",
Expand Down Expand Up @@ -288,6 +293,10 @@ var commandMaps = map[string]func(cmd *cobra.Command){
cmd.PersistentFlags().Bool("from-plan", false, "If set atmos will use the previously generated plan file")
cmd.PersistentFlags().String("planfile", "", "Set the plan file to use")
},
"plan-diff": func(cmd *cobra.Command) {
cmd.PersistentFlags().String("orig", "", "Original Terraform plan file path")
cmd.PersistentFlags().String("new", "", "New Terraform plan file path (optional, if not provided, a new plan will be generated)")
},
"clean": func(cmd *cobra.Command) {
cmd.PersistentFlags().Bool("everything", false, "If set atmos will also delete the Terraform state files and directories for the component.")
cmd.PersistentFlags().Bool("force", false, "Forcefully delete Terraform state files and directories without interaction")
Expand Down
7 changes: 7 additions & 0 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,13 @@
!u.SliceContainsStringHasPrefix(info.AdditionalArgsAndFlags, outFlag+"=") {
allArgsAndFlags = append(allArgsAndFlags, []string{outFlag, planFile}...)
}
case "plan-diff":
// The plan-diff command is handled separately
err = executeTerraformPlanDiff(atmosConfig, info, componentPath, varFile, planFile)
if err != nil {
return err
}
return nil

Check warning on line 359 in internal/exec/terraform.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/terraform.go#L353-L359

Added lines #L353 - L359 were not covered by tests
case "destroy":
allArgsAndFlags = append(allArgsAndFlags, []string{varFileFlag, varFile}...)
case "import":
Expand Down
47 changes: 47 additions & 0 deletions internal/exec/terraform_plan_diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package exec

import (
"strings"
"testing"

"github.com/cloudposse/atmos/pkg/schema"
)

// TestPlanDiffCommandRouting tests that the plan-diff command is correctly routed
// in the ExecuteTerraform function's switch statement.
//
// NOTE: This test will show a linter error when run separately with golangci-lint,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have many lint rules disabled in *_test.go files. Is there another linter we should exclude?

// because ExecuteTerraform is defined in terraform.go and not visible when linting
// this file alone. However, it works correctly when running 'go test' because
// all files in the package are compiled together.
func TestPlanDiffCommandRouting(t *testing.T) {
// Create minimal info with the plan-diff command
info := schema.ConfigAndStacksInfo{
Command: "terraform",
SubCommand: "plan-diff",
ComponentFromArg: "test-component",
Component: "test-component",
FinalComponent: "test-component",
ComponentIsEnabled: true,
AdditionalArgsAndFlags: []string{"--orig", "test.tfplan"},
}

// Execute the function - we expect it to fail because we haven't set up a proper environment
err := ExecuteTerraform(info) // This line will show a linter error but works in tests

// Verify we get an error since we have an incomplete setup
if err == nil {
t.Fatalf("Expected ExecuteTerraform to fail with incomplete setup")
}

// Check the error message to make sure it's not indicating that the command wasn't recognized
errMsg := err.Error()
t.Logf("Error: %v", errMsg)

// If the error indicates that the command wasn't recognized, the test should fail
if strings.Contains(errMsg, "unknown command") ||
strings.Contains(errMsg, "unrecognized command") ||
strings.Contains(errMsg, "invalid command") {
t.Errorf("Error suggests the plan-diff command wasn't recognized in the switch statement")
}
}
Loading