Skip to content

Commit c5f85f9

Browse files
authored
Merge pull request hashicorp#9356 from hashicorp/jbardin/TF-9337
Filter nil Deposed values during State init
2 parents 6721a8a + 95786c5 commit c5f85f9

4 files changed

Lines changed: 61 additions & 16 deletions

File tree

terraform/state.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,9 +1403,18 @@ func (s *ResourceState) init() {
14031403
s.Deposed = make([]*InstanceState, 0)
14041404
}
14051405

1406-
for _, dep := range s.Deposed {
1407-
dep.init()
1406+
// clean out any possible nil values read in from the state file
1407+
end := len(s.Deposed) - 1
1408+
for i := 0; i <= end; i++ {
1409+
if s.Deposed[i] == nil {
1410+
s.Deposed[i], s.Deposed[end] = s.Deposed[end], s.Deposed[i]
1411+
end--
1412+
i--
1413+
} else {
1414+
s.Deposed[i].init()
1415+
}
14081416
}
1417+
s.Deposed = s.Deposed[:end+1]
14091418
}
14101419

14111420
func (s *ResourceState) deepcopy() *ResourceState {

terraform/state_test.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,30 +254,66 @@ func TestStateModuleOrphans_deepNestedNilConfig(t *testing.T) {
254254

255255
func TestStateDeepCopy(t *testing.T) {
256256
cases := []struct {
257-
One, Two *State
258-
F func(*State) interface{}
257+
State *State
259258
}{
260259
// Version
261260
{
262261
&State{Version: 5},
263-
&State{Version: 5},
264-
func(s *State) interface{} { return s.Version },
265262
},
266-
267263
// TFVersion
268264
{
269265
&State{TFVersion: "5"},
270-
&State{TFVersion: "5"},
271-
func(s *State) interface{} { return s.TFVersion },
266+
},
267+
// Modules
268+
{
269+
&State{
270+
Version: 6,
271+
Modules: []*ModuleState{
272+
&ModuleState{
273+
Path: rootModulePath,
274+
Resources: map[string]*ResourceState{
275+
"test_instance.foo": &ResourceState{
276+
Primary: &InstanceState{
277+
Meta: map[string]string{},
278+
},
279+
},
280+
},
281+
},
282+
},
283+
},
284+
},
285+
// Deposed
286+
// The nil values shouldn't be there if the State was properly init'ed,
287+
// but the Copy should still work anyway.
288+
{
289+
&State{
290+
Version: 6,
291+
Modules: []*ModuleState{
292+
&ModuleState{
293+
Path: rootModulePath,
294+
Resources: map[string]*ResourceState{
295+
"test_instance.foo": &ResourceState{
296+
Primary: &InstanceState{
297+
Meta: map[string]string{},
298+
},
299+
Deposed: []*InstanceState{
300+
{ID: "test"},
301+
nil,
302+
},
303+
},
304+
},
305+
},
306+
},
307+
},
272308
},
273309
}
274310

275311
for i, tc := range cases {
276312
t.Run(fmt.Sprintf("copy-%d", i), func(t *testing.T) {
277-
actual := tc.F(tc.One.DeepCopy())
278-
expected := tc.F(tc.Two)
313+
actual := tc.State.DeepCopy()
314+
expected := tc.State
279315
if !reflect.DeepEqual(actual, expected) {
280-
t.Fatalf("Bad: %d\n\n%s\n\n%s", i, actual, expected)
316+
t.Fatalf("Expected: %#v\nRecevied: %#v\n", expected, actual)
281317
}
282318
})
283319
}

vendor/github.com/mitchellh/copystructure/copystructure.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/vendor.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,10 +1474,10 @@
14741474
"revision": "8631ce90f28644f54aeedcb3e389a85174e067d1"
14751475
},
14761476
{
1477-
"checksumSHA1": "BsIMq23KDyxdhQC7g2dDz/9oHbA=",
1477+
"checksumSHA1": "guxbLo8KHHBeM0rzou4OTzzpDNs=",
14781478
"path": "github.com/mitchellh/copystructure",
1479-
"revision": "c4815f984fb5c5486f6db1c9a5660e7605fd4c20",
1480-
"revisionTime": "2016-10-03T18:23:19Z"
1479+
"revision": "5af94aef99f597e6a9e1f6ac6be6ce0f3c96b49d",
1480+
"revisionTime": "2016-10-13T19:53:42Z"
14811481
},
14821482
{
14831483
"path": "github.com/mitchellh/go-homedir",

0 commit comments

Comments
 (0)