Merge pull request #17261 from hashicorp/b-diff-crash-set

helper/schema: Prevent crash on removal of computed field in CustomizeDiff
This commit is contained in:
Radek Simko 2018-02-01 13:33:28 +00:00 committed by GitHub
commit a4388b0ef2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 7 deletions

View File

@ -427,6 +427,13 @@ func (m schemaMap) Diff(
} }
} }
// Remove any nil diffs just to keep things clean
for k, v := range result.Attributes {
if v == nil {
delete(result.Attributes, k)
}
}
// If this is a non-destroy diff, call any custom diff logic that has been // If this is a non-destroy diff, call any custom diff logic that has been
// defined. // defined.
if !result.DestroyTainted && customizeDiff != nil { if !result.DestroyTainted && customizeDiff != nil {
@ -521,13 +528,6 @@ func (m schemaMap) Diff(
result = result2 result = result2
} }
// Remove any nil diffs just to keep things clean
for k, v := range result.Attributes {
if v == nil {
delete(result.Attributes, k)
}
}
// Go through and detect all of the ComputedWhens now that we've // Go through and detect all of the ComputedWhens now that we've
// finished the diff. // finished the diff.
// TODO // TODO

View File

@ -3142,6 +3142,53 @@ func TestSchemaMap_Diff(t *testing.T) {
"var.foo": interfaceToVariableSwallowError(""), "var.foo": interfaceToVariableSwallowError(""),
}, },
}, },
{
Name: "optional, computed, empty string should not crash in CustomizeDiff",
Schema: map[string]*Schema{
"unrelated_set": {
Type: TypeSet,
Optional: true,
Elem: &Schema{Type: TypeString},
},
"stream_enabled": {
Type: TypeBool,
Optional: true,
},
"stream_view_type": {
Type: TypeString,
Optional: true,
Computed: true,
},
},
State: &terraform.InstanceState{
Attributes: map[string]string{
"unrelated_set.#": "0",
"stream_enabled": "true",
"stream_view_type": "KEYS_ONLY",
},
},
Config: map[string]interface{}{
"stream_enabled": false,
"stream_view_type": "",
},
CustomizeDiff: func(diff *ResourceDiff, v interface{}) error {
v, ok := diff.GetOk("unrelated_set")
if ok {
return fmt.Errorf("Didn't expect unrelated_set: %#v", v)
}
return nil
},
Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"stream_enabled": {
Old: "true",
New: "false",
},
},
},
},
} }
for i, tc := range cases { for i, tc := range cases {