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

Sort results #75

Merged
merged 4 commits into from
Aug 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ jobs:
uses: actions/setup-go@v4
with:
go-version: "1.21"

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest

- name: Linter
uses: docker://morphy/revive-action:v2

- name: Test
run: go test -v ./...
Expand Down
17 changes: 17 additions & 0 deletions terraformstate/terraform_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terraformstate
import (
"encoding/json"
"fmt"
"sort"

tfjson "github.com/hashicorp/terraform-json"
)
Expand Down Expand Up @@ -107,6 +108,18 @@ func GetAllResourceChanges(plan tfjson.Plan) map[string]ResourceChanges {
recreatedResources := recreatedResources(plan.ResourceChanges)
importedResources := importedResources(plan.ResourceChanges)

sortResources := func(resources ResourceChanges) {
sort.Slice(resources, func(i, j int) bool {
return resources[i].Address < resources[j].Address
})
}

sortResources(addedResources)
sortResources(deletedResources)
sortResources(updatedResources)
sortResources(recreatedResources)
sortResources(importedResources)

return map[string]ResourceChanges{
"import": importedResources,
"add": addedResources,
Expand All @@ -123,6 +136,10 @@ func GetAllOutputChanges(plan tfjson.Plan) map[string][]string {
deletedResources := filterOutputs(plan.OutputChanges, "delete")
updatedResources := filterOutputs(plan.OutputChanges, "update")

sort.Strings(addedResources)
sort.Strings(deletedResources)
sort.Strings(updatedResources)

return map[string][]string{
"add": addedResources,
"delete": deletedResources,
Expand Down
68 changes: 68 additions & 0 deletions terraformstate/terraform_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestResourceChangeColor(t *testing.T) {

assert.Equal(t, color, expectedColor)
}

CreateDelete := &ResourceChange{Change: &Change{Actions: []Action{ActionCreate, ActionDelete}}}
color, _ := GetColorPrefixAndSuffixText(CreateDelete)
assert.Equal(t, color, ColorMagenta)
Expand All @@ -31,6 +32,73 @@ func TestResourceChangeColor(t *testing.T) {
assert.Equal(t, color, ColorMagenta)
}

func TestGetAllResourceChanges(t *testing.T) {
resourceChanges := ResourceChanges{
&ResourceChange{Address: "create2", Change: &Change{Actions: Actions{ActionCreate}}},
&ResourceChange{Address: "create1", Change: &Change{Actions: Actions{ActionCreate}}},
&ResourceChange{Address: "delete2", Change: &Change{Actions: Actions{ActionDelete}}},
&ResourceChange{Address: "delete1", Change: &Change{Actions: Actions{ActionDelete}}},
&ResourceChange{Address: "update2", Change: &Change{Actions: Actions{ActionUpdate}}},
&ResourceChange{Address: "update1", Change: &Change{Actions: Actions{ActionUpdate}}},
&ResourceChange{Address: "import2", Change: &Change{Importing: &Importing{ID: "id1"}}},
&ResourceChange{Address: "import1", Change: &Change{Importing: &Importing{ID: "id2"}}},
&ResourceChange{Address: "recreate2", Change: &Change{Actions: Actions{ActionDelete, ActionCreate}}},
&ResourceChange{Address: "recreate1", Change: &Change{Actions: Actions{ActionDelete, ActionCreate}}},
}
plan := tfjson.Plan{ResourceChanges: resourceChanges}

result := GetAllResourceChanges(plan)

expectedResourceChanges := map[string]ResourceChanges{
"add": {
&ResourceChange{Address: "create1", Change: &Change{Actions: Actions{ActionCreate}}},
&ResourceChange{Address: "create2", Change: &Change{Actions: Actions{ActionCreate}}},
},
"delete": {
&ResourceChange{Address: "delete1", Change: &Change{Actions: Actions{ActionDelete}}},
&ResourceChange{Address: "delete2", Change: &Change{Actions: Actions{ActionDelete}}},
},
"update": {
&ResourceChange{Address: "update1", Change: &Change{Actions: Actions{ActionUpdate}}},
&ResourceChange{Address: "update2", Change: &Change{Actions: Actions{ActionUpdate}}},
},
"recreate": {
&ResourceChange{Address: "recreate1", Change: &Change{Actions: Actions{ActionDelete, ActionCreate}}},
&ResourceChange{Address: "recreate2", Change: &Change{Actions: Actions{ActionDelete, ActionCreate}}},
},
"import": {
&ResourceChange{Address: "import1", Change: &Change{Importing: &Importing{ID: "id2"}}},
&ResourceChange{Address: "import2", Change: &Change{Importing: &Importing{ID: "id1"}}},
},
}

assert.Equal(t, expectedResourceChanges, result)
}

func TestGetAllOutputChanges(t *testing.T) {

outputChanges := map[string]*Change{
"create2": {Actions: Actions{ActionCreate}},
"create1": {Actions: Actions{ActionCreate}},
"delete2": {Actions: Actions{ActionDelete}},
"delete1": {Actions: Actions{ActionDelete}},
"update2": {Actions: Actions{ActionUpdate}},
"update1": {Actions: Actions{ActionUpdate}},
}

plan := tfjson.Plan{OutputChanges: outputChanges}

result := GetAllOutputChanges(plan)

expectedResourceChanges := map[string][]string{
"add": {"create1", "create2"},
"delete": {"delete1", "delete2"},
"update": {"update1", "update2"},
}

assert.Equal(t, expectedResourceChanges, result)
}

func TestResourceChangeSuffix(t *testing.T) {
ExpectedSuffix := map[Action]string{
ActionCreate: "(+)",
Expand Down
Loading