terraform/internal/plans/objchange/all_null.go

34 lines
1.2 KiB
Go
Raw Normal View History

2019-02-07 23:01:39 +01:00
package objchange
import (
"github.com/hashicorp/terraform/internal/configs/configschema"
2019-02-07 23:01:39 +01:00
"github.com/zclconf/go-cty/cty"
)
// AllBlockAttributesNull constructs a non-null cty.Value of the object type implied
2019-02-07 23:01:39 +01:00
// by the given schema that has all of its leaf attributes set to null and all
// of its nested block collections set to zero-length.
//
// This simulates what would result from decoding an empty configuration block
// with the given schema, except that it does not produce errors
func AllBlockAttributesNull(schema *configschema.Block) cty.Value {
configs/configschema: Introduce the NestingGroup mode for blocks In study of existing providers we've found a pattern we werent previously accounting for of using a nested block type to represent a group of arguments that relate to a particular feature that is always enabled but where it improves configuration readability to group all of its settings together in a nested block. The existing NestingSingle was not a good fit for this because it is designed under the assumption that the presence or absence of the block has some significance in enabling or disabling the relevant feature, and so for these always-active cases we'd generate a misleading plan where the settings for the feature appear totally absent, rather than showing the default values that will be selected. NestingGroup is, therefore, a slight variation of NestingSingle where presence vs. absence of the block is not distinguishable (it's never null) and instead its contents are treated as unset when the block is absent. This then in turn causes any default values associated with the nested arguments to be honored and displayed in the plan whenever the block is not explicitly configured. The current SDK cannot activate this mode, but that's okay because its "legacy type system" opt-out flag allows it to force a block to be processed in this way anyway. We're adding this now so that we can introduce the feature in a future SDK without causing a breaking change to the protocol, since the set of possible block nesting modes is not extensible.
2019-04-09 00:32:53 +02:00
// "All attributes null" happens to be the definition of EmptyValue for
// a Block, so we can just delegate to that.
return schema.EmptyValue()
2019-02-07 23:01:39 +01:00
}
// AllAttributesNull returns a cty.Value of the object type implied by the given
// attriubutes that has all of its leaf attributes set to null.
func AllAttributesNull(attrs map[string]*configschema.Attribute) cty.Value {
newAttrs := make(map[string]cty.Value, len(attrs))
for name, attr := range attrs {
if attr.NestedType != nil {
newAttrs[name] = AllAttributesNull(attr.NestedType.Attributes)
} else {
newAttrs[name] = cty.NullVal(attr.Type)
}
}
return cty.ObjectVal(newAttrs)
}