helper/schema: Prevent crash on removal of computed field in CustomizeDiff

This commit is contained in:
Radek Simko 2018-02-01 12:05:22 +00:00
parent 1ba8691f35
commit 7af1c2b3a4
No known key found for this signature in database
GPG Key ID: 1F1C84FE689A88D7
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
// defined.
if !result.DestroyTainted && customizeDiff != nil {
@ -521,13 +528,6 @@ func (m schemaMap) Diff(
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
// finished the diff.
// TODO

View File

@ -3142,6 +3142,53 @@ func TestSchemaMap_Diff(t *testing.T) {
"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 {