Skip to content

Commit 90544b0

Browse files
committed
feat(terraformstate): handle TF 1.12-style imports
Terraform 1.12 introduced a new way of importing resources using a identity structure rather than a formatted string.
1 parent 03c3e70 commit 90544b0

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.21
55
require (
66
github.com/fatih/color v1.15.0
77
github.com/golang/mock v1.6.0
8-
github.com/hashicorp/terraform-json v0.20.0
8+
github.com/hashicorp/terraform-json v0.27.2
99
github.com/m1gwings/treedrawer v0.3.3-beta
1010
github.com/nsf/jsondiff v0.0.0-20210926074059-1e845ec5d249
1111
github.com/olekukonko/tablewriter v0.0.5
@@ -15,12 +15,12 @@ require (
1515
require (
1616
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
1717
github.com/davecgh/go-spew v1.1.1 // indirect
18-
github.com/hashicorp/go-version v1.6.0 // indirect
18+
github.com/hashicorp/go-version v1.7.0 // indirect
1919
github.com/mattn/go-colorable v0.1.13 // indirect
2020
github.com/mattn/go-isatty v0.0.17 // indirect
2121
github.com/mattn/go-runewidth v0.0.9 // indirect
2222
github.com/pmezard/go-difflib v1.0.0 // indirect
23-
github.com/zclconf/go-cty v1.14.1 // indirect
23+
github.com/zclconf/go-cty v1.16.4 // indirect
2424
golang.org/x/sys v0.6.0 // indirect
2525
golang.org/x/text v0.11.0 // indirect
2626
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
77
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
88
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
99
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
10-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
11-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
12-
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
13-
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
14-
github.com/hashicorp/terraform-json v0.20.0 h1:cJcvn4gIOTi0SD7pIy+xiofV1zFA3hza+6K+fo52IX8=
15-
github.com/hashicorp/terraform-json v0.20.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk=
10+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
11+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
12+
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
13+
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
14+
github.com/hashicorp/terraform-json v0.27.2 h1:BwGuzM6iUPqf9JYM/Z4AF1OJ5VVJEEzoKST/tRDBJKU=
15+
github.com/hashicorp/terraform-json v0.27.2/go.mod h1:GzPLJ1PLdUG5xL6xn1OXWIjteQRT2CNT9o/6A9mi9hE=
1616
github.com/m1gwings/treedrawer v0.3.3-beta h1:VeeQ4I90+NL0G2Tga3H4EY4hbOyVP3ID4T93r21oLbQ=
1717
github.com/m1gwings/treedrawer v0.3.3-beta/go.mod h1:Sebh5tCtjQWAG/B9xWct163vB9pCbBcA1ykaUErDUTY=
1818
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
@@ -32,8 +32,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
3232
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
3333
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3434
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
35-
github.com/zclconf/go-cty v1.14.1 h1:t9fyA35fwjjUMcmL5hLER+e/rEPqrbCK1/OSE4SI9KA=
36-
github.com/zclconf/go-cty v1.14.1/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
35+
github.com/zclconf/go-cty v1.16.4 h1:QGXaag7/7dCzb+odlGrgr+YmYZFaOCMW6DEpS+UD1eE=
36+
github.com/zclconf/go-cty v1.16.4/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
3737
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3838
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
3939
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=

terraformstate/terraform_state.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ func importedResources(resources ResourceChanges) ResourceChanges {
8383
}
8484

8585
id := r.Change.Importing.ID
86-
if id != "" {
86+
identity := r.Change.Importing.Identity
87+
if id != "" || identity != nil {
8788
acc = append(acc, r)
8889
}
8990
}
@@ -93,7 +94,13 @@ func importedResources(resources ResourceChanges) ResourceChanges {
9394
func FilterNoOpResources(ts *tfjson.Plan) {
9495
acc := make(ResourceChanges, 0)
9596
for _, r := range ts.ResourceChanges {
96-
if len(r.Change.Actions) == 1 && r.Change.Actions[0] == "no-op" && (r.Change.Importing == nil || r.Change.Importing.ID == "") {
97+
// ID-based importing
98+
oldImporting := r.Change.Importing != nil && r.Change.Importing.ID != ""
99+
100+
// New identity-based importing introduced in terraform 1.12
101+
newImporting := r.Change.Importing != nil && r.Change.Importing.Identity != nil
102+
103+
if r.Change.Actions.NoOp() && !oldImporting && !newImporting {
97104
continue
98105
}
99106
acc = append(acc, r)

terraformstate/terraform_state_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,22 @@ func TestResourceChangeColorAndSuffixImport(t *testing.T) {
130130
}
131131

132132
func TestFilterNoOpResources(t *testing.T) {
133+
identityImport := &ResourceChange{Address: "no-op5", Change: &Change{Actions: Actions{ActionNoop}, Importing: &Importing{Identity: struct{ Account string }{Account: "account ID"}}}}
133134
resourceChanges := ResourceChanges{
134135
&ResourceChange{Address: "no-op1", Change: &Change{Actions: Actions{ActionNoop}}},
135136
&ResourceChange{Address: "no-op3", Change: &Change{Actions: Actions{ActionNoop}, Importing: nil}},
136137
&ResourceChange{Address: "no-op2", Change: &Change{Actions: Actions{ActionNoop}, Importing: &Importing{ID: ""}}},
138+
&ResourceChange{Address: "no-op4", Change: &Change{Actions: Actions{ActionNoop}, Importing: &Importing{Identity: nil}}},
137139
&ResourceChange{Address: "create", Change: &Change{Actions: Actions{ActionCreate}}},
140+
identityImport,
138141
}
139142
plan := tfjson.Plan{ResourceChanges: resourceChanges}
140143

141144
FilterNoOpResources(&plan)
142145

143146
expectedResourceChangesAfterFiltering := ResourceChanges{
144147
&ResourceChange{Address: "create", Change: &Change{Actions: Actions{ActionCreate}}},
148+
identityImport,
145149
}
146150
assert.Equal(t, expectedResourceChangesAfterFiltering, plan.ResourceChanges)
147151
}

0 commit comments

Comments
 (0)