remove empty timeouts blocks in copyTimeoutValues

The hcl2shims will always add in the timeouts block, because there's no
way to differentiate a null single block from an empty one in the
flatmapped state. Since we are only concerned with keeping the prior
timeouts value, always set the new value to null, and then copy over the
prior value if it exists.
This commit is contained in:
James Bardin 2019-03-02 11:30:37 -05:00
parent e50be82da4
commit 33d5ddf291
1 changed files with 20 additions and 8 deletions

View File

@ -1125,22 +1125,34 @@ func normalizeFlatmapContainers(prior map[string]string, attrs map[string]string
// the Private/Meta fields. we need to copy those values into the planned state // the Private/Meta fields. we need to copy those values into the planned state
// so that core doesn't see a perpetual diff with the timeout block. // so that core doesn't see a perpetual diff with the timeout block.
func copyTimeoutValues(to cty.Value, from cty.Value) cty.Value { func copyTimeoutValues(to cty.Value, from cty.Value) cty.Value {
// if `from` is null, then there are no attributes, and if `to` is null we // if `to` is null we are planning to remove it altogether.
// are planning to remove it altogether. if to.IsNull() {
if from.IsNull() || to.IsNull() {
return to return to
} }
toAttrs := to.AsValueMap()
// We need to remove the key since the hcl2shims will add a non-null block
// because we can't determine if a single block was null from the flatmapped
// values. This needs to conform to the correct schema for marshaling, so
// change the value to null rather than deleting it from the object map.
timeouts, ok := toAttrs[schema.TimeoutsConfigKey]
if ok {
toAttrs[schema.TimeoutsConfigKey] = cty.NullVal(timeouts.Type())
}
// if from is null then there are no timeouts to copy
if from.IsNull() {
return cty.ObjectVal(toAttrs)
}
fromAttrs := from.AsValueMap() fromAttrs := from.AsValueMap()
timeouts, ok := fromAttrs[schema.TimeoutsConfigKey] timeouts, ok = fromAttrs[schema.TimeoutsConfigKey]
// no timeouts to copy // timeouts shouldn't be unknown, but don't copy possibly invalid values either
// timeouts shouldn't be unknown, but don't copy possibly invalid values
if !ok || timeouts.IsNull() || !timeouts.IsWhollyKnown() { if !ok || timeouts.IsNull() || !timeouts.IsWhollyKnown() {
return to // no timeouts block to copy
return cty.ObjectVal(toAttrs)
} }
toAttrs := to.AsValueMap()
toAttrs[schema.TimeoutsConfigKey] = timeouts toAttrs[schema.TimeoutsConfigKey] = timeouts
return cty.ObjectVal(toAttrs) return cty.ObjectVal(toAttrs)