Skip to content

Commit

Permalink
Fix incorrect filtering of no-op changes (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuintenBruynseraede authored Mar 28, 2024
1 parent 4a0c9a2 commit 9e403ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion terraformstate/terraform_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func importedResources(resources ResourceChanges) ResourceChanges {
func FilterNoOpResources(ts *tfjson.Plan) {
acc := make(ResourceChanges, 0)
for _, r := range ts.ResourceChanges {
if len(r.Change.Actions) == 1 && r.Change.Actions[0] == "no-op" && r.Change.Importing != nil && r.Change.Importing.ID == "" {
if len(r.Change.Actions) == 1 && r.Change.Actions[0] == "no-op" && (r.Change.Importing == nil || r.Change.Importing.ID == "") {
continue
}
acc = append(acc, r)
Expand Down
18 changes: 18 additions & 0 deletions terraformstate/terraform_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

. "github.com/hashicorp/terraform-json"
tfjson "github.com/hashicorp/terraform-json"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -59,3 +60,20 @@ func TestResourceChangeColorAndSuffixImport(t *testing.T) {
assert.Equal(t, color, ColorCyan)
assert.Equal(t, suffix, "(i)")
}

func TestFilterNoOpResources(t *testing.T) {
resourceChanges := ResourceChanges{
&ResourceChange{Address: "no-op1", Change: &Change{Actions: Actions{ActionNoop}}},
&ResourceChange{Address: "no-op3", Change: &Change{Actions: Actions{ActionNoop}, Importing: nil}},
&ResourceChange{Address: "no-op2", Change: &Change{Actions: Actions{ActionNoop}, Importing: &Importing{ID: ""}}},
&ResourceChange{Address: "create", Change: &Change{Actions: Actions{ActionCreate}}},
}
plan := tfjson.Plan{ResourceChanges: resourceChanges}

FilterNoOpResources(&plan)

expectedResourceChangesAfterFiltering := ResourceChanges{
&ResourceChange{Address: "create", Change: &Change{Actions: Actions{ActionCreate}}},
}
assert.Equal(t, expectedResourceChangesAfterFiltering, plan.ResourceChanges)
}

0 comments on commit 9e403ca

Please sign in to comment.