github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/schema/schema.go (about)

     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.
     4  //
     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.
    10  //
    11  // A good starting point is to view the Provider structure.
    12  package schema
    13  
    14  import (
    15  	"context"
    16  	"fmt"
    17  	"os"
    18  	"reflect"
    19  	"regexp"
    20  	"sort"
    21  	"strconv"
    22  	"strings"
    23  	"sync"
    24  
    25  	"github.com/hashicorp/terraform-plugin-sdk/internal/configs/hcl2shim"
    26  	"github.com/hashicorp/terraform-plugin-sdk/terraform"
    27  	"github.com/mitchellh/copystructure"
    28  	"github.com/mitchellh/mapstructure"
    29  )
    30  
    31  // Name of ENV variable which (if not empty) prefers panic over error
    32  const PanicOnErr = "TF_SCHEMA_PANIC_ON_ERROR"
    33  
    34  // type used for schema package context keys
    35  type contextKey string
    36  
    37  var (
    38  	protoVersionMu sync.Mutex
    39  	protoVersion5  = false
    40  )
    41  
    42  func isProto5() bool {
    43  	protoVersionMu.Lock()
    44  	defer protoVersionMu.Unlock()
    45  	return protoVersion5
    46  
    47  }
    48  
    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
    51  // provider.
    52  func SetProto5() {
    53  	protoVersionMu.Lock()
    54  	defer protoVersionMu.Unlock()
    55  	protoVersion5 = true
    56  }
    57  
    58  // Schema is used to describe the structure of a value.
    59  //
    60  // Read the documentation of the struct elements for important details.
    61  type Schema struct {
    62  	// Type is the type of the value and must be one of the ValueType values.
    63  	//
    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:
    67  	//
    68  	//   TypeBool - bool
    69  	//   TypeInt - int
    70  	//   TypeFloat - float64
    71  	//   TypeString - string
    72  	//   TypeList - []interface{}
    73  	//   TypeMap - map[string]interface{}
    74  	//   TypeSet - *schema.Set
    75  	//
    76  	Type ValueType
    77  
    78  	// ConfigMode allows for overriding the default behaviors for mapping
    79  	// schema entries onto configuration constructs.
    80  	//
    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.
    85  	//
    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
    91  	// empty list.
    92  	//
    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
    97  
    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.
   101  	//
   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.
   104  	Optional bool
   105  	Required bool
   106  
   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.
   110  	//
   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
   115  
   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.
   118  	//
   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.
   123  	//
   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
   130  	// new default.
   131  	//
   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.
   135  	//
   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.
   138  	Default     interface{}
   139  	DefaultFunc SchemaDefaultFunc
   140  
   141  	// Description is used as the description for docs, the language server and
   142  	// other user facing usage. It can be plain-text or markdown depending on the
   143  	// global DescriptionKind setting.
   144  	Description string
   145  
   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.
   149  	InputDefault string
   150  
   151  	// The fields below relate to diffs.
   152  	//
   153  	// If Computed is true, then the result of this value is computed
   154  	// (unless specified by config) on creation.
   155  	//
   156  	// If ForceNew is true, then a change in this resource necessitates
   157  	// the creation of a new resource.
   158  	//
   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.
   163  	Computed  bool
   164  	ForceNew  bool
   165  	StateFunc SchemaStateFunc
   166  
   167  	// The following fields are only set for a TypeList, TypeSet, or TypeMap.
   168  	//
   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 managed via its own CRUD actions on the API.
   175  	Elem interface{}
   176  
   177  	// The following fields are only set for a TypeList or TypeSet.
   178  	//
   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.
   183  	//
   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.
   188  	//
   189  	// If the field Optional is set to true then MinItems is ignored and thus
   190  	// effectively zero.
   191  	MaxItems int
   192  	MinItems int
   193  
   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.
   200  	PromoteSingle bool
   201  
   202  	// The following fields are only valid for a TypeSet type.
   203  	//
   204  	// Set defines a function to determine the unique ID of an item so that
   205  	// a proper set can be built.
   206  	Set SchemaSetFunc
   207  
   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).
   211  	//
   212  	// NOTE: This currently does not work.
   213  	ComputedWhen []string
   214  
   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
   218  	// key.
   219  	//
   220  	// ExactlyOneOf is a set of schema keys that, when set, only one of the
   221  	// keys in that list can be specified. It will error if none are
   222  	// specified as well.
   223  	//
   224  	// AtLeastOneOf is a set of schema keys that, when set, at least one of
   225  	// the keys in that list must be specified.
   226  	//
   227  	// RequiredWith is a set of schema keys that must be set simultaneously.
   228  	ConflictsWith []string
   229  	ExactlyOneOf  []string
   230  	AtLeastOneOf  []string
   231  	RequiredWith  []string
   232  
   233  	// When Deprecated is set, this attribute is deprecated.
   234  	//
   235  	// A deprecated field still works, but will probably stop working in near
   236  	// future. This string is the message shown to the user with instructions on
   237  	// how to address the deprecation.
   238  	Deprecated string
   239  
   240  	// When Removed is set, this attribute has been removed from the schema
   241  	//
   242  	// Deprecated: This field will be removed in version 2 without replacement
   243  	// as the functionality is not necessary.
   244  	//
   245  	// Removed attributes can be left in the Schema to generate informative error
   246  	// messages for the user when they show up in resource configurations.
   247  	// This string is the message shown to the user with instructions on
   248  	// what do to about the removed attribute.
   249  	Removed string
   250  
   251  	// ValidateFunc allows individual fields to define arbitrary validation
   252  	// logic. It is yielded the provided config value as an interface{} that is
   253  	// guaranteed to be of the proper Schema type, and it can yield warnings or
   254  	// errors based on inspection of that value.
   255  	//
   256  	// ValidateFunc is honored only when the schema's Type is set to TypeInt,
   257  	// TypeFloat, TypeString, TypeBool, or TypeMap. It is ignored for all other types.
   258  	ValidateFunc SchemaValidateFunc
   259  
   260  	// Sensitive ensures that the attribute's value does not get displayed in
   261  	// logs or regular output. It should be used for passwords or other
   262  	// secret fields. Future versions of Terraform may encrypt these
   263  	// values.
   264  	Sensitive bool
   265  }
   266  
   267  // SchemaConfigMode is used to influence how a schema item is mapped into a
   268  // corresponding configuration construct, using the ConfigMode field of
   269  // Schema.
   270  type SchemaConfigMode int
   271  
   272  const (
   273  	SchemaConfigModeAuto SchemaConfigMode = iota
   274  	SchemaConfigModeAttr
   275  	SchemaConfigModeBlock
   276  )
   277  
   278  // SchemaDiffSuppressFunc is a function which can be used to determine
   279  // whether a detected diff on a schema element is "valid" or not, and
   280  // suppress it from the plan if necessary.
   281  //
   282  // Return true if the diff should be suppressed, false to retain it.
   283  type SchemaDiffSuppressFunc func(k, old, new string, d *ResourceData) bool
   284  
   285  // SchemaDefaultFunc is a function called to return a default value for
   286  // a field.
   287  type SchemaDefaultFunc func() (interface{}, error)
   288  
   289  // EnvDefaultFunc is a helper function that returns the value of the
   290  // given environment variable, if one exists, or the default value
   291  // otherwise.
   292  func EnvDefaultFunc(k string, dv interface{}) SchemaDefaultFunc {
   293  	return func() (interface{}, error) {
   294  		if v := os.Getenv(k); v != "" {
   295  			return v, nil
   296  		}
   297  
   298  		return dv, nil
   299  	}
   300  }
   301  
   302  // MultiEnvDefaultFunc is a helper function that returns the value of the first
   303  // environment variable in the given list that returns a non-empty value. If
   304  // none of the environment variables return a value, the default value is
   305  // returned.
   306  func MultiEnvDefaultFunc(ks []string, dv interface{}) SchemaDefaultFunc {
   307  	return func() (interface{}, error) {
   308  		for _, k := range ks {
   309  			if v := os.Getenv(k); v != "" {
   310  				return v, nil
   311  			}
   312  		}
   313  		return dv, nil
   314  	}
   315  }
   316  
   317  // SchemaSetFunc is a function that must return a unique ID for the given
   318  // element. This unique ID is used to store the element in a hash.
   319  type SchemaSetFunc func(interface{}) int
   320  
   321  // SchemaStateFunc is a function used to convert some type to a string
   322  // to be stored in the state.
   323  type SchemaStateFunc func(interface{}) string
   324  
   325  // SchemaValidateFunc is a function used to validate a single field in the
   326  // schema.
   327  type SchemaValidateFunc func(interface{}, string) ([]string, []error)
   328  
   329  func (s *Schema) GoString() string {
   330  	return fmt.Sprintf("*%#v", *s)
   331  }
   332  
   333  // Returns a default value for this schema by either reading Default or
   334  // evaluating DefaultFunc. If neither of these are defined, returns nil.
   335  func (s *Schema) DefaultValue() (interface{}, error) {
   336  	if s.Default != nil {
   337  		return s.Default, nil
   338  	}
   339  
   340  	if s.DefaultFunc != nil {
   341  		defaultValue, err := s.DefaultFunc()
   342  		if err != nil {
   343  			return nil, fmt.Errorf("error loading default: %s", err)
   344  		}
   345  		return defaultValue, nil
   346  	}
   347  
   348  	return nil, nil
   349  }
   350  
   351  // Returns a zero value for the schema.
   352  func (s *Schema) ZeroValue() interface{} {
   353  	// If it's a set then we'll do a bit of extra work to provide the
   354  	// right hashing function in our empty value.
   355  	if s.Type == TypeSet {
   356  		setFunc := s.Set
   357  		if setFunc == nil {
   358  			// Default set function uses the schema to hash the whole value
   359  			elem := s.Elem
   360  			switch t := elem.(type) {
   361  			case *Schema:
   362  				setFunc = HashSchema(t)
   363  			case *Resource:
   364  				setFunc = HashResource(t)
   365  			default:
   366  				panic("invalid set element type")
   367  			}
   368  		}
   369  		return &Set{F: setFunc}
   370  	} else {
   371  		return s.Type.Zero()
   372  	}
   373  }
   374  
   375  func (s *Schema) finalizeDiff(d *terraform.ResourceAttrDiff, customized bool) *terraform.ResourceAttrDiff {
   376  	if d == nil {
   377  		return d
   378  	}
   379  
   380  	if s.Type == TypeBool {
   381  		normalizeBoolString := func(s string) string {
   382  			switch s {
   383  			case "0":
   384  				return "false"
   385  			case "1":
   386  				return "true"
   387  			}
   388  			return s
   389  		}
   390  		d.Old = normalizeBoolString(d.Old)
   391  		d.New = normalizeBoolString(d.New)
   392  	}
   393  
   394  	if s.Computed && !d.NewRemoved && d.New == "" {
   395  		// Computed attribute without a new value set
   396  		d.NewComputed = true
   397  	}
   398  
   399  	if s.ForceNew {
   400  		// ForceNew, mark that this field is requiring new under the
   401  		// following conditions, explained below:
   402  		//
   403  		//   * Old != New - There is a change in value. This field
   404  		//       is therefore causing a new resource.
   405  		//
   406  		//   * NewComputed - This field is being computed, hence a
   407  		//       potential change in value, mark as causing a new resource.
   408  		d.RequiresNew = d.Old != d.New || d.NewComputed
   409  	}
   410  
   411  	if d.NewRemoved {
   412  		return d
   413  	}
   414  
   415  	if s.Computed {
   416  		// FIXME: This is where the customized bool from getChange finally
   417  		//        comes into play.  It allows the previously incorrect behavior
   418  		//        of an empty string being used as "unset" when the value is
   419  		//        computed. This should be removed once we can properly
   420  		//        represent an unset/nil value from the configuration.
   421  		if !customized {
   422  			if d.Old != "" && d.New == "" {
   423  				// This is a computed value with an old value set already,
   424  				// just let it go.
   425  				return nil
   426  			}
   427  		}
   428  
   429  		if d.New == "" && !d.NewComputed {
   430  			// Computed attribute without a new value set
   431  			d.NewComputed = true
   432  		}
   433  	}
   434  
   435  	if s.Sensitive {
   436  		// Set the Sensitive flag so output is hidden in the UI
   437  		d.Sensitive = true
   438  	}
   439  
   440  	return d
   441  }
   442  
   443  // InternalMap is used to aid in the transition to the new schema types and
   444  // protocol. The name is not meant to convey any usefulness, as this is not to
   445  // be used directly by any providers.
   446  type InternalMap = schemaMap
   447  
   448  // schemaMap is a wrapper that adds nice functions on top of schemas.
   449  type schemaMap map[string]*Schema
   450  
   451  func (m schemaMap) panicOnError() bool {
   452  	if os.Getenv(PanicOnErr) != "" {
   453  		return true
   454  	}
   455  	return false
   456  }
   457  
   458  // Data returns a ResourceData for the given schema, state, and diff.
   459  //
   460  // The diff is optional.
   461  func (m schemaMap) Data(
   462  	s *terraform.InstanceState,
   463  	d *terraform.InstanceDiff) (*ResourceData, error) {
   464  	return &ResourceData{
   465  		schema:       m,
   466  		state:        s,
   467  		diff:         d,
   468  		panicOnError: m.panicOnError(),
   469  	}, nil
   470  }
   471  
   472  // DeepCopy returns a copy of this schemaMap. The copy can be safely modified
   473  // without affecting the original.
   474  func (m *schemaMap) DeepCopy() schemaMap {
   475  	copy, err := copystructure.Config{Lock: true}.Copy(m)
   476  	if err != nil {
   477  		panic(err)
   478  	}
   479  	return *copy.(*schemaMap)
   480  }
   481  
   482  // Diff returns the diff for a resource given the schema map,
   483  // state, and configuration.
   484  func (m schemaMap) Diff(
   485  	s *terraform.InstanceState,
   486  	c *terraform.ResourceConfig,
   487  	customizeDiff CustomizeDiffFunc,
   488  	meta interface{},
   489  	handleRequiresNew bool) (*terraform.InstanceDiff, error) {
   490  	result := new(terraform.InstanceDiff)
   491  	result.Attributes = make(map[string]*terraform.ResourceAttrDiff)
   492  
   493  	// Make sure to mark if the resource is tainted
   494  	if s != nil {
   495  		result.DestroyTainted = s.Tainted
   496  	}
   497  
   498  	d := &ResourceData{
   499  		schema:       m,
   500  		state:        s,
   501  		config:       c,
   502  		panicOnError: m.panicOnError(),
   503  	}
   504  
   505  	for k, schema := range m {
   506  		err := m.diff(k, schema, result, d, false)
   507  		if err != nil {
   508  			return nil, err
   509  		}
   510  	}
   511  
   512  	// Remove any nil diffs just to keep things clean
   513  	for k, v := range result.Attributes {
   514  		if v == nil {
   515  			delete(result.Attributes, k)
   516  		}
   517  	}
   518  
   519  	// If this is a non-destroy diff, call any custom diff logic that has been
   520  	// defined.
   521  	if !result.DestroyTainted && customizeDiff != nil {
   522  		mc := m.DeepCopy()
   523  		rd := newResourceDiff(mc, c, s, result)
   524  		if err := customizeDiff(rd, meta); err != nil {
   525  			return nil, err
   526  		}
   527  		for _, k := range rd.UpdatedKeys() {
   528  			err := m.diff(k, mc[k], result, rd, false)
   529  			if err != nil {
   530  				return nil, err
   531  			}
   532  		}
   533  	}
   534  
   535  	if handleRequiresNew {
   536  		// If the diff requires a new resource, then we recompute the diff
   537  		// so we have the complete new resource diff, and preserve the
   538  		// RequiresNew fields where necessary so the user knows exactly what
   539  		// caused that.
   540  		if result.RequiresNew() {
   541  			// Create the new diff
   542  			result2 := new(terraform.InstanceDiff)
   543  			result2.Attributes = make(map[string]*terraform.ResourceAttrDiff)
   544  
   545  			// Preserve the DestroyTainted flag
   546  			result2.DestroyTainted = result.DestroyTainted
   547  
   548  			// Reset the data to not contain state. We have to call init()
   549  			// again in order to reset the FieldReaders.
   550  			d.state = nil
   551  			d.init()
   552  
   553  			// Perform the diff again
   554  			for k, schema := range m {
   555  				err := m.diff(k, schema, result2, d, false)
   556  				if err != nil {
   557  					return nil, err
   558  				}
   559  			}
   560  
   561  			// Re-run customization
   562  			if !result2.DestroyTainted && customizeDiff != nil {
   563  				mc := m.DeepCopy()
   564  				rd := newResourceDiff(mc, c, d.state, result2)
   565  				if err := customizeDiff(rd, meta); err != nil {
   566  					return nil, err
   567  				}
   568  				for _, k := range rd.UpdatedKeys() {
   569  					err := m.diff(k, mc[k], result2, rd, false)
   570  					if err != nil {
   571  						return nil, err
   572  					}
   573  				}
   574  			}
   575  
   576  			// Force all the fields to not force a new since we know what we
   577  			// want to force new.
   578  			for k, attr := range result2.Attributes {
   579  				if attr == nil {
   580  					continue
   581  				}
   582  
   583  				if attr.RequiresNew {
   584  					attr.RequiresNew = false
   585  				}
   586  
   587  				if s != nil {
   588  					attr.Old = s.Attributes[k]
   589  				}
   590  			}
   591  
   592  			// Now copy in all the requires new diffs...
   593  			for k, attr := range result.Attributes {
   594  				if attr == nil {
   595  					continue
   596  				}
   597  
   598  				newAttr, ok := result2.Attributes[k]
   599  				if !ok {
   600  					newAttr = attr
   601  				}
   602  
   603  				if attr.RequiresNew {
   604  					newAttr.RequiresNew = true
   605  				}
   606  
   607  				result2.Attributes[k] = newAttr
   608  			}
   609  
   610  			// And set the diff!
   611  			result = result2
   612  		}
   613  
   614  	}
   615  
   616  	// Go through and detect all of the ComputedWhens now that we've
   617  	// finished the diff.
   618  	// TODO
   619  
   620  	if result.Empty() {
   621  		// If we don't have any diff elements, just return nil
   622  		return nil, nil
   623  	}
   624  
   625  	return result, nil
   626  }
   627  
   628  // Input implements the terraform.ResourceProvider method by asking
   629  // for input for required configuration keys that don't have a value.
   630  func (m schemaMap) Input(
   631  	input terraform.UIInput,
   632  	c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) {
   633  	keys := make([]string, 0, len(m))
   634  	for k := range m {
   635  		keys = append(keys, k)
   636  	}
   637  	sort.Strings(keys)
   638  
   639  	for _, k := range keys {
   640  		v := m[k]
   641  
   642  		// Skip things that don't require config, if that is even valid
   643  		// for a provider schema.
   644  		// Required XOR Optional must always be true to validate, so we only
   645  		// need to check one.
   646  		if v.Optional {
   647  			continue
   648  		}
   649  
   650  		// Deprecated fields should never prompt
   651  		if v.Deprecated != "" {
   652  			continue
   653  		}
   654  
   655  		// Skip things that have a value of some sort already
   656  		if _, ok := c.Raw[k]; ok {
   657  			continue
   658  		}
   659  
   660  		// Skip if it has a default value
   661  		defaultValue, err := v.DefaultValue()
   662  		if err != nil {
   663  			return nil, fmt.Errorf("%s: error loading default: %s", k, err)
   664  		}
   665  		if defaultValue != nil {
   666  			continue
   667  		}
   668  
   669  		var value interface{}
   670  		switch v.Type {
   671  		case TypeBool, TypeInt, TypeFloat, TypeSet, TypeList:
   672  			continue
   673  		case TypeString:
   674  			value, err = m.inputString(input, k, v)
   675  		default:
   676  			panic(fmt.Sprintf("Unknown type for input: %#v", v.Type))
   677  		}
   678  
   679  		if err != nil {
   680  			return nil, fmt.Errorf(
   681  				"%s: %s", k, err)
   682  		}
   683  
   684  		c.Config[k] = value
   685  	}
   686  
   687  	return c, nil
   688  }
   689  
   690  // Validate validates the configuration against this schema mapping.
   691  func (m schemaMap) Validate(c *terraform.ResourceConfig) ([]string, []error) {
   692  	return m.validateObject("", m, c)
   693  }
   694  
   695  // InternalValidate validates the format of this schema. This should be called
   696  // from a unit test (and not in user-path code) to verify that a schema
   697  // is properly built.
   698  func (m schemaMap) InternalValidate(topSchemaMap schemaMap) error {
   699  	return m.internalValidate(topSchemaMap, false)
   700  }
   701  
   702  func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) error {
   703  	if topSchemaMap == nil {
   704  		topSchemaMap = m
   705  	}
   706  	for k, v := range m {
   707  		if v.Type == TypeInvalid {
   708  			return fmt.Errorf("%s: Type must be specified", k)
   709  		}
   710  
   711  		if v.Optional && v.Required {
   712  			return fmt.Errorf("%s: Optional or Required must be set, not both", k)
   713  		}
   714  
   715  		if v.Required && v.Computed {
   716  			return fmt.Errorf("%s: Cannot be both Required and Computed", k)
   717  		}
   718  
   719  		if !v.Required && !v.Optional && !v.Computed {
   720  			return fmt.Errorf("%s: One of optional, required, or computed must be set", k)
   721  		}
   722  
   723  		computedOnly := v.Computed && !v.Optional
   724  
   725  		switch v.ConfigMode {
   726  		case SchemaConfigModeBlock:
   727  			if _, ok := v.Elem.(*Resource); !ok {
   728  				return fmt.Errorf("%s: ConfigMode of block is allowed only when Elem is *schema.Resource", k)
   729  			}
   730  			if attrsOnly {
   731  				return fmt.Errorf("%s: ConfigMode of block cannot be used in child of schema with ConfigMode of attribute", k)
   732  			}
   733  			if computedOnly {
   734  				return fmt.Errorf("%s: ConfigMode of block cannot be used for computed schema", k)
   735  			}
   736  		case SchemaConfigModeAttr:
   737  			// anything goes
   738  		case SchemaConfigModeAuto:
   739  			// Since "Auto" for Elem: *Resource would create a nested block,
   740  			// and that's impossible inside an attribute, we require it to be
   741  			// explicitly overridden as mode "Attr" for clarity.
   742  			if _, ok := v.Elem.(*Resource); ok {
   743  				if attrsOnly {
   744  					return fmt.Errorf("%s: in *schema.Resource with ConfigMode of attribute, so must also have ConfigMode of attribute", k)
   745  				}
   746  			}
   747  		default:
   748  			return fmt.Errorf("%s: invalid ConfigMode value", k)
   749  		}
   750  
   751  		if v.Computed && v.Default != nil {
   752  			return fmt.Errorf("%s: Default must be nil if computed", k)
   753  		}
   754  
   755  		if v.Required && v.Default != nil {
   756  			return fmt.Errorf("%s: Default cannot be set with Required", k)
   757  		}
   758  
   759  		if len(v.ComputedWhen) > 0 && !v.Computed {
   760  			return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k)
   761  		}
   762  
   763  		if len(v.ConflictsWith) > 0 && v.Required {
   764  			return fmt.Errorf("%s: ConflictsWith cannot be set with Required", k)
   765  		}
   766  
   767  		if len(v.ExactlyOneOf) > 0 && v.Required {
   768  			return fmt.Errorf("%s: ExactlyOneOf cannot be set with Required", k)
   769  		}
   770  
   771  		if len(v.AtLeastOneOf) > 0 && v.Required {
   772  			return fmt.Errorf("%s: AtLeastOneOf cannot be set with Required", k)
   773  		}
   774  
   775  		if len(v.ConflictsWith) > 0 {
   776  			err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap, v, false)
   777  			if err != nil {
   778  				return fmt.Errorf("ConflictsWith: %+v", err)
   779  			}
   780  		}
   781  
   782  		if len(v.RequiredWith) > 0 {
   783  			err := checkKeysAgainstSchemaFlags(k, v.RequiredWith, topSchemaMap, v, true)
   784  			if err != nil {
   785  				return fmt.Errorf("RequiredWith: %+v", err)
   786  			}
   787  		}
   788  
   789  		if len(v.ExactlyOneOf) > 0 {
   790  			err := checkKeysAgainstSchemaFlags(k, v.ExactlyOneOf, topSchemaMap, v, true)
   791  			if err != nil {
   792  				return fmt.Errorf("ExactlyOneOf: %+v", err)
   793  			}
   794  		}
   795  
   796  		if len(v.AtLeastOneOf) > 0 {
   797  			err := checkKeysAgainstSchemaFlags(k, v.AtLeastOneOf, topSchemaMap, v, true)
   798  			if err != nil {
   799  				return fmt.Errorf("AtLeastOneOf: %+v", err)
   800  			}
   801  		}
   802  
   803  		if v.Type == TypeList || v.Type == TypeSet {
   804  			if v.Elem == nil {
   805  				return fmt.Errorf("%s: Elem must be set for lists", k)
   806  			}
   807  
   808  			if v.Default != nil {
   809  				return fmt.Errorf("%s: Default is not valid for lists or sets", k)
   810  			}
   811  
   812  			if v.Type != TypeSet && v.Set != nil {
   813  				return fmt.Errorf("%s: Set can only be set for TypeSet", k)
   814  			}
   815  
   816  			switch t := v.Elem.(type) {
   817  			case *Resource:
   818  				attrsOnly := attrsOnly || v.ConfigMode == SchemaConfigModeAttr
   819  
   820  				if err := schemaMap(t.Schema).internalValidate(topSchemaMap, attrsOnly); err != nil {
   821  					return err
   822  				}
   823  			case *Schema:
   824  				bad := t.Computed || t.Optional || t.Required
   825  				if bad {
   826  					return fmt.Errorf(
   827  						"%s: Elem must have only Type set", k)
   828  				}
   829  			}
   830  		} else {
   831  			if v.MaxItems > 0 || v.MinItems > 0 {
   832  				return fmt.Errorf("%s: MaxItems and MinItems are only supported on lists or sets", k)
   833  			}
   834  		}
   835  
   836  		// Computed-only field
   837  		if v.Computed && !v.Optional {
   838  			if v.ValidateFunc != nil {
   839  				return fmt.Errorf("%s: ValidateFunc is for validating user input, "+
   840  					"there's nothing to validate on computed-only field", k)
   841  			}
   842  			if v.DiffSuppressFunc != nil {
   843  				return fmt.Errorf("%s: DiffSuppressFunc is for suppressing differences"+
   844  					" between config and state representation. "+
   845  					"There is no config for computed-only field, nothing to compare.", k)
   846  			}
   847  		}
   848  
   849  		if v.ValidateFunc != nil {
   850  			switch v.Type {
   851  			case TypeList, TypeSet:
   852  				return fmt.Errorf("%s: ValidateFunc is not yet supported on lists or sets.", k)
   853  			}
   854  		}
   855  
   856  		if v.Deprecated == "" && v.Removed == "" {
   857  			if !isValidFieldName(k) {
   858  				return fmt.Errorf("%s: Field name may only contain lowercase alphanumeric characters & underscores.", k)
   859  			}
   860  		}
   861  	}
   862  
   863  	return nil
   864  }
   865  
   866  func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap, self *Schema, allowSelfReference bool) error {
   867  	for _, key := range keys {
   868  		parts := strings.Split(key, ".")
   869  		sm := topSchemaMap
   870  		var target *Schema
   871  		for idx, part := range parts {
   872  			// Skip index fields if 0
   873  			partInt, err := strconv.Atoi(part)
   874  
   875  			if err == nil {
   876  				if partInt != 0 {
   877  					return fmt.Errorf("%s configuration block reference (%s) can only use the .0. index for TypeList and MaxItems: 1 configuration blocks", k, key)
   878  				}
   879  
   880  				continue
   881  			}
   882  
   883  			var ok bool
   884  			if target, ok = sm[part]; !ok {
   885  				return fmt.Errorf("%s references unknown attribute (%s) at part (%s)", k, key, part)
   886  			}
   887  
   888  			subResource, ok := target.Elem.(*Resource)
   889  
   890  			if !ok {
   891  				continue
   892  			}
   893  
   894  			// Skip Type/MaxItems check if not the last element
   895  			if (target.Type == TypeSet || target.MaxItems != 1) && idx+1 != len(parts) {
   896  				return fmt.Errorf("%s configuration block reference (%s) can only be used with TypeList and MaxItems: 1 configuration blocks", k, key)
   897  			}
   898  
   899  			sm = schemaMap(subResource.Schema)
   900  		}
   901  
   902  		if target == nil {
   903  			return fmt.Errorf("%s cannot find target attribute (%s), sm: %#v", k, key, sm)
   904  		}
   905  
   906  		if target == self && !allowSelfReference {
   907  			return fmt.Errorf("%s cannot reference self (%s)", k, key)
   908  		}
   909  
   910  		if target.Required {
   911  			return fmt.Errorf("%s cannot contain Required attribute (%s)", k, key)
   912  		}
   913  
   914  		if len(target.ComputedWhen) > 0 {
   915  			return fmt.Errorf("%s cannot contain Computed(When) attribute (%s)", k, key)
   916  		}
   917  	}
   918  
   919  	return nil
   920  }
   921  
   922  func isValidFieldName(name string) bool {
   923  	re := regexp.MustCompile("^[a-z0-9_]+$")
   924  	return re.MatchString(name)
   925  }
   926  
   927  // resourceDiffer is an interface that is used by the private diff functions.
   928  // This helps facilitate diff logic for both ResourceData and ResoureDiff with
   929  // minimal divergence in code.
   930  type resourceDiffer interface {
   931  	diffChange(string) (interface{}, interface{}, bool, bool, bool)
   932  	Get(string) interface{}
   933  	GetChange(string) (interface{}, interface{})
   934  	GetOk(string) (interface{}, bool)
   935  	HasChange(string) bool
   936  	Id() string
   937  }
   938  
   939  func (m schemaMap) diff(
   940  	k string,
   941  	schema *Schema,
   942  	diff *terraform.InstanceDiff,
   943  	d resourceDiffer,
   944  	all bool) error {
   945  
   946  	unsupressedDiff := new(terraform.InstanceDiff)
   947  	unsupressedDiff.Attributes = make(map[string]*terraform.ResourceAttrDiff)
   948  
   949  	var err error
   950  	switch schema.Type {
   951  	case TypeBool, TypeInt, TypeFloat, TypeString:
   952  		err = m.diffString(k, schema, unsupressedDiff, d, all)
   953  	case TypeList:
   954  		err = m.diffList(k, schema, unsupressedDiff, d, all)
   955  	case TypeMap:
   956  		err = m.diffMap(k, schema, unsupressedDiff, d, all)
   957  	case TypeSet:
   958  		err = m.diffSet(k, schema, unsupressedDiff, d, all)
   959  	default:
   960  		err = fmt.Errorf("%s: unknown type %#v", k, schema.Type)
   961  	}
   962  
   963  	for attrK, attrV := range unsupressedDiff.Attributes {
   964  		switch rd := d.(type) {
   965  		case *ResourceData:
   966  			if schema.DiffSuppressFunc != nil && attrV != nil &&
   967  				schema.DiffSuppressFunc(attrK, attrV.Old, attrV.New, rd) {
   968  				// If this attr diff is suppressed, we may still need it in the
   969  				// overall diff if it's contained within a set. Rather than
   970  				// dropping the diff, make it a NOOP.
   971  				if !all {
   972  					continue
   973  				}
   974  
   975  				attrV = &terraform.ResourceAttrDiff{
   976  					Old: attrV.Old,
   977  					New: attrV.Old,
   978  				}
   979  			}
   980  		}
   981  		diff.Attributes[attrK] = attrV
   982  	}
   983  
   984  	return err
   985  }
   986  
   987  func (m schemaMap) diffList(
   988  	k string,
   989  	schema *Schema,
   990  	diff *terraform.InstanceDiff,
   991  	d resourceDiffer,
   992  	all bool) error {
   993  	o, n, _, computedList, customized := d.diffChange(k)
   994  	if computedList {
   995  		n = nil
   996  	}
   997  	nSet := n != nil
   998  
   999  	// If we have an old value and no new value is set or will be
  1000  	// computed once all variables can be interpolated and we're
  1001  	// computed, then nothing has changed.
  1002  	if o != nil && n == nil && !computedList && schema.Computed {
  1003  		return nil
  1004  	}
  1005  
  1006  	if o == nil {
  1007  		o = []interface{}{}
  1008  	}
  1009  	if n == nil {
  1010  		n = []interface{}{}
  1011  	}
  1012  	if s, ok := o.(*Set); ok {
  1013  		o = s.List()
  1014  	}
  1015  	if s, ok := n.(*Set); ok {
  1016  		n = s.List()
  1017  	}
  1018  	os := o.([]interface{})
  1019  	vs := n.([]interface{})
  1020  
  1021  	// If the new value was set, and the two are equal, then we're done.
  1022  	// We have to do this check here because sets might be NOT
  1023  	// reflect.DeepEqual so we need to wait until we get the []interface{}
  1024  	if !all && nSet && reflect.DeepEqual(os, vs) {
  1025  		return nil
  1026  	}
  1027  
  1028  	// Get the counts
  1029  	oldLen := len(os)
  1030  	newLen := len(vs)
  1031  	oldStr := strconv.FormatInt(int64(oldLen), 10)
  1032  
  1033  	// If the whole list is computed, then say that the # is computed
  1034  	if computedList {
  1035  		diff.Attributes[k+".#"] = &terraform.ResourceAttrDiff{
  1036  			Old:         oldStr,
  1037  			NewComputed: true,
  1038  			RequiresNew: schema.ForceNew,
  1039  		}
  1040  		return nil
  1041  	}
  1042  
  1043  	// If the counts are not the same, then record that diff
  1044  	changed := oldLen != newLen
  1045  	computed := oldLen == 0 && newLen == 0 && schema.Computed
  1046  	if changed || computed || all {
  1047  		countSchema := &Schema{
  1048  			Type:     TypeInt,
  1049  			Computed: schema.Computed,
  1050  			ForceNew: schema.ForceNew,
  1051  		}
  1052  
  1053  		newStr := ""
  1054  		if !computed {
  1055  			newStr = strconv.FormatInt(int64(newLen), 10)
  1056  		} else {
  1057  			oldStr = ""
  1058  		}
  1059  
  1060  		diff.Attributes[k+".#"] = countSchema.finalizeDiff(
  1061  			&terraform.ResourceAttrDiff{
  1062  				Old: oldStr,
  1063  				New: newStr,
  1064  			},
  1065  			customized,
  1066  		)
  1067  	}
  1068  
  1069  	// Figure out the maximum
  1070  	maxLen := oldLen
  1071  	if newLen > maxLen {
  1072  		maxLen = newLen
  1073  	}
  1074  
  1075  	switch t := schema.Elem.(type) {
  1076  	case *Resource:
  1077  		// This is a complex resource
  1078  		for i := 0; i < maxLen; i++ {
  1079  			for k2, schema := range t.Schema {
  1080  				subK := fmt.Sprintf("%s.%d.%s", k, i, k2)
  1081  				err := m.diff(subK, schema, diff, d, all)
  1082  				if err != nil {
  1083  					return err
  1084  				}
  1085  			}
  1086  		}
  1087  	case *Schema:
  1088  		// Copy the schema so that we can set Computed/ForceNew from
  1089  		// the parent schema (the TypeList).
  1090  		t2 := *t
  1091  		t2.ForceNew = schema.ForceNew
  1092  
  1093  		// This is just a primitive element, so go through each and
  1094  		// just diff each.
  1095  		for i := 0; i < maxLen; i++ {
  1096  			subK := fmt.Sprintf("%s.%d", k, i)
  1097  			err := m.diff(subK, &t2, diff, d, all)
  1098  			if err != nil {
  1099  				return err
  1100  			}
  1101  		}
  1102  	default:
  1103  		return fmt.Errorf("%s: unknown element type (internal)", k)
  1104  	}
  1105  
  1106  	return nil
  1107  }
  1108  
  1109  func (m schemaMap) diffMap(
  1110  	k string,
  1111  	schema *Schema,
  1112  	diff *terraform.InstanceDiff,
  1113  	d resourceDiffer,
  1114  	all bool) error {
  1115  	prefix := k + "."
  1116  
  1117  	// First get all the values from the state
  1118  	var stateMap, configMap map[string]string
  1119  	o, n, _, nComputed, customized := d.diffChange(k)
  1120  	if err := mapstructure.WeakDecode(o, &stateMap); err != nil {
  1121  		return fmt.Errorf("%s: %s", k, err)
  1122  	}
  1123  	if err := mapstructure.WeakDecode(n, &configMap); err != nil {
  1124  		return fmt.Errorf("%s: %s", k, err)
  1125  	}
  1126  
  1127  	// Keep track of whether the state _exists_ at all prior to clearing it
  1128  	stateExists := o != nil
  1129  
  1130  	// Delete any count values, since we don't use those
  1131  	delete(configMap, "%")
  1132  	delete(stateMap, "%")
  1133  
  1134  	// Check if the number of elements has changed.
  1135  	oldLen, newLen := len(stateMap), len(configMap)
  1136  	changed := oldLen != newLen
  1137  	if oldLen != 0 && newLen == 0 && schema.Computed {
  1138  		changed = false
  1139  	}
  1140  
  1141  	// It is computed if we have no old value, no new value, the schema
  1142  	// says it is computed, and it didn't exist in the state before. The
  1143  	// last point means: if it existed in the state, even empty, then it
  1144  	// has already been computed.
  1145  	computed := oldLen == 0 && newLen == 0 && schema.Computed && !stateExists
  1146  
  1147  	// If the count has changed or we're computed, then add a diff for the
  1148  	// count. "nComputed" means that the new value _contains_ a value that
  1149  	// is computed. We don't do granular diffs for this yet, so we mark the
  1150  	// whole map as computed.
  1151  	if changed || computed || nComputed {
  1152  		countSchema := &Schema{
  1153  			Type:     TypeInt,
  1154  			Computed: schema.Computed || nComputed,
  1155  			ForceNew: schema.ForceNew,
  1156  		}
  1157  
  1158  		oldStr := strconv.FormatInt(int64(oldLen), 10)
  1159  		newStr := ""
  1160  		if !computed && !nComputed {
  1161  			newStr = strconv.FormatInt(int64(newLen), 10)
  1162  		} else {
  1163  			oldStr = ""
  1164  		}
  1165  
  1166  		diff.Attributes[k+".%"] = countSchema.finalizeDiff(
  1167  			&terraform.ResourceAttrDiff{
  1168  				Old: oldStr,
  1169  				New: newStr,
  1170  			},
  1171  			customized,
  1172  		)
  1173  	}
  1174  
  1175  	// If the new map is nil and we're computed, then ignore it.
  1176  	if n == nil && schema.Computed {
  1177  		return nil
  1178  	}
  1179  
  1180  	// Now we compare, preferring values from the config map
  1181  	for k, v := range configMap {
  1182  		old, ok := stateMap[k]
  1183  		delete(stateMap, k)
  1184  
  1185  		if old == v && ok && !all {
  1186  			continue
  1187  		}
  1188  
  1189  		diff.Attributes[prefix+k] = schema.finalizeDiff(
  1190  			&terraform.ResourceAttrDiff{
  1191  				Old: old,
  1192  				New: v,
  1193  			},
  1194  			customized,
  1195  		)
  1196  	}
  1197  	for k, v := range stateMap {
  1198  		diff.Attributes[prefix+k] = schema.finalizeDiff(
  1199  			&terraform.ResourceAttrDiff{
  1200  				Old:        v,
  1201  				NewRemoved: true,
  1202  			},
  1203  			customized,
  1204  		)
  1205  	}
  1206  
  1207  	return nil
  1208  }
  1209  
  1210  func (m schemaMap) diffSet(
  1211  	k string,
  1212  	schema *Schema,
  1213  	diff *terraform.InstanceDiff,
  1214  	d resourceDiffer,
  1215  	all bool) error {
  1216  
  1217  	o, n, _, computedSet, customized := d.diffChange(k)
  1218  	if computedSet {
  1219  		n = nil
  1220  	}
  1221  	nSet := n != nil
  1222  
  1223  	// If we have an old value and no new value is set or will be
  1224  	// computed once all variables can be interpolated and we're
  1225  	// computed, then nothing has changed.
  1226  	if o != nil && n == nil && !computedSet && schema.Computed {
  1227  		return nil
  1228  	}
  1229  
  1230  	if o == nil {
  1231  		o = schema.ZeroValue().(*Set)
  1232  	}
  1233  	if n == nil {
  1234  		n = schema.ZeroValue().(*Set)
  1235  	}
  1236  	os := o.(*Set)
  1237  	ns := n.(*Set)
  1238  
  1239  	// If the new value was set, compare the listCode's to determine if
  1240  	// the two are equal. Comparing listCode's instead of the actual values
  1241  	// is needed because there could be computed values in the set which
  1242  	// would result in false positives while comparing.
  1243  	if !all && nSet && reflect.DeepEqual(os.listCode(), ns.listCode()) {
  1244  		return nil
  1245  	}
  1246  
  1247  	// Get the counts
  1248  	oldLen := os.Len()
  1249  	newLen := ns.Len()
  1250  	oldStr := strconv.Itoa(oldLen)
  1251  	newStr := strconv.Itoa(newLen)
  1252  
  1253  	// Build a schema for our count
  1254  	countSchema := &Schema{
  1255  		Type:     TypeInt,
  1256  		Computed: schema.Computed,
  1257  		ForceNew: schema.ForceNew,
  1258  	}
  1259  
  1260  	// If the set computed then say that the # is computed
  1261  	if computedSet || schema.Computed && !nSet {
  1262  		// If # already exists, equals 0 and no new set is supplied, there
  1263  		// is nothing to record in the diff
  1264  		count, ok := d.GetOk(k + ".#")
  1265  		if ok && count.(int) == 0 && !nSet && !computedSet {
  1266  			return nil
  1267  		}
  1268  
  1269  		// Set the count but make sure that if # does not exist, we don't
  1270  		// use the zeroed value
  1271  		countStr := strconv.Itoa(count.(int))
  1272  		if !ok {
  1273  			countStr = ""
  1274  		}
  1275  
  1276  		diff.Attributes[k+".#"] = countSchema.finalizeDiff(
  1277  			&terraform.ResourceAttrDiff{
  1278  				Old:         countStr,
  1279  				NewComputed: true,
  1280  			},
  1281  			customized,
  1282  		)
  1283  		return nil
  1284  	}
  1285  
  1286  	// If the counts are not the same, then record that diff
  1287  	changed := oldLen != newLen
  1288  	if changed || all {
  1289  		diff.Attributes[k+".#"] = countSchema.finalizeDiff(
  1290  			&terraform.ResourceAttrDiff{
  1291  				Old: oldStr,
  1292  				New: newStr,
  1293  			},
  1294  			customized,
  1295  		)
  1296  	}
  1297  
  1298  	// Build the list of codes that will make up our set. This is the
  1299  	// removed codes as well as all the codes in the new codes.
  1300  	codes := make([][]string, 2)
  1301  	codes[0] = os.Difference(ns).listCode()
  1302  	codes[1] = ns.listCode()
  1303  	for _, list := range codes {
  1304  		for _, code := range list {
  1305  			switch t := schema.Elem.(type) {
  1306  			case *Resource:
  1307  				// This is a complex resource
  1308  				for k2, schema := range t.Schema {
  1309  					subK := fmt.Sprintf("%s.%s.%s", k, code, k2)
  1310  					err := m.diff(subK, schema, diff, d, true)
  1311  					if err != nil {
  1312  						return err
  1313  					}
  1314  				}
  1315  			case *Schema:
  1316  				// Copy the schema so that we can set Computed/ForceNew from
  1317  				// the parent schema (the TypeSet).
  1318  				t2 := *t
  1319  				t2.ForceNew = schema.ForceNew
  1320  
  1321  				// This is just a primitive element, so go through each and
  1322  				// just diff each.
  1323  				subK := fmt.Sprintf("%s.%s", k, code)
  1324  				err := m.diff(subK, &t2, diff, d, true)
  1325  				if err != nil {
  1326  					return err
  1327  				}
  1328  			default:
  1329  				return fmt.Errorf("%s: unknown element type (internal)", k)
  1330  			}
  1331  		}
  1332  	}
  1333  
  1334  	return nil
  1335  }
  1336  
  1337  func (m schemaMap) diffString(
  1338  	k string,
  1339  	schema *Schema,
  1340  	diff *terraform.InstanceDiff,
  1341  	d resourceDiffer,
  1342  	all bool) error {
  1343  	var originalN interface{}
  1344  	var os, ns string
  1345  	o, n, _, computed, customized := d.diffChange(k)
  1346  	if schema.StateFunc != nil && n != nil {
  1347  		originalN = n
  1348  		n = schema.StateFunc(n)
  1349  	}
  1350  	nraw := n
  1351  	if nraw == nil && o != nil {
  1352  		nraw = schema.Type.Zero()
  1353  	}
  1354  	if err := mapstructure.WeakDecode(o, &os); err != nil {
  1355  		return fmt.Errorf("%s: %s", k, err)
  1356  	}
  1357  	if err := mapstructure.WeakDecode(nraw, &ns); err != nil {
  1358  		return fmt.Errorf("%s: %s", k, err)
  1359  	}
  1360  
  1361  	if os == ns && !all && !computed {
  1362  		// They're the same value. If there old value is not blank or we
  1363  		// have an ID, then return right away since we're already setup.
  1364  		if os != "" || d.Id() != "" {
  1365  			return nil
  1366  		}
  1367  
  1368  		// Otherwise, only continue if we're computed
  1369  		if !schema.Computed {
  1370  			return nil
  1371  		}
  1372  	}
  1373  
  1374  	removed := false
  1375  	if o != nil && n == nil && !computed {
  1376  		removed = true
  1377  	}
  1378  	if removed && schema.Computed {
  1379  		return nil
  1380  	}
  1381  
  1382  	diff.Attributes[k] = schema.finalizeDiff(
  1383  		&terraform.ResourceAttrDiff{
  1384  			Old:         os,
  1385  			New:         ns,
  1386  			NewExtra:    originalN,
  1387  			NewRemoved:  removed,
  1388  			NewComputed: computed,
  1389  		},
  1390  		customized,
  1391  	)
  1392  
  1393  	return nil
  1394  }
  1395  
  1396  func (m schemaMap) inputString(
  1397  	input terraform.UIInput,
  1398  	k string,
  1399  	schema *Schema) (interface{}, error) {
  1400  	result, err := input.Input(context.Background(), &terraform.InputOpts{
  1401  		Id:          k,
  1402  		Query:       k,
  1403  		Description: schema.Description,
  1404  		Default:     schema.InputDefault,
  1405  	})
  1406  
  1407  	return result, err
  1408  }
  1409  
  1410  func (m schemaMap) validate(
  1411  	k string,
  1412  	schema *Schema,
  1413  	c *terraform.ResourceConfig) ([]string, []error) {
  1414  	raw, ok := c.Get(k)
  1415  	if !ok && schema.DefaultFunc != nil {
  1416  		// We have a dynamic default. Check if we have a value.
  1417  		var err error
  1418  		raw, err = schema.DefaultFunc()
  1419  		if err != nil {
  1420  			return nil, []error{fmt.Errorf(
  1421  				"%q, error loading default: %s", k, err)}
  1422  		}
  1423  
  1424  		// We're okay as long as we had a value set
  1425  		ok = raw != nil
  1426  	}
  1427  
  1428  	err := validateExactlyOneAttribute(k, schema, c)
  1429  	if err != nil {
  1430  		return nil, []error{err}
  1431  	}
  1432  
  1433  	err = validateAtLeastOneAttribute(k, schema, c)
  1434  	if err != nil {
  1435  		return nil, []error{err}
  1436  	}
  1437  
  1438  	if !ok {
  1439  		if schema.Required {
  1440  			return nil, []error{fmt.Errorf(
  1441  				"%q: required field is not set", k)}
  1442  		}
  1443  		return nil, nil
  1444  	}
  1445  
  1446  	if !schema.Required && !schema.Optional {
  1447  		// This is a computed-only field
  1448  		return nil, []error{fmt.Errorf(
  1449  			"%q: this field cannot be set", k)}
  1450  	}
  1451  
  1452  	err = validateRequiredWithAttribute(k, schema, c)
  1453  	if err != nil {
  1454  		return nil, []error{err}
  1455  	}
  1456  
  1457  	// If the value is unknown then we can't validate it yet.
  1458  	// In particular, this avoids spurious type errors where downstream
  1459  	// validation code sees UnknownVariableValue as being just a string.
  1460  	// The SDK has to allow the unknown value through initially, so that
  1461  	// Required fields set via an interpolated value are accepted.
  1462  	if !isWhollyKnown(raw) {
  1463  		if schema.Deprecated != "" {
  1464  			return []string{fmt.Sprintf("%q: [DEPRECATED] %s", k, schema.Deprecated)}, nil
  1465  		}
  1466  		return nil, nil
  1467  	}
  1468  
  1469  	err = validateConflictingAttributes(k, schema, c)
  1470  	if err != nil {
  1471  		return nil, []error{err}
  1472  	}
  1473  
  1474  	return m.validateType(k, raw, schema, c)
  1475  }
  1476  
  1477  // isWhollyKnown returns false if the argument contains an UnknownVariableValue
  1478  func isWhollyKnown(raw interface{}) bool {
  1479  	switch raw := raw.(type) {
  1480  	case string:
  1481  		if raw == hcl2shim.UnknownVariableValue {
  1482  			return false
  1483  		}
  1484  	case []interface{}:
  1485  		for _, v := range raw {
  1486  			if !isWhollyKnown(v) {
  1487  				return false
  1488  			}
  1489  		}
  1490  	case map[string]interface{}:
  1491  		for _, v := range raw {
  1492  			if !isWhollyKnown(v) {
  1493  				return false
  1494  			}
  1495  		}
  1496  	}
  1497  	return true
  1498  }
  1499  func validateConflictingAttributes(
  1500  	k string,
  1501  	schema *Schema,
  1502  	c *terraform.ResourceConfig) error {
  1503  
  1504  	if len(schema.ConflictsWith) == 0 {
  1505  		return nil
  1506  	}
  1507  
  1508  	for _, conflictingKey := range schema.ConflictsWith {
  1509  		if raw, ok := c.Get(conflictingKey); ok {
  1510  			if raw == hcl2shim.UnknownVariableValue {
  1511  				// An unknown value might become unset (null) once known, so
  1512  				// we must defer validation until it's known.
  1513  				continue
  1514  			}
  1515  			return fmt.Errorf(
  1516  				"%q: conflicts with %s", k, conflictingKey)
  1517  		}
  1518  	}
  1519  
  1520  	return nil
  1521  }
  1522  
  1523  func removeDuplicates(elements []string) []string {
  1524  	encountered := make(map[string]struct{}, 0)
  1525  	result := []string{}
  1526  
  1527  	for v := range elements {
  1528  		if _, ok := encountered[elements[v]]; !ok {
  1529  			encountered[elements[v]] = struct{}{}
  1530  			result = append(result, elements[v])
  1531  		}
  1532  	}
  1533  
  1534  	return result
  1535  }
  1536  
  1537  func validateRequiredWithAttribute(
  1538  	k string,
  1539  	schema *Schema,
  1540  	c *terraform.ResourceConfig) error {
  1541  
  1542  	if len(schema.RequiredWith) == 0 {
  1543  		return nil
  1544  	}
  1545  
  1546  	allKeys := removeDuplicates(append(schema.RequiredWith, k))
  1547  	sort.Strings(allKeys)
  1548  
  1549  	for _, key := range allKeys {
  1550  		if _, ok := c.Get(key); !ok {
  1551  			return fmt.Errorf("%q: all of `%s` must be specified", k, strings.Join(allKeys, ","))
  1552  		}
  1553  	}
  1554  
  1555  	return nil
  1556  }
  1557  
  1558  func validateExactlyOneAttribute(
  1559  	k string,
  1560  	schema *Schema,
  1561  	c *terraform.ResourceConfig) error {
  1562  
  1563  	if len(schema.ExactlyOneOf) == 0 {
  1564  		return nil
  1565  	}
  1566  
  1567  	allKeys := removeDuplicates(append(schema.ExactlyOneOf, k))
  1568  	sort.Strings(allKeys)
  1569  	specified := make([]string, 0)
  1570  	unknownVariableValueCount := 0
  1571  	for _, exactlyOneOfKey := range allKeys {
  1572  		if c.IsComputed(exactlyOneOfKey) {
  1573  			unknownVariableValueCount++
  1574  			continue
  1575  		}
  1576  
  1577  		_, ok := c.Get(exactlyOneOfKey)
  1578  		if ok {
  1579  			specified = append(specified, exactlyOneOfKey)
  1580  		}
  1581  	}
  1582  
  1583  	if len(specified) == 0 && unknownVariableValueCount == 0 {
  1584  		return fmt.Errorf("%q: one of `%s` must be specified", k, strings.Join(allKeys, ","))
  1585  	}
  1586  
  1587  	if len(specified) > 1 {
  1588  		return fmt.Errorf("%q: only one of `%s` can be specified, but `%s` were specified.", k, strings.Join(allKeys, ","), strings.Join(specified, ","))
  1589  	}
  1590  
  1591  	return nil
  1592  }
  1593  
  1594  func validateAtLeastOneAttribute(
  1595  	k string,
  1596  	schema *Schema,
  1597  	c *terraform.ResourceConfig) error {
  1598  
  1599  	if len(schema.AtLeastOneOf) == 0 {
  1600  		return nil
  1601  	}
  1602  
  1603  	allKeys := removeDuplicates(append(schema.AtLeastOneOf, k))
  1604  	sort.Strings(allKeys)
  1605  
  1606  	for _, atLeastOneOfKey := range allKeys {
  1607  		if _, ok := c.Get(atLeastOneOfKey); ok {
  1608  			// We can ignore hcl2shim.UnknownVariable by assuming it's been set and additional validation elsewhere
  1609  			// will uncover this if it is in fact null.
  1610  			return nil
  1611  		}
  1612  	}
  1613  
  1614  	return fmt.Errorf("%q: one of `%s` must be specified", k, strings.Join(allKeys, ","))
  1615  }
  1616  
  1617  func (m schemaMap) validateList(
  1618  	k string,
  1619  	raw interface{},
  1620  	schema *Schema,
  1621  	c *terraform.ResourceConfig) ([]string, []error) {
  1622  	// first check if the list is wholly unknown
  1623  	if s, ok := raw.(string); ok {
  1624  		if s == hcl2shim.UnknownVariableValue {
  1625  			return nil, nil
  1626  		}
  1627  	}
  1628  
  1629  	// schemaMap can't validate nil
  1630  	if raw == nil {
  1631  		return nil, nil
  1632  	}
  1633  
  1634  	// We use reflection to verify the slice because you can't
  1635  	// case to []interface{} unless the slice is exactly that type.
  1636  	rawV := reflect.ValueOf(raw)
  1637  
  1638  	// If we support promotion and the raw value isn't a slice, wrap
  1639  	// it in []interface{} and check again.
  1640  	if schema.PromoteSingle && rawV.Kind() != reflect.Slice {
  1641  		raw = []interface{}{raw}
  1642  		rawV = reflect.ValueOf(raw)
  1643  	}
  1644  
  1645  	if rawV.Kind() != reflect.Slice {
  1646  		return nil, []error{fmt.Errorf(
  1647  			"%s: should be a list", k)}
  1648  	}
  1649  
  1650  	// We can't validate list length if this came from a dynamic block.
  1651  	// Since there's no way to determine if something was from a dynamic block
  1652  	// at this point, we're going to skip validation in the new protocol if
  1653  	// there are any unknowns. Validate will eventually be called again once
  1654  	// all values are known.
  1655  	if isProto5() && !isWhollyKnown(raw) {
  1656  		return nil, nil
  1657  	}
  1658  
  1659  	// Validate length
  1660  	if schema.MaxItems > 0 && rawV.Len() > schema.MaxItems {
  1661  		return nil, []error{fmt.Errorf(
  1662  			"%s: attribute supports %d item maximum, config has %d declared", k, schema.MaxItems, rawV.Len())}
  1663  	}
  1664  
  1665  	if schema.MinItems > 0 && rawV.Len() < schema.MinItems {
  1666  		return nil, []error{fmt.Errorf(
  1667  			"%s: attribute supports %d item as a minimum, config has %d declared", k, schema.MinItems, rawV.Len())}
  1668  	}
  1669  
  1670  	// Now build the []interface{}
  1671  	raws := make([]interface{}, rawV.Len())
  1672  	for i := range raws {
  1673  		raws[i] = rawV.Index(i).Interface()
  1674  	}
  1675  
  1676  	var ws []string
  1677  	var es []error
  1678  	for i, raw := range raws {
  1679  		key := fmt.Sprintf("%s.%d", k, i)
  1680  
  1681  		// Reify the key value from the ResourceConfig.
  1682  		// If the list was computed we have all raw values, but some of these
  1683  		// may be known in the config, and aren't individually marked as Computed.
  1684  		if r, ok := c.Get(key); ok {
  1685  			raw = r
  1686  		}
  1687  
  1688  		var ws2 []string
  1689  		var es2 []error
  1690  		switch t := schema.Elem.(type) {
  1691  		case *Resource:
  1692  			// This is a sub-resource
  1693  			ws2, es2 = m.validateObject(key, t.Schema, c)
  1694  		case *Schema:
  1695  			ws2, es2 = m.validateType(key, raw, t, c)
  1696  		}
  1697  
  1698  		if len(ws2) > 0 {
  1699  			ws = append(ws, ws2...)
  1700  		}
  1701  		if len(es2) > 0 {
  1702  			es = append(es, es2...)
  1703  		}
  1704  	}
  1705  
  1706  	return ws, es
  1707  }
  1708  
  1709  func (m schemaMap) validateMap(
  1710  	k string,
  1711  	raw interface{},
  1712  	schema *Schema,
  1713  	c *terraform.ResourceConfig) ([]string, []error) {
  1714  	// first check if the list is wholly unknown
  1715  	if s, ok := raw.(string); ok {
  1716  		if s == hcl2shim.UnknownVariableValue {
  1717  			return nil, nil
  1718  		}
  1719  	}
  1720  
  1721  	// schemaMap can't validate nil
  1722  	if raw == nil {
  1723  		return nil, nil
  1724  	}
  1725  	// We use reflection to verify the slice because you can't
  1726  	// case to []interface{} unless the slice is exactly that type.
  1727  	rawV := reflect.ValueOf(raw)
  1728  	switch rawV.Kind() {
  1729  	case reflect.String:
  1730  		// If raw and reified are equal, this is a string and should
  1731  		// be rejected.
  1732  		reified, reifiedOk := c.Get(k)
  1733  		if reifiedOk && raw == reified && !c.IsComputed(k) {
  1734  			return nil, []error{fmt.Errorf("%s: should be a map", k)}
  1735  		}
  1736  		// Otherwise it's likely raw is an interpolation.
  1737  		return nil, nil
  1738  	case reflect.Map:
  1739  	case reflect.Slice:
  1740  	default:
  1741  		return nil, []error{fmt.Errorf("%s: should be a map", k)}
  1742  	}
  1743  
  1744  	// If it is not a slice, validate directly
  1745  	if rawV.Kind() != reflect.Slice {
  1746  		mapIface := rawV.Interface()
  1747  		if _, errs := validateMapValues(k, mapIface.(map[string]interface{}), schema); len(errs) > 0 {
  1748  			return nil, errs
  1749  		}
  1750  		if schema.ValidateFunc != nil {
  1751  			return schema.ValidateFunc(mapIface, k)
  1752  		}
  1753  		return nil, nil
  1754  	}
  1755  
  1756  	// It is a slice, verify that all the elements are maps
  1757  	raws := make([]interface{}, rawV.Len())
  1758  	for i := range raws {
  1759  		raws[i] = rawV.Index(i).Interface()
  1760  	}
  1761  
  1762  	for _, raw := range raws {
  1763  		v := reflect.ValueOf(raw)
  1764  		if v.Kind() != reflect.Map {
  1765  			return nil, []error{fmt.Errorf(
  1766  				"%s: should be a map", k)}
  1767  		}
  1768  		mapIface := v.Interface()
  1769  		if _, errs := validateMapValues(k, mapIface.(map[string]interface{}), schema); len(errs) > 0 {
  1770  			return nil, errs
  1771  		}
  1772  	}
  1773  
  1774  	if schema.ValidateFunc != nil {
  1775  		validatableMap := make(map[string]interface{})
  1776  		for _, raw := range raws {
  1777  			for k, v := range raw.(map[string]interface{}) {
  1778  				validatableMap[k] = v
  1779  			}
  1780  		}
  1781  
  1782  		return schema.ValidateFunc(validatableMap, k)
  1783  	}
  1784  
  1785  	return nil, nil
  1786  }
  1787  
  1788  func validateMapValues(k string, m map[string]interface{}, schema *Schema) ([]string, []error) {
  1789  	for key, raw := range m {
  1790  		valueType, err := getValueType(k, schema)
  1791  		if err != nil {
  1792  			return nil, []error{err}
  1793  		}
  1794  
  1795  		switch valueType {
  1796  		case TypeBool:
  1797  			var n bool
  1798  			if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1799  				return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
  1800  			}
  1801  		case TypeInt:
  1802  			var n int
  1803  			if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1804  				return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
  1805  			}
  1806  		case TypeFloat:
  1807  			var n float64
  1808  			if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1809  				return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
  1810  			}
  1811  		case TypeString:
  1812  			var n string
  1813  			if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1814  				return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
  1815  			}
  1816  		default:
  1817  			panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
  1818  		}
  1819  	}
  1820  	return nil, nil
  1821  }
  1822  
  1823  func getValueType(k string, schema *Schema) (ValueType, error) {
  1824  	if schema.Elem == nil {
  1825  		return TypeString, nil
  1826  	}
  1827  	if vt, ok := schema.Elem.(ValueType); ok {
  1828  		return vt, nil
  1829  	}
  1830  
  1831  	// If a Schema is provided to a Map, we use the Type of that schema
  1832  	// as the type for each element in the Map.
  1833  	if s, ok := schema.Elem.(*Schema); ok {
  1834  		return s.Type, nil
  1835  	}
  1836  
  1837  	if _, ok := schema.Elem.(*Resource); ok {
  1838  		// TODO: We don't actually support this (yet)
  1839  		// but silently pass the validation, until we decide
  1840  		// how to handle nested structures in maps
  1841  		return TypeString, nil
  1842  	}
  1843  	return 0, fmt.Errorf("%s: unexpected map value type: %#v", k, schema.Elem)
  1844  }
  1845  
  1846  func (m schemaMap) validateObject(
  1847  	k string,
  1848  	schema map[string]*Schema,
  1849  	c *terraform.ResourceConfig) ([]string, []error) {
  1850  	raw, _ := c.Get(k)
  1851  
  1852  	// schemaMap can't validate nil
  1853  	if raw == nil {
  1854  		return nil, nil
  1855  	}
  1856  
  1857  	if _, ok := raw.(map[string]interface{}); !ok && !c.IsComputed(k) {
  1858  		return nil, []error{fmt.Errorf(
  1859  			"%s: expected object, got %s",
  1860  			k, reflect.ValueOf(raw).Kind())}
  1861  	}
  1862  
  1863  	var ws []string
  1864  	var es []error
  1865  	for subK, s := range schema {
  1866  		key := subK
  1867  		if k != "" {
  1868  			key = fmt.Sprintf("%s.%s", k, subK)
  1869  		}
  1870  
  1871  		ws2, es2 := m.validate(key, s, c)
  1872  		if len(ws2) > 0 {
  1873  			ws = append(ws, ws2...)
  1874  		}
  1875  		if len(es2) > 0 {
  1876  			es = append(es, es2...)
  1877  		}
  1878  	}
  1879  
  1880  	// Detect any extra/unknown keys and report those as errors.
  1881  	if m, ok := raw.(map[string]interface{}); ok {
  1882  		for subk := range m {
  1883  			if _, ok := schema[subk]; !ok {
  1884  				if subk == TimeoutsConfigKey {
  1885  					continue
  1886  				}
  1887  				es = append(es, fmt.Errorf(
  1888  					"%s: invalid or unknown key: %s", k, subk))
  1889  			}
  1890  		}
  1891  	}
  1892  
  1893  	return ws, es
  1894  }
  1895  
  1896  func (m schemaMap) validatePrimitive(
  1897  	k string,
  1898  	raw interface{},
  1899  	schema *Schema,
  1900  	c *terraform.ResourceConfig) ([]string, []error) {
  1901  
  1902  	// a nil value shouldn't happen in the old protocol, and in the new
  1903  	// protocol the types have already been validated. Either way, we can't
  1904  	// reflect on nil, so don't panic.
  1905  	if raw == nil {
  1906  		return nil, nil
  1907  	}
  1908  
  1909  	// Catch if the user gave a complex type where a primitive was
  1910  	// expected, so we can return a friendly error message that
  1911  	// doesn't contain Go type system terminology.
  1912  	switch reflect.ValueOf(raw).Type().Kind() {
  1913  	case reflect.Slice:
  1914  		return nil, []error{
  1915  			fmt.Errorf("%s must be a single value, not a list", k),
  1916  		}
  1917  	case reflect.Map:
  1918  		return nil, []error{
  1919  			fmt.Errorf("%s must be a single value, not a map", k),
  1920  		}
  1921  	default: // ok
  1922  	}
  1923  
  1924  	if c.IsComputed(k) {
  1925  		// If the key is being computed, then it is not an error as
  1926  		// long as it's not a slice or map.
  1927  		return nil, nil
  1928  	}
  1929  
  1930  	var decoded interface{}
  1931  	switch schema.Type {
  1932  	case TypeBool:
  1933  		// Verify that we can parse this as the correct type
  1934  		var n bool
  1935  		if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1936  			return nil, []error{fmt.Errorf("%s: %s", k, err)}
  1937  		}
  1938  		decoded = n
  1939  	case TypeInt:
  1940  		switch {
  1941  		case isProto5():
  1942  			// We need to verify the type precisely, because WeakDecode will
  1943  			// decode a float as an integer.
  1944  
  1945  			// the config shims only use int for integral number values
  1946  			if v, ok := raw.(int); ok {
  1947  				decoded = v
  1948  			} else {
  1949  				return nil, []error{fmt.Errorf("%s: must be a whole number, got %v", k, raw)}
  1950  			}
  1951  		default:
  1952  			// Verify that we can parse this as an int
  1953  			var n int
  1954  			if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1955  				return nil, []error{fmt.Errorf("%s: %s", k, err)}
  1956  			}
  1957  			decoded = n
  1958  		}
  1959  	case TypeFloat:
  1960  		// Verify that we can parse this as an int
  1961  		var n float64
  1962  		if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1963  			return nil, []error{fmt.Errorf("%s: %s", k, err)}
  1964  		}
  1965  		decoded = n
  1966  	case TypeString:
  1967  		// Verify that we can parse this as a string
  1968  		var n string
  1969  		if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1970  			return nil, []error{fmt.Errorf("%s: %s", k, err)}
  1971  		}
  1972  		decoded = n
  1973  	default:
  1974  		panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
  1975  	}
  1976  
  1977  	if schema.ValidateFunc != nil {
  1978  		return schema.ValidateFunc(decoded, k)
  1979  	}
  1980  
  1981  	return nil, nil
  1982  }
  1983  
  1984  func (m schemaMap) validateType(
  1985  	k string,
  1986  	raw interface{},
  1987  	schema *Schema,
  1988  	c *terraform.ResourceConfig) ([]string, []error) {
  1989  	var ws []string
  1990  	var es []error
  1991  	switch schema.Type {
  1992  	case TypeSet, TypeList:
  1993  		ws, es = m.validateList(k, raw, schema, c)
  1994  	case TypeMap:
  1995  		ws, es = m.validateMap(k, raw, schema, c)
  1996  	default:
  1997  		ws, es = m.validatePrimitive(k, raw, schema, c)
  1998  	}
  1999  
  2000  	if schema.Deprecated != "" {
  2001  		ws = append(ws, fmt.Sprintf(
  2002  			"%q: [DEPRECATED] %s", k, schema.Deprecated))
  2003  	}
  2004  
  2005  	if schema.Removed != "" {
  2006  		es = append(es, fmt.Errorf(
  2007  			"%q: [REMOVED] %s", k, schema.Removed))
  2008  	}
  2009  
  2010  	return ws, es
  2011  }
  2012  
  2013  // Zero returns the zero value for a type.
  2014  func (t ValueType) Zero() interface{} {
  2015  	switch t {
  2016  	case TypeInvalid:
  2017  		return nil
  2018  	case TypeBool:
  2019  		return false
  2020  	case TypeInt:
  2021  		return 0
  2022  	case TypeFloat:
  2023  		return 0.0
  2024  	case TypeString:
  2025  		return ""
  2026  	case TypeList:
  2027  		return []interface{}{}
  2028  	case TypeMap:
  2029  		return map[string]interface{}{}
  2030  	case TypeSet:
  2031  		return new(Set)
  2032  	case typeObject:
  2033  		return map[string]interface{}{}
  2034  	default:
  2035  		panic(fmt.Sprintf("unknown type %s", t))
  2036  	}
  2037  }