1 // schema is a high-level framework for easily writing new providers
2 // for Terraform. Usage of schema is recommended over attempting to write
3 // to the low-level plugin interfaces manually.
5 // schema breaks down provider creation into simple CRUD operations for
6 // resources. The logic of diffing, destroying before creating, updating
7 // or creating, etc. is all handled by the framework. The plugin author
8 // only needs to implement a configuration schema and the CRUD operations and
9 // everything else is meant to just work.
11 // A good starting point is to view the Provider structure.
25 "github.com/hashicorp/terraform/config"
26 "github.com/hashicorp/terraform/terraform"
27 "github.com/mitchellh/copystructure"
28 "github.com/mitchellh/mapstructure"
31 // Name of ENV variable which (if not empty) prefers panic over error
32 const PanicOnErr = "TF_SCHEMA_PANIC_ON_ERROR"
34 // type used for schema package context keys
35 type contextKey string
38 protoVersionMu sync.Mutex
42 func isProto5() bool {
44 defer protoVersionMu.Unlock()
49 // SetProto5 enables a feature flag for any internal changes required required
50 // to work with the new plugin protocol. This should not be called by
54 defer protoVersionMu.Unlock()
58 // Schema is used to describe the structure of a value.
60 // Read the documentation of the struct elements for important details.
62 // Type is the type of the value and must be one of the ValueType values.
64 // This type not only determines what type is expected/valid in configuring
65 // this value, but also what type is returned when ResourceData.Get is
66 // called. The types returned by Get are:
70 // TypeFloat - float64
71 // TypeString - string
72 // TypeList - []interface{}
73 // TypeMap - map[string]interface{}
74 // TypeSet - *schema.Set
78 // ConfigMode allows for overriding the default behaviors for mapping
79 // schema entries onto configuration constructs.
81 // By default, the Elem field is used to choose whether a particular
82 // schema is represented in configuration as an attribute or as a nested
83 // block; if Elem is a *schema.Resource then it's a block and it's an
84 // attribute otherwise.
86 // If Elem is *schema.Resource then setting ConfigMode to
87 // SchemaConfigModeAttr will force it to be represented in configuration
88 // as an attribute, which means that the Computed flag can be used to
89 // provide default elements when the argument isn't set at all, while still
90 // allowing the user to force zero elements by explicitly assigning an
93 // When Computed is set without Optional, the attribute is not settable
94 // in configuration at all and so SchemaConfigModeAttr is the automatic
95 // behavior, and SchemaConfigModeBlock is not permitted.
96 ConfigMode SchemaConfigMode
98 // If one of these is set, then this item can come from the configuration.
99 // Both cannot be set. If Optional is set, the value is optional. If
100 // Required is set, the value is required.
102 // One of these must be set if the value is not computed. That is:
103 // value either comes from the config, is computed, or is both.
107 // If this is non-nil, the provided function will be used during diff
108 // of this field. If this is nil, a default diff for the type of the
109 // schema will be used.
111 // This allows comparison based on something other than primitive, list
112 // or map equality - for example SSH public keys may be considered
113 // equivalent regardless of trailing whitespace.
114 DiffSuppressFunc SchemaDiffSuppressFunc
116 // If this is non-nil, then this will be a default value that is used
117 // when this item is not set in the configuration.
119 // DefaultFunc can be specified to compute a dynamic default.
120 // Only one of Default or DefaultFunc can be set. If DefaultFunc is
121 // used then its return value should be stable to avoid generating
122 // confusing/perpetual diffs.
124 // Changing either Default or the return value of DefaultFunc can be
125 // a breaking change, especially if the attribute in question has
126 // ForceNew set. If a default needs to change to align with changing
127 // assumptions in an upstream API then it may be necessary to also use
128 // the MigrateState function on the resource to change the state to match,
129 // or have the Read function adjust the state value to align with the
132 // If Required is true above, then Default cannot be set. DefaultFunc
133 // can be set with Required. If the DefaultFunc returns nil, then there
134 // will be no default and the user will be asked to fill it in.
136 // If either of these is set, then the user won't be asked for input
137 // for this key if the default is not nil.
139 DefaultFunc SchemaDefaultFunc
141 // Description is used as the description for docs or asking for user
142 // input. It should be relatively short (a few sentences max) and should
143 // be formatted to fit a CLI.
146 // InputDefault is the default value to use for when inputs are requested.
147 // This differs from Default in that if Default is set, no input is
148 // asked for. If Input is asked, this will be the default value offered.
151 // The fields below relate to diffs.
153 // If Computed is true, then the result of this value is computed
154 // (unless specified by config) on creation.
156 // If ForceNew is true, then a change in this resource necessitates
157 // the creation of a new resource.
159 // StateFunc is a function called to change the value of this before
160 // storing it in the state (and likewise before comparing for diffs).
161 // The use for this is for example with large strings, you may want
162 // to simply store the hash of it.
165 StateFunc SchemaStateFunc
167 // The following fields are only set for a TypeList, TypeSet, or TypeMap.
169 // Elem represents the element type. For a TypeMap, it must be a *Schema
170 // with a Type that is one of the primitives: TypeString, TypeBool,
171 // TypeInt, or TypeFloat. Otherwise it may be either a *Schema or a
172 // *Resource. If it is *Schema, the element type is just a simple value.
173 // If it is *Resource, the element type is a complex structure,
174 // potentially with its own lifecycle.
177 // The following fields are only set for a TypeList or TypeSet.
179 // MaxItems defines a maximum amount of items that can exist within a
180 // TypeSet or TypeList. Specific use cases would be if a TypeSet is being
181 // used to wrap a complex structure, however more than one instance would
182 // cause instability.
184 // MinItems defines a minimum amount of items that can exist within a
185 // TypeSet or TypeList. Specific use cases would be if a TypeSet is being
186 // used to wrap a complex structure, however less than one instance would
187 // cause instability.
189 // If the field Optional is set to true then MinItems is ignored and thus
194 // PromoteSingle originally allowed for a single element to be assigned
195 // where a primitive list was expected, but this no longer works from
196 // Terraform v0.12 onwards (Terraform Core will require a list to be set
197 // regardless of what this is set to) and so only applies to Terraform v0.11
198 // and earlier, and so should be used only to retain this functionality
199 // for those still using v0.11 with a provider that formerly used this.
202 // The following fields are only valid for a TypeSet type.
204 // Set defines a function to determine the unique ID of an item so that
205 // a proper set can be built.
208 // ComputedWhen is a set of queries on the configuration. Whenever any
209 // of these things is changed, it will require a recompute (this requires
210 // that Computed is set to true).
212 // NOTE: This currently does not work.
213 ComputedWhen []string
215 // ConflictsWith is a set of schema keys that conflict with this schema.
216 // This will only check that they're set in the _config_. This will not
217 // raise an error for a malfunctioning resource that sets a conflicting
219 ConflictsWith []string
221 // When Deprecated is set, this attribute is deprecated.
223 // A deprecated field still works, but will probably stop working in near
224 // future. This string is the message shown to the user with instructions on
225 // how to address the deprecation.
228 // When Removed is set, this attribute has been removed from the schema
230 // Removed attributes can be left in the Schema to generate informative error
231 // messages for the user when they show up in resource configurations.
232 // This string is the message shown to the user with instructions on
233 // what do to about the removed attribute.
236 // ValidateFunc allows individual fields to define arbitrary validation
237 // logic. It is yielded the provided config value as an interface{} that is
238 // guaranteed to be of the proper Schema type, and it can yield warnings or
239 // errors based on inspection of that value.
241 // ValidateFunc is honored only when the schema's Type is set to TypeInt,
242 // TypeFloat, TypeString, TypeBool, or TypeMap. It is ignored for all other types.
243 ValidateFunc SchemaValidateFunc
245 // Sensitive ensures that the attribute's value does not get displayed in
246 // logs or regular output. It should be used for passwords or other
247 // secret fields. Future versions of Terraform may encrypt these
252 // SchemaConfigMode is used to influence how a schema item is mapped into a
253 // corresponding configuration construct, using the ConfigMode field of
255 type SchemaConfigMode int
258 SchemaConfigModeAuto SchemaConfigMode = iota
260 SchemaConfigModeBlock
263 // SchemaDiffSuppressFunc is a function which can be used to determine
264 // whether a detected diff on a schema element is "valid" or not, and
265 // suppress it from the plan if necessary.
267 // Return true if the diff should be suppressed, false to retain it.
268 type SchemaDiffSuppressFunc func(k, old, new string, d *ResourceData) bool
270 // SchemaDefaultFunc is a function called to return a default value for
272 type SchemaDefaultFunc func() (interface{}, error)
274 // EnvDefaultFunc is a helper function that returns the value of the
275 // given environment variable, if one exists, or the default value
277 func EnvDefaultFunc(k string, dv interface{}) SchemaDefaultFunc {
278 return func() (interface{}, error) {
279 if v := os.Getenv(k); v != "" {
287 // MultiEnvDefaultFunc is a helper function that returns the value of the first
288 // environment variable in the given list that returns a non-empty value. If
289 // none of the environment variables return a value, the default value is
291 func MultiEnvDefaultFunc(ks []string, dv interface{}) SchemaDefaultFunc {
292 return func() (interface{}, error) {
293 for _, k := range ks {
294 if v := os.Getenv(k); v != "" {
302 // SchemaSetFunc is a function that must return a unique ID for the given
303 // element. This unique ID is used to store the element in a hash.
304 type SchemaSetFunc func(interface{}) int
306 // SchemaStateFunc is a function used to convert some type to a string
307 // to be stored in the state.
308 type SchemaStateFunc func(interface{}) string
310 // SchemaValidateFunc is a function used to validate a single field in the
312 type SchemaValidateFunc func(interface{}, string) ([]string, []error)
314 func (s *Schema) GoString() string {
315 return fmt.Sprintf("*%#v", *s)
318 // Returns a default value for this schema by either reading Default or
319 // evaluating DefaultFunc. If neither of these are defined, returns nil.
320 func (s *Schema) DefaultValue() (interface{}, error) {
321 if s.Default != nil {
322 return s.Default, nil
325 if s.DefaultFunc != nil {
326 defaultValue, err := s.DefaultFunc()
328 return nil, fmt.Errorf("error loading default: %s", err)
330 return defaultValue, nil
336 // Returns a zero value for the schema.
337 func (s *Schema) ZeroValue() interface{} {
338 // If it's a set then we'll do a bit of extra work to provide the
339 // right hashing function in our empty value.
340 if s.Type == TypeSet {
343 // Default set function uses the schema to hash the whole value
345 switch t := elem.(type) {
347 setFunc = HashSchema(t)
349 setFunc = HashResource(t)
351 panic("invalid set element type")
354 return &Set{F: setFunc}
360 func (s *Schema) finalizeDiff(d *terraform.ResourceAttrDiff, customized bool) *terraform.ResourceAttrDiff {
365 if s.Type == TypeBool {
366 normalizeBoolString := func(s string) string {
375 d.Old = normalizeBoolString(d.Old)
376 d.New = normalizeBoolString(d.New)
379 if s.Computed && !d.NewRemoved && d.New == "" {
380 // Computed attribute without a new value set
385 // ForceNew, mark that this field is requiring new under the
386 // following conditions, explained below:
388 // * Old != New - There is a change in value. This field
389 // is therefore causing a new resource.
391 // * NewComputed - This field is being computed, hence a
392 // potential change in value, mark as causing a new resource.
393 d.RequiresNew = d.Old != d.New || d.NewComputed
401 // FIXME: This is where the customized bool from getChange finally
402 // comes into play. It allows the previously incorrect behavior
403 // of an empty string being used as "unset" when the value is
404 // computed. This should be removed once we can properly
405 // represent an unset/nil value from the configuration.
407 if d.Old != "" && d.New == "" {
408 // This is a computed value with an old value set already,
414 if d.New == "" && !d.NewComputed {
415 // Computed attribute without a new value set
421 // Set the Sensitive flag so output is hidden in the UI
428 // InternalMap is used to aid in the transition to the new schema types and
429 // protocol. The name is not meant to convey any usefulness, as this is not to
430 // be used directly by any providers.
431 type InternalMap = schemaMap
433 // schemaMap is a wrapper that adds nice functions on top of schemas.
434 type schemaMap map[string]*Schema
436 func (m schemaMap) panicOnError() bool {
437 if os.Getenv(PanicOnErr) != "" {
443 // Data returns a ResourceData for the given schema, state, and diff.
445 // The diff is optional.
446 func (m schemaMap) Data(
447 s *terraform.InstanceState,
448 d *terraform.InstanceDiff) (*ResourceData, error) {
449 return &ResourceData{
453 panicOnError: m.panicOnError(),
457 // DeepCopy returns a copy of this schemaMap. The copy can be safely modified
458 // without affecting the original.
459 func (m *schemaMap) DeepCopy() schemaMap {
460 copy, err := copystructure.Config{Lock: true}.Copy(m)
464 return *copy.(*schemaMap)
467 // Diff returns the diff for a resource given the schema map,
468 // state, and configuration.
469 func (m schemaMap) Diff(
470 s *terraform.InstanceState,
471 c *terraform.ResourceConfig,
472 customizeDiff CustomizeDiffFunc,
474 handleRequiresNew bool) (*terraform.InstanceDiff, error) {
475 result := new(terraform.InstanceDiff)
476 result.Attributes = make(map[string]*terraform.ResourceAttrDiff)
478 // Make sure to mark if the resource is tainted
480 result.DestroyTainted = s.Tainted
487 panicOnError: m.panicOnError(),
490 for k, schema := range m {
491 err := m.diff(k, schema, result, d, false)
497 // Remove any nil diffs just to keep things clean
498 for k, v := range result.Attributes {
500 delete(result.Attributes, k)
504 // If this is a non-destroy diff, call any custom diff logic that has been
506 if !result.DestroyTainted && customizeDiff != nil {
508 rd := newResourceDiff(mc, c, s, result)
509 if err := customizeDiff(rd, meta); err != nil {
512 for _, k := range rd.UpdatedKeys() {
513 err := m.diff(k, mc[k], result, rd, false)
520 if handleRequiresNew {
521 // If the diff requires a new resource, then we recompute the diff
522 // so we have the complete new resource diff, and preserve the
523 // RequiresNew fields where necessary so the user knows exactly what
525 if result.RequiresNew() {
526 // Create the new diff
527 result2 := new(terraform.InstanceDiff)
528 result2.Attributes = make(map[string]*terraform.ResourceAttrDiff)
530 // Preserve the DestroyTainted flag
531 result2.DestroyTainted = result.DestroyTainted
533 // Reset the data to not contain state. We have to call init()
534 // again in order to reset the FieldReaders.
538 // Perform the diff again
539 for k, schema := range m {
540 err := m.diff(k, schema, result2, d, false)
546 // Re-run customization
547 if !result2.DestroyTainted && customizeDiff != nil {
549 rd := newResourceDiff(mc, c, d.state, result2)
550 if err := customizeDiff(rd, meta); err != nil {
553 for _, k := range rd.UpdatedKeys() {
554 err := m.diff(k, mc[k], result2, rd, false)
561 // Force all the fields to not force a new since we know what we
562 // want to force new.
563 for k, attr := range result2.Attributes {
568 if attr.RequiresNew {
569 attr.RequiresNew = false
573 attr.Old = s.Attributes[k]
577 // Now copy in all the requires new diffs...
578 for k, attr := range result.Attributes {
583 newAttr, ok := result2.Attributes[k]
588 if attr.RequiresNew {
589 newAttr.RequiresNew = true
592 result2.Attributes[k] = newAttr
601 // Go through and detect all of the ComputedWhens now that we've
602 // finished the diff.
606 // If we don't have any diff elements, just return nil
613 // Input implements the terraform.ResourceProvider method by asking
614 // for input for required configuration keys that don't have a value.
615 func (m schemaMap) Input(
616 input terraform.UIInput,
617 c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) {
618 keys := make([]string, 0, len(m))
619 for k, _ := range m {
620 keys = append(keys, k)
624 for _, k := range keys {
627 // Skip things that don't require config, if that is even valid
628 // for a provider schema.
629 // Required XOR Optional must always be true to validate, so we only
630 // need to check one.
635 // Deprecated fields should never prompt
636 if v.Deprecated != "" {
640 // Skip things that have a value of some sort already
641 if _, ok := c.Raw[k]; ok {
645 // Skip if it has a default value
646 defaultValue, err := v.DefaultValue()
648 return nil, fmt.Errorf("%s: error loading default: %s", k, err)
650 if defaultValue != nil {
654 var value interface{}
656 case TypeBool, TypeInt, TypeFloat, TypeSet, TypeList:
659 value, err = m.inputString(input, k, v)
661 panic(fmt.Sprintf("Unknown type for input: %#v", v.Type))
665 return nil, fmt.Errorf(
675 // Validate validates the configuration against this schema mapping.
676 func (m schemaMap) Validate(c *terraform.ResourceConfig) ([]string, []error) {
677 return m.validateObject("", m, c)
680 // InternalValidate validates the format of this schema. This should be called
681 // from a unit test (and not in user-path code) to verify that a schema
682 // is properly built.
683 func (m schemaMap) InternalValidate(topSchemaMap schemaMap) error {
684 return m.internalValidate(topSchemaMap, false)
687 func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) error {
688 if topSchemaMap == nil {
691 for k, v := range m {
692 if v.Type == TypeInvalid {
693 return fmt.Errorf("%s: Type must be specified", k)
696 if v.Optional && v.Required {
697 return fmt.Errorf("%s: Optional or Required must be set, not both", k)
700 if v.Required && v.Computed {
701 return fmt.Errorf("%s: Cannot be both Required and Computed", k)
704 if !v.Required && !v.Optional && !v.Computed {
705 return fmt.Errorf("%s: One of optional, required, or computed must be set", k)
708 computedOnly := v.Computed && !v.Optional
710 switch v.ConfigMode {
711 case SchemaConfigModeBlock:
712 if _, ok := v.Elem.(*Resource); !ok {
713 return fmt.Errorf("%s: ConfigMode of block is allowed only when Elem is *schema.Resource", k)
716 return fmt.Errorf("%s: ConfigMode of block cannot be used in child of schema with ConfigMode of attribute", k)
719 return fmt.Errorf("%s: ConfigMode of block cannot be used for computed schema", k)
721 case SchemaConfigModeAttr:
723 case SchemaConfigModeAuto:
724 // Since "Auto" for Elem: *Resource would create a nested block,
725 // and that's impossible inside an attribute, we require it to be
726 // explicitly overridden as mode "Attr" for clarity.
727 if _, ok := v.Elem.(*Resource); ok {
729 return fmt.Errorf("%s: in *schema.Resource with ConfigMode of attribute, so must also have ConfigMode of attribute", k)
733 return fmt.Errorf("%s: invalid ConfigMode value", k)
736 if v.Computed && v.Default != nil {
737 return fmt.Errorf("%s: Default must be nil if computed", k)
740 if v.Required && v.Default != nil {
741 return fmt.Errorf("%s: Default cannot be set with Required", k)
744 if len(v.ComputedWhen) > 0 && !v.Computed {
745 return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k)
748 if len(v.ConflictsWith) > 0 && v.Required {
749 return fmt.Errorf("%s: ConflictsWith cannot be set with Required", k)
752 if len(v.ConflictsWith) > 0 {
753 for _, key := range v.ConflictsWith {
754 parts := strings.Split(key, ".")
757 for _, part := range parts {
759 if _, err := strconv.Atoi(part); err == nil {
764 if target, ok = sm[part]; !ok {
765 return fmt.Errorf("%s: ConflictsWith references unknown attribute (%s)", k, key)
768 if subResource, ok := target.Elem.(*Resource); ok {
769 sm = schemaMap(subResource.Schema)
773 return fmt.Errorf("%s: ConflictsWith cannot find target attribute (%s), sm: %#v", k, key, sm)
776 return fmt.Errorf("%s: ConflictsWith cannot contain Required attribute (%s)", k, key)
779 if len(target.ComputedWhen) > 0 {
780 return fmt.Errorf("%s: ConflictsWith cannot contain Computed(When) attribute (%s)", k, key)
785 if v.Type == TypeList || v.Type == TypeSet {
787 return fmt.Errorf("%s: Elem must be set for lists", k)
790 if v.Default != nil {
791 return fmt.Errorf("%s: Default is not valid for lists or sets", k)
794 if v.Type != TypeSet && v.Set != nil {
795 return fmt.Errorf("%s: Set can only be set for TypeSet", k)
798 switch t := v.Elem.(type) {
800 attrsOnly := attrsOnly || v.ConfigMode == SchemaConfigModeAttr
802 if err := schemaMap(t.Schema).internalValidate(topSchemaMap, attrsOnly); err != nil {
806 bad := t.Computed || t.Optional || t.Required
809 "%s: Elem must have only Type set", k)
813 if v.MaxItems > 0 || v.MinItems > 0 {
814 return fmt.Errorf("%s: MaxItems and MinItems are only supported on lists or sets", k)
818 // Computed-only field
819 if v.Computed && !v.Optional {
820 if v.ValidateFunc != nil {
821 return fmt.Errorf("%s: ValidateFunc is for validating user input, "+
822 "there's nothing to validate on computed-only field", k)
824 if v.DiffSuppressFunc != nil {
825 return fmt.Errorf("%s: DiffSuppressFunc is for suppressing differences"+
826 " between config and state representation. "+
827 "There is no config for computed-only field, nothing to compare.", k)
831 if v.ValidateFunc != nil {
833 case TypeList, TypeSet:
834 return fmt.Errorf("%s: ValidateFunc is not yet supported on lists or sets.", k)
838 if v.Deprecated == "" && v.Removed == "" {
839 if !isValidFieldName(k) {
840 return fmt.Errorf("%s: Field name may only contain lowercase alphanumeric characters & underscores.", k)
848 func isValidFieldName(name string) bool {
849 re := regexp.MustCompile("^[a-z0-9_]+$")
850 return re.MatchString(name)
853 // resourceDiffer is an interface that is used by the private diff functions.
854 // This helps facilitate diff logic for both ResourceData and ResoureDiff with
855 // minimal divergence in code.
856 type resourceDiffer interface {
857 diffChange(string) (interface{}, interface{}, bool, bool, bool)
858 Get(string) interface{}
859 GetChange(string) (interface{}, interface{})
860 GetOk(string) (interface{}, bool)
861 HasChange(string) bool
865 func (m schemaMap) diff(
868 diff *terraform.InstanceDiff,
872 unsupressedDiff := new(terraform.InstanceDiff)
873 unsupressedDiff.Attributes = make(map[string]*terraform.ResourceAttrDiff)
877 case TypeBool, TypeInt, TypeFloat, TypeString:
878 err = m.diffString(k, schema, unsupressedDiff, d, all)
880 err = m.diffList(k, schema, unsupressedDiff, d, all)
882 err = m.diffMap(k, schema, unsupressedDiff, d, all)
884 err = m.diffSet(k, schema, unsupressedDiff, d, all)
886 err = fmt.Errorf("%s: unknown type %#v", k, schema.Type)
889 for attrK, attrV := range unsupressedDiff.Attributes {
890 switch rd := d.(type) {
892 if schema.DiffSuppressFunc != nil && attrV != nil &&
893 schema.DiffSuppressFunc(attrK, attrV.Old, attrV.New, rd) {
894 // If this attr diff is suppressed, we may still need it in the
895 // overall diff if it's contained within a set. Rather than
896 // dropping the diff, make it a NOOP.
901 attrV = &terraform.ResourceAttrDiff{
907 diff.Attributes[attrK] = attrV
913 func (m schemaMap) diffList(
916 diff *terraform.InstanceDiff,
919 o, n, _, computedList, customized := d.diffChange(k)
925 // If we have an old value and no new value is set or will be
926 // computed once all variables can be interpolated and we're
927 // computed, then nothing has changed.
928 if o != nil && n == nil && !computedList && schema.Computed {
938 if s, ok := o.(*Set); ok {
941 if s, ok := n.(*Set); ok {
944 os := o.([]interface{})
945 vs := n.([]interface{})
947 // If the new value was set, and the two are equal, then we're done.
948 // We have to do this check here because sets might be NOT
949 // reflect.DeepEqual so we need to wait until we get the []interface{}
950 if !all && nSet && reflect.DeepEqual(os, vs) {
957 oldStr := strconv.FormatInt(int64(oldLen), 10)
959 // If the whole list is computed, then say that the # is computed
961 diff.Attributes[k+".#"] = &terraform.ResourceAttrDiff{
964 RequiresNew: schema.ForceNew,
969 // If the counts are not the same, then record that diff
970 changed := oldLen != newLen
971 computed := oldLen == 0 && newLen == 0 && schema.Computed
972 if changed || computed || all {
973 countSchema := &Schema{
975 Computed: schema.Computed,
976 ForceNew: schema.ForceNew,
981 newStr = strconv.FormatInt(int64(newLen), 10)
986 diff.Attributes[k+".#"] = countSchema.finalizeDiff(
987 &terraform.ResourceAttrDiff{
995 // Figure out the maximum
1001 switch t := schema.Elem.(type) {
1003 // This is a complex resource
1004 for i := 0; i < maxLen; i++ {
1005 for k2, schema := range t.Schema {
1006 subK := fmt.Sprintf("%s.%d.%s", k, i, k2)
1007 err := m.diff(subK, schema, diff, d, all)
1014 // Copy the schema so that we can set Computed/ForceNew from
1015 // the parent schema (the TypeList).
1017 t2.ForceNew = schema.ForceNew
1019 // This is just a primitive element, so go through each and
1021 for i := 0; i < maxLen; i++ {
1022 subK := fmt.Sprintf("%s.%d", k, i)
1023 err := m.diff(subK, &t2, diff, d, all)
1029 return fmt.Errorf("%s: unknown element type (internal)", k)
1035 func (m schemaMap) diffMap(
1038 diff *terraform.InstanceDiff,
1043 // First get all the values from the state
1044 var stateMap, configMap map[string]string
1045 o, n, _, nComputed, customized := d.diffChange(k)
1046 if err := mapstructure.WeakDecode(o, &stateMap); err != nil {
1047 return fmt.Errorf("%s: %s", k, err)
1049 if err := mapstructure.WeakDecode(n, &configMap); err != nil {
1050 return fmt.Errorf("%s: %s", k, err)
1053 // Keep track of whether the state _exists_ at all prior to clearing it
1054 stateExists := o != nil
1056 // Delete any count values, since we don't use those
1057 delete(configMap, "%")
1058 delete(stateMap, "%")
1060 // Check if the number of elements has changed.
1061 oldLen, newLen := len(stateMap), len(configMap)
1062 changed := oldLen != newLen
1063 if oldLen != 0 && newLen == 0 && schema.Computed {
1067 // It is computed if we have no old value, no new value, the schema
1068 // says it is computed, and it didn't exist in the state before. The
1069 // last point means: if it existed in the state, even empty, then it
1070 // has already been computed.
1071 computed := oldLen == 0 && newLen == 0 && schema.Computed && !stateExists
1073 // If the count has changed or we're computed, then add a diff for the
1074 // count. "nComputed" means that the new value _contains_ a value that
1075 // is computed. We don't do granular diffs for this yet, so we mark the
1076 // whole map as computed.
1077 if changed || computed || nComputed {
1078 countSchema := &Schema{
1080 Computed: schema.Computed || nComputed,
1081 ForceNew: schema.ForceNew,
1084 oldStr := strconv.FormatInt(int64(oldLen), 10)
1086 if !computed && !nComputed {
1087 newStr = strconv.FormatInt(int64(newLen), 10)
1092 diff.Attributes[k+".%"] = countSchema.finalizeDiff(
1093 &terraform.ResourceAttrDiff{
1101 // If the new map is nil and we're computed, then ignore it.
1102 if n == nil && schema.Computed {
1106 // Now we compare, preferring values from the config map
1107 for k, v := range configMap {
1108 old, ok := stateMap[k]
1111 if old == v && ok && !all {
1115 diff.Attributes[prefix+k] = schema.finalizeDiff(
1116 &terraform.ResourceAttrDiff{
1123 for k, v := range stateMap {
1124 diff.Attributes[prefix+k] = schema.finalizeDiff(
1125 &terraform.ResourceAttrDiff{
1136 func (m schemaMap) diffSet(
1139 diff *terraform.InstanceDiff,
1143 o, n, _, computedSet, customized := d.diffChange(k)
1149 // If we have an old value and no new value is set or will be
1150 // computed once all variables can be interpolated and we're
1151 // computed, then nothing has changed.
1152 if o != nil && n == nil && !computedSet && schema.Computed {
1157 o = schema.ZeroValue().(*Set)
1160 n = schema.ZeroValue().(*Set)
1165 // If the new value was set, compare the listCode's to determine if
1166 // the two are equal. Comparing listCode's instead of the actual values
1167 // is needed because there could be computed values in the set which
1168 // would result in false positives while comparing.
1169 if !all && nSet && reflect.DeepEqual(os.listCode(), ns.listCode()) {
1176 oldStr := strconv.Itoa(oldLen)
1177 newStr := strconv.Itoa(newLen)
1179 // Build a schema for our count
1180 countSchema := &Schema{
1182 Computed: schema.Computed,
1183 ForceNew: schema.ForceNew,
1186 // If the set computed then say that the # is computed
1187 if computedSet || schema.Computed && !nSet {
1188 // If # already exists, equals 0 and no new set is supplied, there
1189 // is nothing to record in the diff
1190 count, ok := d.GetOk(k + ".#")
1191 if ok && count.(int) == 0 && !nSet && !computedSet {
1195 // Set the count but make sure that if # does not exist, we don't
1196 // use the zeroed value
1197 countStr := strconv.Itoa(count.(int))
1202 diff.Attributes[k+".#"] = countSchema.finalizeDiff(
1203 &terraform.ResourceAttrDiff{
1212 // If the counts are not the same, then record that diff
1213 changed := oldLen != newLen
1215 diff.Attributes[k+".#"] = countSchema.finalizeDiff(
1216 &terraform.ResourceAttrDiff{
1224 // Build the list of codes that will make up our set. This is the
1225 // removed codes as well as all the codes in the new codes.
1226 codes := make([][]string, 2)
1227 codes[0] = os.Difference(ns).listCode()
1228 codes[1] = ns.listCode()
1229 for _, list := range codes {
1230 for _, code := range list {
1231 switch t := schema.Elem.(type) {
1233 // This is a complex resource
1234 for k2, schema := range t.Schema {
1235 subK := fmt.Sprintf("%s.%s.%s", k, code, k2)
1236 err := m.diff(subK, schema, diff, d, true)
1242 // Copy the schema so that we can set Computed/ForceNew from
1243 // the parent schema (the TypeSet).
1245 t2.ForceNew = schema.ForceNew
1247 // This is just a primitive element, so go through each and
1249 subK := fmt.Sprintf("%s.%s", k, code)
1250 err := m.diff(subK, &t2, diff, d, true)
1255 return fmt.Errorf("%s: unknown element type (internal)", k)
1263 func (m schemaMap) diffString(
1266 diff *terraform.InstanceDiff,
1269 var originalN interface{}
1271 o, n, _, computed, customized := d.diffChange(k)
1272 if schema.StateFunc != nil && n != nil {
1274 n = schema.StateFunc(n)
1277 if nraw == nil && o != nil {
1278 nraw = schema.Type.Zero()
1280 if err := mapstructure.WeakDecode(o, &os); err != nil {
1281 return fmt.Errorf("%s: %s", k, err)
1283 if err := mapstructure.WeakDecode(nraw, &ns); err != nil {
1284 return fmt.Errorf("%s: %s", k, err)
1287 if os == ns && !all && !computed {
1288 // They're the same value. If there old value is not blank or we
1289 // have an ID, then return right away since we're already setup.
1290 if os != "" || d.Id() != "" {
1294 // Otherwise, only continue if we're computed
1295 if !schema.Computed {
1301 if o != nil && n == nil && !computed {
1304 if removed && schema.Computed {
1308 diff.Attributes[k] = schema.finalizeDiff(
1309 &terraform.ResourceAttrDiff{
1312 NewExtra: originalN,
1313 NewRemoved: removed,
1314 NewComputed: computed,
1322 func (m schemaMap) inputString(
1323 input terraform.UIInput,
1325 schema *Schema) (interface{}, error) {
1326 result, err := input.Input(context.Background(), &terraform.InputOpts{
1329 Description: schema.Description,
1330 Default: schema.InputDefault,
1336 func (m schemaMap) validate(
1339 c *terraform.ResourceConfig) ([]string, []error) {
1341 if !ok && schema.DefaultFunc != nil {
1342 // We have a dynamic default. Check if we have a value.
1344 raw, err = schema.DefaultFunc()
1346 return nil, []error{fmt.Errorf(
1347 "%q, error loading default: %s", k, err)}
1350 // We're okay as long as we had a value set
1354 if schema.Required {
1355 return nil, []error{fmt.Errorf(
1356 "%q: required field is not set", k)}
1362 if !schema.Required && !schema.Optional {
1363 // This is a computed-only field
1364 return nil, []error{fmt.Errorf(
1365 "%q: this field cannot be set", k)}
1368 if raw == config.UnknownVariableValue {
1369 // If the value is unknown then we can't validate it yet.
1370 // In particular, this avoids spurious type errors where downstream
1371 // validation code sees UnknownVariableValue as being just a string.
1375 err := m.validateConflictingAttributes(k, schema, c)
1377 return nil, []error{err}
1380 return m.validateType(k, raw, schema, c)
1383 func (m schemaMap) validateConflictingAttributes(
1386 c *terraform.ResourceConfig) error {
1388 if len(schema.ConflictsWith) == 0 {
1392 for _, conflictingKey := range schema.ConflictsWith {
1393 if raw, ok := c.Get(conflictingKey); ok {
1394 if raw == config.UnknownVariableValue {
1395 // An unknown value might become unset (null) once known, so
1396 // we must defer validation until it's known.
1400 "%q: conflicts with %s", k, conflictingKey)
1407 func (m schemaMap) validateList(
1411 c *terraform.ResourceConfig) ([]string, []error) {
1412 // first check if the list is wholly unknown
1413 if s, ok := raw.(string); ok {
1414 if s == config.UnknownVariableValue {
1419 // We use reflection to verify the slice because you can't
1420 // case to []interface{} unless the slice is exactly that type.
1421 rawV := reflect.ValueOf(raw)
1423 // If we support promotion and the raw value isn't a slice, wrap
1424 // it in []interface{} and check again.
1425 if schema.PromoteSingle && rawV.Kind() != reflect.Slice {
1426 raw = []interface{}{raw}
1427 rawV = reflect.ValueOf(raw)
1430 if rawV.Kind() != reflect.Slice {
1431 return nil, []error{fmt.Errorf(
1432 "%s: should be a list", k)}
1436 if schema.MaxItems > 0 && rawV.Len() > schema.MaxItems {
1437 return nil, []error{fmt.Errorf(
1438 "%s: attribute supports %d item maximum, config has %d declared", k, schema.MaxItems, rawV.Len())}
1441 if schema.MinItems > 0 && rawV.Len() < schema.MinItems {
1442 return nil, []error{fmt.Errorf(
1443 "%s: attribute supports %d item as a minimum, config has %d declared", k, schema.MinItems, rawV.Len())}
1446 // Now build the []interface{}
1447 raws := make([]interface{}, rawV.Len())
1448 for i, _ := range raws {
1449 raws[i] = rawV.Index(i).Interface()
1454 for i, raw := range raws {
1455 key := fmt.Sprintf("%s.%d", k, i)
1457 // Reify the key value from the ResourceConfig.
1458 // If the list was computed we have all raw values, but some of these
1459 // may be known in the config, and aren't individually marked as Computed.
1460 if r, ok := c.Get(key); ok {
1466 switch t := schema.Elem.(type) {
1468 // This is a sub-resource
1469 ws2, es2 = m.validateObject(key, t.Schema, c)
1471 ws2, es2 = m.validateType(key, raw, t, c)
1475 ws = append(ws, ws2...)
1478 es = append(es, es2...)
1485 func (m schemaMap) validateMap(
1489 c *terraform.ResourceConfig) ([]string, []error) {
1490 // first check if the list is wholly unknown
1491 if s, ok := raw.(string); ok {
1492 if s == config.UnknownVariableValue {
1497 // We use reflection to verify the slice because you can't
1498 // case to []interface{} unless the slice is exactly that type.
1499 rawV := reflect.ValueOf(raw)
1500 switch rawV.Kind() {
1501 case reflect.String:
1502 // If raw and reified are equal, this is a string and should
1504 reified, reifiedOk := c.Get(k)
1505 if reifiedOk && raw == reified && !c.IsComputed(k) {
1506 return nil, []error{fmt.Errorf("%s: should be a map", k)}
1508 // Otherwise it's likely raw is an interpolation.
1513 return nil, []error{fmt.Errorf("%s: should be a map", k)}
1516 // If it is not a slice, validate directly
1517 if rawV.Kind() != reflect.Slice {
1518 mapIface := rawV.Interface()
1519 if _, errs := validateMapValues(k, mapIface.(map[string]interface{}), schema); len(errs) > 0 {
1522 if schema.ValidateFunc != nil {
1523 return schema.ValidateFunc(mapIface, k)
1528 // It is a slice, verify that all the elements are maps
1529 raws := make([]interface{}, rawV.Len())
1530 for i, _ := range raws {
1531 raws[i] = rawV.Index(i).Interface()
1534 for _, raw := range raws {
1535 v := reflect.ValueOf(raw)
1536 if v.Kind() != reflect.Map {
1537 return nil, []error{fmt.Errorf(
1538 "%s: should be a map", k)}
1540 mapIface := v.Interface()
1541 if _, errs := validateMapValues(k, mapIface.(map[string]interface{}), schema); len(errs) > 0 {
1546 if schema.ValidateFunc != nil {
1547 validatableMap := make(map[string]interface{})
1548 for _, raw := range raws {
1549 for k, v := range raw.(map[string]interface{}) {
1550 validatableMap[k] = v
1554 return schema.ValidateFunc(validatableMap, k)
1560 func validateMapValues(k string, m map[string]interface{}, schema *Schema) ([]string, []error) {
1561 for key, raw := range m {
1562 valueType, err := getValueType(k, schema)
1564 return nil, []error{err}
1570 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1571 return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
1575 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1576 return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
1580 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1581 return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
1585 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1586 return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
1589 panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
1595 func getValueType(k string, schema *Schema) (ValueType, error) {
1596 if schema.Elem == nil {
1597 return TypeString, nil
1599 if vt, ok := schema.Elem.(ValueType); ok {
1603 // If a Schema is provided to a Map, we use the Type of that schema
1604 // as the type for each element in the Map.
1605 if s, ok := schema.Elem.(*Schema); ok {
1609 if _, ok := schema.Elem.(*Resource); ok {
1610 // TODO: We don't actually support this (yet)
1611 // but silently pass the validation, until we decide
1612 // how to handle nested structures in maps
1613 return TypeString, nil
1615 return 0, fmt.Errorf("%s: unexpected map value type: %#v", k, schema.Elem)
1618 func (m schemaMap) validateObject(
1620 schema map[string]*Schema,
1621 c *terraform.ResourceConfig) ([]string, []error) {
1623 if _, ok := raw.(map[string]interface{}); !ok && !c.IsComputed(k) {
1624 return nil, []error{fmt.Errorf(
1625 "%s: expected object, got %s",
1626 k, reflect.ValueOf(raw).Kind())}
1631 for subK, s := range schema {
1634 key = fmt.Sprintf("%s.%s", k, subK)
1637 ws2, es2 := m.validate(key, s, c)
1639 ws = append(ws, ws2...)
1642 es = append(es, es2...)
1646 // Detect any extra/unknown keys and report those as errors.
1647 if m, ok := raw.(map[string]interface{}); ok {
1648 for subk, _ := range m {
1649 if _, ok := schema[subk]; !ok {
1650 if subk == TimeoutsConfigKey {
1653 es = append(es, fmt.Errorf(
1654 "%s: invalid or unknown key: %s", k, subk))
1662 func (m schemaMap) validatePrimitive(
1666 c *terraform.ResourceConfig) ([]string, []error) {
1667 // Catch if the user gave a complex type where a primitive was
1668 // expected, so we can return a friendly error message that
1669 // doesn't contain Go type system terminology.
1670 switch reflect.ValueOf(raw).Type().Kind() {
1672 return nil, []error{
1673 fmt.Errorf("%s must be a single value, not a list", k),
1676 return nil, []error{
1677 fmt.Errorf("%s must be a single value, not a map", k),
1682 if c.IsComputed(k) {
1683 // If the key is being computed, then it is not an error as
1684 // long as it's not a slice or map.
1688 var decoded interface{}
1689 switch schema.Type {
1691 // Verify that we can parse this as the correct type
1693 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1694 return nil, []error{fmt.Errorf("%s: %s", k, err)}
1700 // We need to verify the type precisely, because WeakDecode will
1701 // decode a float as an integer.
1703 // the config shims only use int for integral number values
1704 if v, ok := raw.(int); ok {
1707 return nil, []error{fmt.Errorf("%s: must be a whole number, got %v", k, raw)}
1710 // Verify that we can parse this as an int
1712 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1713 return nil, []error{fmt.Errorf("%s: %s", k, err)}
1718 // Verify that we can parse this as an int
1720 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1721 return nil, []error{fmt.Errorf("%s: %s", k, err)}
1725 // Verify that we can parse this as a string
1727 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1728 return nil, []error{fmt.Errorf("%s: %s", k, err)}
1732 panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
1735 if schema.ValidateFunc != nil {
1736 return schema.ValidateFunc(decoded, k)
1742 func (m schemaMap) validateType(
1746 c *terraform.ResourceConfig) ([]string, []error) {
1749 switch schema.Type {
1750 case TypeSet, TypeList:
1751 ws, es = m.validateList(k, raw, schema, c)
1753 ws, es = m.validateMap(k, raw, schema, c)
1755 ws, es = m.validatePrimitive(k, raw, schema, c)
1758 if schema.Deprecated != "" {
1759 ws = append(ws, fmt.Sprintf(
1760 "%q: [DEPRECATED] %s", k, schema.Deprecated))
1763 if schema.Removed != "" {
1764 es = append(es, fmt.Errorf(
1765 "%q: [REMOVED] %s", k, schema.Removed))
1771 // Zero returns the zero value for a type.
1772 func (t ValueType) Zero() interface{} {
1785 return []interface{}{}
1787 return map[string]interface{}{}
1791 return map[string]interface{}{}
1793 panic(fmt.Sprintf("unknown type %s", t))