prevent panics when encountering nil diffs

While we can't change the behavior of helper/schema at this point, we
can protect against panics in the case of unexpected nils in the
instance diff.
This commit is contained in:
James Bardin 2019-09-04 16:39:45 -04:00
parent 481b25b5d2
commit a94f5ee132
2 changed files with 14 additions and 0 deletions

View File

@ -47,6 +47,8 @@ func TestDiffApply_set(t *testing.T) {
"egress.746197026.security_groups.#": {Old: "", New: "0", NewComputed: false, NewRemoved: false},
"egress.746197026.self": {Old: "", New: "false", NewComputed: false, NewRemoved: false},
"egress.746197026.to_port": {Old: "", New: "8000", NewComputed: false, NewRemoved: false},
// an erroneous nil diff should do nothing
"egress.111111111.to_port": nil,
},
}

View File

@ -513,6 +513,12 @@ func (d *InstanceDiff) applyBlockDiff(path []string, attrs map[string]string, sc
}
for k, diff := range d.Attributes {
// helper/schema should not insert nil diff values, but don't panic
// if it does.
if diff == nil {
continue
}
if strings.HasPrefix(k, blockKey) {
nextDot := strings.Index(k[len(blockKey):], ".")
if nextDot < 0 {
@ -539,6 +545,12 @@ func (d *InstanceDiff) applyBlockDiff(path []string, attrs map[string]string, sc
// that we're dropping. Since we're only applying the "New"
// portion of the set, we can ignore diffs that only contain "Old"
for attr, diff := range d.Attributes {
// helper/schema should not insert nil diff values, but don't panic
// if it does.
if diff == nil {
continue
}
if !strings.HasPrefix(attr, indexPrefix) {
continue
}