github.com/emc-cmd/terraform@v0.7.8-0.20161101145618-f16309630e7c/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  	"fmt"
    16  	"os"
    17  	"reflect"
    18  	"sort"
    19  	"strconv"
    20  	"strings"
    21  
    22  	"github.com/hashicorp/terraform/terraform"
    23  	"github.com/mitchellh/mapstructure"
    24  )
    25  
    26  // Schema is used to describe the structure of a value.
    27  //
    28  // Read the documentation of the struct elements for important details.
    29  type Schema struct {
    30  	// Type is the type of the value and must be one of the ValueType values.
    31  	//
    32  	// This type not only determines what type is expected/valid in configuring
    33  	// this value, but also what type is returned when ResourceData.Get is
    34  	// called. The types returned by Get are:
    35  	//
    36  	//   TypeBool - bool
    37  	//   TypeInt - int
    38  	//   TypeFloat - float64
    39  	//   TypeString - string
    40  	//   TypeList - []interface{}
    41  	//   TypeMap - map[string]interface{}
    42  	//   TypeSet - *schema.Set
    43  	//
    44  	Type ValueType
    45  
    46  	// If one of these is set, then this item can come from the configuration.
    47  	// Both cannot be set. If Optional is set, the value is optional. If
    48  	// Required is set, the value is required.
    49  	//
    50  	// One of these must be set if the value is not computed. That is:
    51  	// value either comes from the config, is computed, or is both.
    52  	Optional bool
    53  	Required bool
    54  
    55  	// If this is non-nil, the provided function will be used during diff
    56  	// of this field. If this is nil, a default diff for the type of the
    57  	// schema will be used.
    58  	//
    59  	// This allows comparison based on something other than primitive, list
    60  	// or map equality - for example SSH public keys may be considered
    61  	// equivalent regardless of trailing whitespace.
    62  	DiffSuppressFunc SchemaDiffSuppressFunc
    63  
    64  	// If this is non-nil, then this will be a default value that is used
    65  	// when this item is not set in the configuration/state.
    66  	//
    67  	// DefaultFunc can be specified if you want a dynamic default value.
    68  	// Only one of Default or DefaultFunc can be set.
    69  	//
    70  	// If Required is true above, then Default cannot be set. DefaultFunc
    71  	// can be set with Required. If the DefaultFunc returns nil, then there
    72  	// will be no default and the user will be asked to fill it in.
    73  	//
    74  	// If either of these is set, then the user won't be asked for input
    75  	// for this key if the default is not nil.
    76  	Default     interface{}
    77  	DefaultFunc SchemaDefaultFunc
    78  
    79  	// Description is used as the description for docs or asking for user
    80  	// input. It should be relatively short (a few sentences max) and should
    81  	// be formatted to fit a CLI.
    82  	Description string
    83  
    84  	// InputDefault is the default value to use for when inputs are requested.
    85  	// This differs from Default in that if Default is set, no input is
    86  	// asked for. If Input is asked, this will be the default value offered.
    87  	InputDefault string
    88  
    89  	// The fields below relate to diffs.
    90  	//
    91  	// If Computed is true, then the result of this value is computed
    92  	// (unless specified by config) on creation.
    93  	//
    94  	// If ForceNew is true, then a change in this resource necessitates
    95  	// the creation of a new resource.
    96  	//
    97  	// StateFunc is a function called to change the value of this before
    98  	// storing it in the state (and likewise before comparing for diffs).
    99  	// The use for this is for example with large strings, you may want
   100  	// to simply store the hash of it.
   101  	Computed  bool
   102  	ForceNew  bool
   103  	StateFunc SchemaStateFunc
   104  
   105  	// The following fields are only set for a TypeList or TypeSet Type.
   106  	//
   107  	// Elem must be either a *Schema or a *Resource only if the Type is
   108  	// TypeList, and represents what the element type is. If it is *Schema,
   109  	// the element type is just a simple value. If it is *Resource, the
   110  	// element type is a complex structure, potentially with its own lifecycle.
   111  	//
   112  	// MaxItems defines a maximum amount of items that can exist within a
   113  	// TypeSet or TypeList. Specific use cases would be if a TypeSet is being
   114  	// used to wrap a complex structure, however more than one instance would
   115  	// cause instability.
   116  	//
   117  	// MinItems defines a minimum amount of items that can exist within a
   118  	// TypeSet or TypeList. Specific use cases would be if a TypeSet is being
   119  	// used to wrap a complex structure, however less than one instance would
   120  	// cause instability.
   121  	Elem     interface{}
   122  	MaxItems int
   123  	MinItems int
   124  
   125  	// The following fields are only valid for a TypeSet type.
   126  	//
   127  	// Set defines a function to determine the unique ID of an item so that
   128  	// a proper set can be built.
   129  	Set SchemaSetFunc
   130  
   131  	// ComputedWhen is a set of queries on the configuration. Whenever any
   132  	// of these things is changed, it will require a recompute (this requires
   133  	// that Computed is set to true).
   134  	//
   135  	// NOTE: This currently does not work.
   136  	ComputedWhen []string
   137  
   138  	// ConflictsWith is a set of schema keys that conflict with this schema
   139  	ConflictsWith []string
   140  
   141  	// When Deprecated is set, this attribute is deprecated.
   142  	//
   143  	// A deprecated field still works, but will probably stop working in near
   144  	// future. This string is the message shown to the user with instructions on
   145  	// how to address the deprecation.
   146  	Deprecated string
   147  
   148  	// When Removed is set, this attribute has been removed from the schema
   149  	//
   150  	// Removed attributes can be left in the Schema to generate informative error
   151  	// messages for the user when they show up in resource configurations.
   152  	// This string is the message shown to the user with instructions on
   153  	// what do to about the removed attribute.
   154  	Removed string
   155  
   156  	// ValidateFunc allows individual fields to define arbitrary validation
   157  	// logic. It is yielded the provided config value as an interface{} that is
   158  	// guaranteed to be of the proper Schema type, and it can yield warnings or
   159  	// errors based on inspection of that value.
   160  	//
   161  	// ValidateFunc currently only works for primitive types.
   162  	ValidateFunc SchemaValidateFunc
   163  
   164  	// Sensitive ensures that the attribute's value does not get displayed in
   165  	// logs or regular output. It should be used for passwords or other
   166  	// secret fields. Futrure versions of Terraform may encrypt these
   167  	// values.
   168  	Sensitive bool
   169  }
   170  
   171  // SchemaDiffSuppresFunc is a function which can be used to determine
   172  // whether a detected diff on a schema element is "valid" or not, and
   173  // suppress it from the plan if necessary.
   174  //
   175  // Return true if the diff should be suppressed, false to retain it.
   176  type SchemaDiffSuppressFunc func(k, old, new string, d *ResourceData) bool
   177  
   178  // SchemaDefaultFunc is a function called to return a default value for
   179  // a field.
   180  type SchemaDefaultFunc func() (interface{}, error)
   181  
   182  // EnvDefaultFunc is a helper function that returns the value of the
   183  // given environment variable, if one exists, or the default value
   184  // otherwise.
   185  func EnvDefaultFunc(k string, dv interface{}) SchemaDefaultFunc {
   186  	return func() (interface{}, error) {
   187  		if v := os.Getenv(k); v != "" {
   188  			return v, nil
   189  		}
   190  
   191  		return dv, nil
   192  	}
   193  }
   194  
   195  // MultiEnvDefaultFunc is a helper function that returns the value of the first
   196  // environment variable in the given list that returns a non-empty value. If
   197  // none of the environment variables return a value, the default value is
   198  // returned.
   199  func MultiEnvDefaultFunc(ks []string, dv interface{}) SchemaDefaultFunc {
   200  	return func() (interface{}, error) {
   201  		for _, k := range ks {
   202  			if v := os.Getenv(k); v != "" {
   203  				return v, nil
   204  			}
   205  		}
   206  		return dv, nil
   207  	}
   208  }
   209  
   210  // SchemaSetFunc is a function that must return a unique ID for the given
   211  // element. This unique ID is used to store the element in a hash.
   212  type SchemaSetFunc func(interface{}) int
   213  
   214  // SchemaStateFunc is a function used to convert some type to a string
   215  // to be stored in the state.
   216  type SchemaStateFunc func(interface{}) string
   217  
   218  // SchemaValidateFunc is a function used to validate a single field in the
   219  // schema.
   220  type SchemaValidateFunc func(interface{}, string) ([]string, []error)
   221  
   222  func (s *Schema) GoString() string {
   223  	return fmt.Sprintf("*%#v", *s)
   224  }
   225  
   226  // Returns a default value for this schema by either reading Default or
   227  // evaluating DefaultFunc. If neither of these are defined, returns nil.
   228  func (s *Schema) DefaultValue() (interface{}, error) {
   229  	if s.Default != nil {
   230  		return s.Default, nil
   231  	}
   232  
   233  	if s.DefaultFunc != nil {
   234  		defaultValue, err := s.DefaultFunc()
   235  		if err != nil {
   236  			return nil, fmt.Errorf("error loading default: %s", err)
   237  		}
   238  		return defaultValue, nil
   239  	}
   240  
   241  	return nil, nil
   242  }
   243  
   244  // Returns a zero value for the schema.
   245  func (s *Schema) ZeroValue() interface{} {
   246  	// If it's a set then we'll do a bit of extra work to provide the
   247  	// right hashing function in our empty value.
   248  	if s.Type == TypeSet {
   249  		setFunc := s.Set
   250  		if setFunc == nil {
   251  			// Default set function uses the schema to hash the whole value
   252  			elem := s.Elem
   253  			switch t := elem.(type) {
   254  			case *Schema:
   255  				setFunc = HashSchema(t)
   256  			case *Resource:
   257  				setFunc = HashResource(t)
   258  			default:
   259  				panic("invalid set element type")
   260  			}
   261  		}
   262  		return &Set{F: setFunc}
   263  	} else {
   264  		return s.Type.Zero()
   265  	}
   266  }
   267  
   268  func (s *Schema) finalizeDiff(
   269  	d *terraform.ResourceAttrDiff) *terraform.ResourceAttrDiff {
   270  	if d == nil {
   271  		return d
   272  	}
   273  
   274  	if s.Type == TypeBool {
   275  		normalizeBoolString := func(s string) string {
   276  			switch s {
   277  			case "0":
   278  				return "false"
   279  			case "1":
   280  				return "true"
   281  			}
   282  			return s
   283  		}
   284  		d.Old = normalizeBoolString(d.Old)
   285  		d.New = normalizeBoolString(d.New)
   286  	}
   287  
   288  	if s.ForceNew {
   289  		// Force new, set it to true in the diff
   290  		d.RequiresNew = true
   291  	}
   292  
   293  	if d.NewRemoved {
   294  		return d
   295  	}
   296  
   297  	if s.Computed {
   298  		if d.Old != "" && d.New == "" {
   299  			// This is a computed value with an old value set already,
   300  			// just let it go.
   301  			return nil
   302  		}
   303  
   304  		if d.New == "" {
   305  			// Computed attribute without a new value set
   306  			d.NewComputed = true
   307  		}
   308  	}
   309  
   310  	if s.Sensitive {
   311  		// Set the Sensitive flag so output is hidden in the UI
   312  		d.Sensitive = true
   313  	}
   314  
   315  	return d
   316  }
   317  
   318  // schemaMap is a wrapper that adds nice functions on top of schemas.
   319  type schemaMap map[string]*Schema
   320  
   321  // Data returns a ResourceData for the given schema, state, and diff.
   322  //
   323  // The diff is optional.
   324  func (m schemaMap) Data(
   325  	s *terraform.InstanceState,
   326  	d *terraform.InstanceDiff) (*ResourceData, error) {
   327  	return &ResourceData{
   328  		schema: m,
   329  		state:  s,
   330  		diff:   d,
   331  	}, nil
   332  }
   333  
   334  // Diff returns the diff for a resource given the schema map,
   335  // state, and configuration.
   336  func (m schemaMap) Diff(
   337  	s *terraform.InstanceState,
   338  	c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
   339  	result := new(terraform.InstanceDiff)
   340  	result.Attributes = make(map[string]*terraform.ResourceAttrDiff)
   341  
   342  	// Make sure to mark if the resource is tainted
   343  	if s != nil {
   344  		result.DestroyTainted = s.Tainted
   345  	}
   346  
   347  	d := &ResourceData{
   348  		schema: m,
   349  		state:  s,
   350  		config: c,
   351  	}
   352  
   353  	for k, schema := range m {
   354  		err := m.diff(k, schema, result, d, false)
   355  		if err != nil {
   356  			return nil, err
   357  		}
   358  	}
   359  
   360  	// If the diff requires a new resource, then we recompute the diff
   361  	// so we have the complete new resource diff, and preserve the
   362  	// RequiresNew fields where necessary so the user knows exactly what
   363  	// caused that.
   364  	if result.RequiresNew() {
   365  		// Create the new diff
   366  		result2 := new(terraform.InstanceDiff)
   367  		result2.Attributes = make(map[string]*terraform.ResourceAttrDiff)
   368  
   369  		// Preserve the DestroyTainted flag
   370  		result2.DestroyTainted = result.DestroyTainted
   371  
   372  		// Reset the data to not contain state. We have to call init()
   373  		// again in order to reset the FieldReaders.
   374  		d.state = nil
   375  		d.init()
   376  
   377  		// Perform the diff again
   378  		for k, schema := range m {
   379  			err := m.diff(k, schema, result2, d, false)
   380  			if err != nil {
   381  				return nil, err
   382  			}
   383  		}
   384  
   385  		// Force all the fields to not force a new since we know what we
   386  		// want to force new.
   387  		for k, attr := range result2.Attributes {
   388  			if attr == nil {
   389  				continue
   390  			}
   391  
   392  			if attr.RequiresNew {
   393  				attr.RequiresNew = false
   394  			}
   395  
   396  			if s != nil {
   397  				attr.Old = s.Attributes[k]
   398  			}
   399  		}
   400  
   401  		// Now copy in all the requires new diffs...
   402  		for k, attr := range result.Attributes {
   403  			if attr == nil {
   404  				continue
   405  			}
   406  
   407  			newAttr, ok := result2.Attributes[k]
   408  			if !ok {
   409  				newAttr = attr
   410  			}
   411  
   412  			if attr.RequiresNew {
   413  				newAttr.RequiresNew = true
   414  			}
   415  
   416  			result2.Attributes[k] = newAttr
   417  		}
   418  
   419  		// And set the diff!
   420  		result = result2
   421  	}
   422  
   423  	// Remove any nil diffs just to keep things clean
   424  	for k, v := range result.Attributes {
   425  		if v == nil {
   426  			delete(result.Attributes, k)
   427  		}
   428  	}
   429  
   430  	// Go through and detect all of the ComputedWhens now that we've
   431  	// finished the diff.
   432  	// TODO
   433  
   434  	if result.Empty() {
   435  		// If we don't have any diff elements, just return nil
   436  		return nil, nil
   437  	}
   438  
   439  	return result, nil
   440  }
   441  
   442  // Input implements the terraform.ResourceProvider method by asking
   443  // for input for required configuration keys that don't have a value.
   444  func (m schemaMap) Input(
   445  	input terraform.UIInput,
   446  	c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) {
   447  	keys := make([]string, 0, len(m))
   448  	for k, _ := range m {
   449  		keys = append(keys, k)
   450  	}
   451  	sort.Strings(keys)
   452  
   453  	for _, k := range keys {
   454  		v := m[k]
   455  
   456  		// Skip things that don't require config, if that is even valid
   457  		// for a provider schema.
   458  		if !v.Required && !v.Optional {
   459  			continue
   460  		}
   461  
   462  		// Deprecated fields should never prompt
   463  		if v.Deprecated != "" {
   464  			continue
   465  		}
   466  
   467  		// Skip things that have a value of some sort already
   468  		if _, ok := c.Raw[k]; ok {
   469  			continue
   470  		}
   471  
   472  		// Skip if it has a default value
   473  		defaultValue, err := v.DefaultValue()
   474  		if err != nil {
   475  			return nil, fmt.Errorf("%s: error loading default: %s", k, err)
   476  		}
   477  		if defaultValue != nil {
   478  			continue
   479  		}
   480  
   481  		var value interface{}
   482  		switch v.Type {
   483  		case TypeBool, TypeInt, TypeFloat, TypeSet:
   484  			continue
   485  		case TypeString:
   486  			value, err = m.inputString(input, k, v)
   487  		default:
   488  			panic(fmt.Sprintf("Unknown type for input: %#v", v.Type))
   489  		}
   490  
   491  		if err != nil {
   492  			return nil, fmt.Errorf(
   493  				"%s: %s", k, err)
   494  		}
   495  
   496  		c.Config[k] = value
   497  	}
   498  
   499  	return c, nil
   500  }
   501  
   502  // Validate validates the configuration against this schema mapping.
   503  func (m schemaMap) Validate(c *terraform.ResourceConfig) ([]string, []error) {
   504  	return m.validateObject("", m, c)
   505  }
   506  
   507  // InternalValidate validates the format of this schema. This should be called
   508  // from a unit test (and not in user-path code) to verify that a schema
   509  // is properly built.
   510  func (m schemaMap) InternalValidate(topSchemaMap schemaMap) error {
   511  	if topSchemaMap == nil {
   512  		topSchemaMap = m
   513  	}
   514  	for k, v := range m {
   515  		if v.Type == TypeInvalid {
   516  			return fmt.Errorf("%s: Type must be specified", k)
   517  		}
   518  
   519  		if v.Optional && v.Required {
   520  			return fmt.Errorf("%s: Optional or Required must be set, not both", k)
   521  		}
   522  
   523  		if v.Required && v.Computed {
   524  			return fmt.Errorf("%s: Cannot be both Required and Computed", k)
   525  		}
   526  
   527  		if !v.Required && !v.Optional && !v.Computed {
   528  			return fmt.Errorf("%s: One of optional, required, or computed must be set", k)
   529  		}
   530  
   531  		if v.Computed && v.Default != nil {
   532  			return fmt.Errorf("%s: Default must be nil if computed", k)
   533  		}
   534  
   535  		if v.Required && v.Default != nil {
   536  			return fmt.Errorf("%s: Default cannot be set with Required", k)
   537  		}
   538  
   539  		if len(v.ComputedWhen) > 0 && !v.Computed {
   540  			return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k)
   541  		}
   542  
   543  		if len(v.ConflictsWith) > 0 && v.Required {
   544  			return fmt.Errorf("%s: ConflictsWith cannot be set with Required", k)
   545  		}
   546  
   547  		if len(v.ConflictsWith) > 0 {
   548  			for _, key := range v.ConflictsWith {
   549  				parts := strings.Split(key, ".")
   550  				sm := topSchemaMap
   551  				var target *Schema
   552  				for _, part := range parts {
   553  					// Skip index fields
   554  					if _, err := strconv.Atoi(part); err == nil {
   555  						continue
   556  					}
   557  
   558  					var ok bool
   559  					if target, ok = sm[part]; !ok {
   560  						return fmt.Errorf("%s: ConflictsWith references unknown attribute (%s)", k, key)
   561  					}
   562  
   563  					if subResource, ok := target.Elem.(*Resource); ok {
   564  						sm = schemaMap(subResource.Schema)
   565  					}
   566  				}
   567  				if target == nil {
   568  					return fmt.Errorf("%s: ConflictsWith cannot find target attribute (%s), sm: %#v", k, key, sm)
   569  				}
   570  				if target.Required {
   571  					return fmt.Errorf("%s: ConflictsWith cannot contain Required attribute (%s)", k, key)
   572  				}
   573  
   574  				if target.Computed || len(target.ComputedWhen) > 0 {
   575  					return fmt.Errorf("%s: ConflictsWith cannot contain Computed(When) attribute (%s)", k, key)
   576  				}
   577  			}
   578  		}
   579  
   580  		if v.Type == TypeList || v.Type == TypeSet {
   581  			if v.Elem == nil {
   582  				return fmt.Errorf("%s: Elem must be set for lists", k)
   583  			}
   584  
   585  			if v.Default != nil {
   586  				return fmt.Errorf("%s: Default is not valid for lists or sets", k)
   587  			}
   588  
   589  			if v.Type != TypeSet && v.Set != nil {
   590  				return fmt.Errorf("%s: Set can only be set for TypeSet", k)
   591  			}
   592  
   593  			switch t := v.Elem.(type) {
   594  			case *Resource:
   595  				if err := t.InternalValidate(topSchemaMap, true); err != nil {
   596  					return err
   597  				}
   598  			case *Schema:
   599  				bad := t.Computed || t.Optional || t.Required
   600  				if bad {
   601  					return fmt.Errorf(
   602  						"%s: Elem must have only Type set", k)
   603  				}
   604  			}
   605  		} else {
   606  			if v.MaxItems > 0 || v.MinItems > 0 {
   607  				return fmt.Errorf("%s: MaxItems and MinItems are only supported on lists or sets", k)
   608  			}
   609  		}
   610  
   611  		if v.ValidateFunc != nil {
   612  			switch v.Type {
   613  			case TypeList, TypeSet:
   614  				return fmt.Errorf("ValidateFunc is not yet supported on lists or sets.")
   615  			}
   616  		}
   617  	}
   618  
   619  	return nil
   620  }
   621  
   622  func (m schemaMap) diff(
   623  	k string,
   624  	schema *Schema,
   625  	diff *terraform.InstanceDiff,
   626  	d *ResourceData,
   627  	all bool) error {
   628  
   629  	unsupressedDiff := new(terraform.InstanceDiff)
   630  	unsupressedDiff.Attributes = make(map[string]*terraform.ResourceAttrDiff)
   631  
   632  	var err error
   633  	switch schema.Type {
   634  	case TypeBool, TypeInt, TypeFloat, TypeString:
   635  		err = m.diffString(k, schema, unsupressedDiff, d, all)
   636  	case TypeList:
   637  		err = m.diffList(k, schema, unsupressedDiff, d, all)
   638  	case TypeMap:
   639  		err = m.diffMap(k, schema, unsupressedDiff, d, all)
   640  	case TypeSet:
   641  		err = m.diffSet(k, schema, unsupressedDiff, d, all)
   642  	default:
   643  		err = fmt.Errorf("%s: unknown type %#v", k, schema.Type)
   644  	}
   645  
   646  	for attrK, attrV := range unsupressedDiff.Attributes {
   647  		if schema.DiffSuppressFunc != nil &&
   648  			attrV != nil &&
   649  			schema.DiffSuppressFunc(attrK, attrV.Old, attrV.New, d) {
   650  			continue
   651  		}
   652  
   653  		diff.Attributes[attrK] = attrV
   654  	}
   655  
   656  	return err
   657  }
   658  
   659  func (m schemaMap) diffList(
   660  	k string,
   661  	schema *Schema,
   662  	diff *terraform.InstanceDiff,
   663  	d *ResourceData,
   664  	all bool) error {
   665  	o, n, _, computedList := d.diffChange(k)
   666  	if computedList {
   667  		n = nil
   668  	}
   669  	nSet := n != nil
   670  
   671  	// If we have an old value and no new value is set or will be
   672  	// computed once all variables can be interpolated and we're
   673  	// computed, then nothing has changed.
   674  	if o != nil && n == nil && !computedList && schema.Computed {
   675  		return nil
   676  	}
   677  
   678  	if o == nil {
   679  		o = []interface{}{}
   680  	}
   681  	if n == nil {
   682  		n = []interface{}{}
   683  	}
   684  	if s, ok := o.(*Set); ok {
   685  		o = s.List()
   686  	}
   687  	if s, ok := n.(*Set); ok {
   688  		n = s.List()
   689  	}
   690  	os := o.([]interface{})
   691  	vs := n.([]interface{})
   692  
   693  	// If the new value was set, and the two are equal, then we're done.
   694  	// We have to do this check here because sets might be NOT
   695  	// reflect.DeepEqual so we need to wait until we get the []interface{}
   696  	if !all && nSet && reflect.DeepEqual(os, vs) {
   697  		return nil
   698  	}
   699  
   700  	// Get the counts
   701  	oldLen := len(os)
   702  	newLen := len(vs)
   703  	oldStr := strconv.FormatInt(int64(oldLen), 10)
   704  
   705  	// If the whole list is computed, then say that the # is computed
   706  	if computedList {
   707  		diff.Attributes[k+".#"] = &terraform.ResourceAttrDiff{
   708  			Old:         oldStr,
   709  			NewComputed: true,
   710  		}
   711  		return nil
   712  	}
   713  
   714  	// If the counts are not the same, then record that diff
   715  	changed := oldLen != newLen
   716  	computed := oldLen == 0 && newLen == 0 && schema.Computed
   717  	if changed || computed || all {
   718  		countSchema := &Schema{
   719  			Type:     TypeInt,
   720  			Computed: schema.Computed,
   721  			ForceNew: schema.ForceNew,
   722  		}
   723  
   724  		newStr := ""
   725  		if !computed {
   726  			newStr = strconv.FormatInt(int64(newLen), 10)
   727  		} else {
   728  			oldStr = ""
   729  		}
   730  
   731  		diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{
   732  			Old: oldStr,
   733  			New: newStr,
   734  		})
   735  	}
   736  
   737  	// Figure out the maximum
   738  	maxLen := oldLen
   739  	if newLen > maxLen {
   740  		maxLen = newLen
   741  	}
   742  
   743  	switch t := schema.Elem.(type) {
   744  	case *Resource:
   745  		// This is a complex resource
   746  		for i := 0; i < maxLen; i++ {
   747  			for k2, schema := range t.Schema {
   748  				subK := fmt.Sprintf("%s.%d.%s", k, i, k2)
   749  				err := m.diff(subK, schema, diff, d, all)
   750  				if err != nil {
   751  					return err
   752  				}
   753  			}
   754  		}
   755  	case *Schema:
   756  		// Copy the schema so that we can set Computed/ForceNew from
   757  		// the parent schema (the TypeList).
   758  		t2 := *t
   759  		t2.ForceNew = schema.ForceNew
   760  
   761  		// This is just a primitive element, so go through each and
   762  		// just diff each.
   763  		for i := 0; i < maxLen; i++ {
   764  			subK := fmt.Sprintf("%s.%d", k, i)
   765  			err := m.diff(subK, &t2, diff, d, all)
   766  			if err != nil {
   767  				return err
   768  			}
   769  		}
   770  	default:
   771  		return fmt.Errorf("%s: unknown element type (internal)", k)
   772  	}
   773  
   774  	return nil
   775  }
   776  
   777  func (m schemaMap) diffMap(
   778  	k string,
   779  	schema *Schema,
   780  	diff *terraform.InstanceDiff,
   781  	d *ResourceData,
   782  	all bool) error {
   783  	prefix := k + "."
   784  
   785  	// First get all the values from the state
   786  	var stateMap, configMap map[string]string
   787  	o, n, _, nComputed := d.diffChange(k)
   788  	if err := mapstructure.WeakDecode(o, &stateMap); err != nil {
   789  		return fmt.Errorf("%s: %s", k, err)
   790  	}
   791  	if err := mapstructure.WeakDecode(n, &configMap); err != nil {
   792  		return fmt.Errorf("%s: %s", k, err)
   793  	}
   794  
   795  	// Keep track of whether the state _exists_ at all prior to clearing it
   796  	stateExists := o != nil
   797  
   798  	// Delete any count values, since we don't use those
   799  	delete(configMap, "%")
   800  	delete(stateMap, "%")
   801  
   802  	// Check if the number of elements has changed.
   803  	oldLen, newLen := len(stateMap), len(configMap)
   804  	changed := oldLen != newLen
   805  	if oldLen != 0 && newLen == 0 && schema.Computed {
   806  		changed = false
   807  	}
   808  
   809  	// It is computed if we have no old value, no new value, the schema
   810  	// says it is computed, and it didn't exist in the state before. The
   811  	// last point means: if it existed in the state, even empty, then it
   812  	// has already been computed.
   813  	computed := oldLen == 0 && newLen == 0 && schema.Computed && !stateExists
   814  
   815  	// If the count has changed or we're computed, then add a diff for the
   816  	// count. "nComputed" means that the new value _contains_ a value that
   817  	// is computed. We don't do granular diffs for this yet, so we mark the
   818  	// whole map as computed.
   819  	if changed || computed || nComputed {
   820  		countSchema := &Schema{
   821  			Type:     TypeInt,
   822  			Computed: schema.Computed || nComputed,
   823  			ForceNew: schema.ForceNew,
   824  		}
   825  
   826  		oldStr := strconv.FormatInt(int64(oldLen), 10)
   827  		newStr := ""
   828  		if !computed && !nComputed {
   829  			newStr = strconv.FormatInt(int64(newLen), 10)
   830  		} else {
   831  			oldStr = ""
   832  		}
   833  
   834  		diff.Attributes[k+".%"] = countSchema.finalizeDiff(
   835  			&terraform.ResourceAttrDiff{
   836  				Old: oldStr,
   837  				New: newStr,
   838  			},
   839  		)
   840  	}
   841  
   842  	// If the new map is nil and we're computed, then ignore it.
   843  	if n == nil && schema.Computed {
   844  		return nil
   845  	}
   846  
   847  	// Now we compare, preferring values from the config map
   848  	for k, v := range configMap {
   849  		old, ok := stateMap[k]
   850  		delete(stateMap, k)
   851  
   852  		if old == v && ok && !all {
   853  			continue
   854  		}
   855  
   856  		diff.Attributes[prefix+k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
   857  			Old: old,
   858  			New: v,
   859  		})
   860  	}
   861  	for k, v := range stateMap {
   862  		diff.Attributes[prefix+k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
   863  			Old:        v,
   864  			NewRemoved: true,
   865  		})
   866  	}
   867  
   868  	return nil
   869  }
   870  
   871  func (m schemaMap) diffSet(
   872  	k string,
   873  	schema *Schema,
   874  	diff *terraform.InstanceDiff,
   875  	d *ResourceData,
   876  	all bool) error {
   877  	o, n, _, computedSet := d.diffChange(k)
   878  	if computedSet {
   879  		n = nil
   880  	}
   881  	nSet := n != nil
   882  
   883  	// If we have an old value and no new value is set or will be
   884  	// computed once all variables can be interpolated and we're
   885  	// computed, then nothing has changed.
   886  	if o != nil && n == nil && !computedSet && schema.Computed {
   887  		return nil
   888  	}
   889  
   890  	if o == nil {
   891  		o = schema.ZeroValue().(*Set)
   892  	}
   893  	if n == nil {
   894  		n = schema.ZeroValue().(*Set)
   895  	}
   896  	os := o.(*Set)
   897  	ns := n.(*Set)
   898  
   899  	// If the new value was set, compare the listCode's to determine if
   900  	// the two are equal. Comparing listCode's instead of the actual values
   901  	// is needed because there could be computed values in the set which
   902  	// would result in false positives while comparing.
   903  	if !all && nSet && reflect.DeepEqual(os.listCode(), ns.listCode()) {
   904  		return nil
   905  	}
   906  
   907  	// Get the counts
   908  	oldLen := os.Len()
   909  	newLen := ns.Len()
   910  	oldStr := strconv.Itoa(oldLen)
   911  	newStr := strconv.Itoa(newLen)
   912  
   913  	// If the set computed then say that the # is computed
   914  	if computedSet || schema.Computed && !nSet {
   915  		// If # already exists, equals 0 and no new set is supplied, there
   916  		// is nothing to record in the diff
   917  		count, ok := d.GetOk(k + ".#")
   918  		if ok && count.(int) == 0 && !nSet && !computedSet {
   919  			return nil
   920  		}
   921  
   922  		// Set the count but make sure that if # does not exist, we don't
   923  		// use the zeroed value
   924  		countStr := strconv.Itoa(count.(int))
   925  		if !ok {
   926  			countStr = ""
   927  		}
   928  
   929  		diff.Attributes[k+".#"] = &terraform.ResourceAttrDiff{
   930  			Old:         countStr,
   931  			NewComputed: true,
   932  		}
   933  		return nil
   934  	}
   935  
   936  	// If the counts are not the same, then record that diff
   937  	changed := oldLen != newLen
   938  	if changed || all {
   939  		countSchema := &Schema{
   940  			Type:     TypeInt,
   941  			Computed: schema.Computed,
   942  			ForceNew: schema.ForceNew,
   943  		}
   944  
   945  		diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{
   946  			Old: oldStr,
   947  			New: newStr,
   948  		})
   949  	}
   950  
   951  	// Build the list of codes that will make up our set. This is the
   952  	// removed codes as well as all the codes in the new codes.
   953  	codes := make([][]string, 2)
   954  	codes[0] = os.Difference(ns).listCode()
   955  	codes[1] = ns.listCode()
   956  	for _, list := range codes {
   957  		for _, code := range list {
   958  			switch t := schema.Elem.(type) {
   959  			case *Resource:
   960  				// This is a complex resource
   961  				for k2, schema := range t.Schema {
   962  					subK := fmt.Sprintf("%s.%s.%s", k, code, k2)
   963  					err := m.diff(subK, schema, diff, d, true)
   964  					if err != nil {
   965  						return err
   966  					}
   967  				}
   968  			case *Schema:
   969  				// Copy the schema so that we can set Computed/ForceNew from
   970  				// the parent schema (the TypeSet).
   971  				t2 := *t
   972  				t2.ForceNew = schema.ForceNew
   973  
   974  				// This is just a primitive element, so go through each and
   975  				// just diff each.
   976  				subK := fmt.Sprintf("%s.%s", k, code)
   977  				err := m.diff(subK, &t2, diff, d, true)
   978  				if err != nil {
   979  					return err
   980  				}
   981  			default:
   982  				return fmt.Errorf("%s: unknown element type (internal)", k)
   983  			}
   984  		}
   985  	}
   986  
   987  	return nil
   988  }
   989  
   990  func (m schemaMap) diffString(
   991  	k string,
   992  	schema *Schema,
   993  	diff *terraform.InstanceDiff,
   994  	d *ResourceData,
   995  	all bool) error {
   996  	var originalN interface{}
   997  	var os, ns string
   998  	o, n, _, computed := d.diffChange(k)
   999  	if schema.StateFunc != nil && n != nil {
  1000  		originalN = n
  1001  		n = schema.StateFunc(n)
  1002  	}
  1003  	nraw := n
  1004  	if nraw == nil && o != nil {
  1005  		nraw = schema.Type.Zero()
  1006  	}
  1007  	if err := mapstructure.WeakDecode(o, &os); err != nil {
  1008  		return fmt.Errorf("%s: %s", k, err)
  1009  	}
  1010  	if err := mapstructure.WeakDecode(nraw, &ns); err != nil {
  1011  		return fmt.Errorf("%s: %s", k, err)
  1012  	}
  1013  
  1014  	if os == ns && !all {
  1015  		// They're the same value. If there old value is not blank or we
  1016  		// have an ID, then return right away since we're already setup.
  1017  		if os != "" || d.Id() != "" {
  1018  			return nil
  1019  		}
  1020  
  1021  		// Otherwise, only continue if we're computed
  1022  		if !schema.Computed && !computed {
  1023  			return nil
  1024  		}
  1025  	}
  1026  
  1027  	removed := false
  1028  	if o != nil && n == nil {
  1029  		removed = true
  1030  	}
  1031  	if removed && schema.Computed {
  1032  		return nil
  1033  	}
  1034  
  1035  	diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
  1036  		Old:         os,
  1037  		New:         ns,
  1038  		NewExtra:    originalN,
  1039  		NewRemoved:  removed,
  1040  		NewComputed: computed,
  1041  	})
  1042  
  1043  	return nil
  1044  }
  1045  
  1046  func (m schemaMap) inputString(
  1047  	input terraform.UIInput,
  1048  	k string,
  1049  	schema *Schema) (interface{}, error) {
  1050  	result, err := input.Input(&terraform.InputOpts{
  1051  		Id:          k,
  1052  		Query:       k,
  1053  		Description: schema.Description,
  1054  		Default:     schema.InputDefault,
  1055  	})
  1056  
  1057  	return result, err
  1058  }
  1059  
  1060  func (m schemaMap) validate(
  1061  	k string,
  1062  	schema *Schema,
  1063  	c *terraform.ResourceConfig) ([]string, []error) {
  1064  	raw, ok := c.Get(k)
  1065  	if !ok && schema.DefaultFunc != nil {
  1066  		// We have a dynamic default. Check if we have a value.
  1067  		var err error
  1068  		raw, err = schema.DefaultFunc()
  1069  		if err != nil {
  1070  			return nil, []error{fmt.Errorf(
  1071  				"%q, error loading default: %s", k, err)}
  1072  		}
  1073  
  1074  		// We're okay as long as we had a value set
  1075  		ok = raw != nil
  1076  	}
  1077  	if !ok {
  1078  		if schema.Required {
  1079  			return nil, []error{fmt.Errorf(
  1080  				"%q: required field is not set", k)}
  1081  		}
  1082  
  1083  		return nil, nil
  1084  	}
  1085  
  1086  	if !schema.Required && !schema.Optional {
  1087  		// This is a computed-only field
  1088  		return nil, []error{fmt.Errorf(
  1089  			"%q: this field cannot be set", k)}
  1090  	}
  1091  
  1092  	err := m.validateConflictingAttributes(k, schema, c)
  1093  	if err != nil {
  1094  		return nil, []error{err}
  1095  	}
  1096  
  1097  	return m.validateType(k, raw, schema, c)
  1098  }
  1099  
  1100  func (m schemaMap) validateConflictingAttributes(
  1101  	k string,
  1102  	schema *Schema,
  1103  	c *terraform.ResourceConfig) error {
  1104  
  1105  	if len(schema.ConflictsWith) == 0 {
  1106  		return nil
  1107  	}
  1108  
  1109  	for _, conflicting_key := range schema.ConflictsWith {
  1110  		if value, ok := c.Get(conflicting_key); ok {
  1111  			return fmt.Errorf(
  1112  				"%q: conflicts with %s (%#v)", k, conflicting_key, value)
  1113  		}
  1114  	}
  1115  
  1116  	return nil
  1117  }
  1118  
  1119  func (m schemaMap) validateList(
  1120  	k string,
  1121  	raw interface{},
  1122  	schema *Schema,
  1123  	c *terraform.ResourceConfig) ([]string, []error) {
  1124  	// We use reflection to verify the slice because you can't
  1125  	// case to []interface{} unless the slice is exactly that type.
  1126  	rawV := reflect.ValueOf(raw)
  1127  	if rawV.Kind() != reflect.Slice {
  1128  		return nil, []error{fmt.Errorf(
  1129  			"%s: should be a list", k)}
  1130  	}
  1131  
  1132  	// Validate length
  1133  	if schema.MaxItems > 0 && rawV.Len() > schema.MaxItems {
  1134  		return nil, []error{fmt.Errorf(
  1135  			"%s: attribute supports %d item maximum, config has %d declared", k, schema.MaxItems, rawV.Len())}
  1136  	}
  1137  
  1138  	if schema.MinItems > 0 && rawV.Len() < schema.MinItems {
  1139  		return nil, []error{fmt.Errorf(
  1140  			"%s: attribute supports %d item as a minimum, config has %d declared", k, schema.MinItems, rawV.Len())}
  1141  	}
  1142  
  1143  	// Now build the []interface{}
  1144  	raws := make([]interface{}, rawV.Len())
  1145  	for i, _ := range raws {
  1146  		raws[i] = rawV.Index(i).Interface()
  1147  	}
  1148  
  1149  	var ws []string
  1150  	var es []error
  1151  	for i, raw := range raws {
  1152  		key := fmt.Sprintf("%s.%d", k, i)
  1153  
  1154  		var ws2 []string
  1155  		var es2 []error
  1156  		switch t := schema.Elem.(type) {
  1157  		case *Resource:
  1158  			// This is a sub-resource
  1159  			ws2, es2 = m.validateObject(key, t.Schema, c)
  1160  		case *Schema:
  1161  			ws2, es2 = m.validateType(key, raw, t, c)
  1162  		}
  1163  
  1164  		if len(ws2) > 0 {
  1165  			ws = append(ws, ws2...)
  1166  		}
  1167  		if len(es2) > 0 {
  1168  			es = append(es, es2...)
  1169  		}
  1170  	}
  1171  
  1172  	return ws, es
  1173  }
  1174  
  1175  func (m schemaMap) validateMap(
  1176  	k string,
  1177  	raw interface{},
  1178  	schema *Schema,
  1179  	c *terraform.ResourceConfig) ([]string, []error) {
  1180  	// We use reflection to verify the slice because you can't
  1181  	// case to []interface{} unless the slice is exactly that type.
  1182  	rawV := reflect.ValueOf(raw)
  1183  	switch rawV.Kind() {
  1184  	case reflect.String:
  1185  		// If raw and reified are equal, this is a string and should
  1186  		// be rejected.
  1187  		reified, reifiedOk := c.Get(k)
  1188  		if reifiedOk && raw == reified && !c.IsComputed(k) {
  1189  			return nil, []error{fmt.Errorf("%s: should be a map", k)}
  1190  		}
  1191  		// Otherwise it's likely raw is an interpolation.
  1192  		return nil, nil
  1193  	case reflect.Map:
  1194  	case reflect.Slice:
  1195  	default:
  1196  		return nil, []error{fmt.Errorf("%s: should be a map", k)}
  1197  	}
  1198  
  1199  	// If it is not a slice, it is valid
  1200  	if rawV.Kind() != reflect.Slice {
  1201  		return nil, nil
  1202  	}
  1203  
  1204  	// It is a slice, verify that all the elements are maps
  1205  	raws := make([]interface{}, rawV.Len())
  1206  	for i, _ := range raws {
  1207  		raws[i] = rawV.Index(i).Interface()
  1208  	}
  1209  
  1210  	for _, raw := range raws {
  1211  		v := reflect.ValueOf(raw)
  1212  		if v.Kind() != reflect.Map {
  1213  			return nil, []error{fmt.Errorf(
  1214  				"%s: should be a map", k)}
  1215  		}
  1216  	}
  1217  
  1218  	if schema.ValidateFunc != nil {
  1219  		validatableMap := make(map[string]interface{})
  1220  		for _, raw := range raws {
  1221  			for k, v := range raw.(map[string]interface{}) {
  1222  				validatableMap[k] = v
  1223  			}
  1224  		}
  1225  
  1226  		return schema.ValidateFunc(validatableMap, k)
  1227  	}
  1228  
  1229  	return nil, nil
  1230  }
  1231  
  1232  func (m schemaMap) validateObject(
  1233  	k string,
  1234  	schema map[string]*Schema,
  1235  	c *terraform.ResourceConfig) ([]string, []error) {
  1236  	raw, _ := c.GetRaw(k)
  1237  	if _, ok := raw.(map[string]interface{}); !ok {
  1238  		return nil, []error{fmt.Errorf(
  1239  			"%s: expected object, got %s",
  1240  			k, reflect.ValueOf(raw).Kind())}
  1241  	}
  1242  
  1243  	var ws []string
  1244  	var es []error
  1245  	for subK, s := range schema {
  1246  		key := subK
  1247  		if k != "" {
  1248  			key = fmt.Sprintf("%s.%s", k, subK)
  1249  		}
  1250  
  1251  		ws2, es2 := m.validate(key, s, c)
  1252  		if len(ws2) > 0 {
  1253  			ws = append(ws, ws2...)
  1254  		}
  1255  		if len(es2) > 0 {
  1256  			es = append(es, es2...)
  1257  		}
  1258  	}
  1259  
  1260  	// Detect any extra/unknown keys and report those as errors.
  1261  	if m, ok := raw.(map[string]interface{}); ok {
  1262  		for subk, _ := range m {
  1263  			if _, ok := schema[subk]; !ok {
  1264  				es = append(es, fmt.Errorf(
  1265  					"%s: invalid or unknown key: %s", k, subk))
  1266  			}
  1267  		}
  1268  	}
  1269  
  1270  	return ws, es
  1271  }
  1272  
  1273  func (m schemaMap) validatePrimitive(
  1274  	k string,
  1275  	raw interface{},
  1276  	schema *Schema,
  1277  	c *terraform.ResourceConfig) ([]string, []error) {
  1278  
  1279  	// Catch if the user gave a complex type where a primitive was
  1280  	// expected, so we can return a friendly error message that
  1281  	// doesn't contain Go type system terminology.
  1282  	switch reflect.ValueOf(raw).Type().Kind() {
  1283  	case reflect.Slice:
  1284  		return nil, []error{
  1285  			fmt.Errorf("%s must be a single value, not a list", k),
  1286  		}
  1287  	case reflect.Map:
  1288  		return nil, []error{
  1289  			fmt.Errorf("%s must be a single value, not a map", k),
  1290  		}
  1291  	default: // ok
  1292  	}
  1293  
  1294  	if c.IsComputed(k) {
  1295  		// If the key is being computed, then it is not an error as
  1296  		// long as it's not a slice or map.
  1297  		return nil, nil
  1298  	}
  1299  
  1300  	var decoded interface{}
  1301  	switch schema.Type {
  1302  	case TypeBool:
  1303  		// Verify that we can parse this as the correct type
  1304  		var n bool
  1305  		if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1306  			return nil, []error{err}
  1307  		}
  1308  		decoded = n
  1309  	case TypeInt:
  1310  		// Verify that we can parse this as an int
  1311  		var n int
  1312  		if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1313  			return nil, []error{err}
  1314  		}
  1315  		decoded = n
  1316  	case TypeFloat:
  1317  		// Verify that we can parse this as an int
  1318  		var n float64
  1319  		if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1320  			return nil, []error{err}
  1321  		}
  1322  		decoded = n
  1323  	case TypeString:
  1324  		// Verify that we can parse this as a string
  1325  		var n string
  1326  		if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1327  			return nil, []error{err}
  1328  		}
  1329  		decoded = n
  1330  	default:
  1331  		panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
  1332  	}
  1333  
  1334  	if schema.ValidateFunc != nil {
  1335  		return schema.ValidateFunc(decoded, k)
  1336  	}
  1337  
  1338  	return nil, nil
  1339  }
  1340  
  1341  func (m schemaMap) validateType(
  1342  	k string,
  1343  	raw interface{},
  1344  	schema *Schema,
  1345  	c *terraform.ResourceConfig) ([]string, []error) {
  1346  	var ws []string
  1347  	var es []error
  1348  	switch schema.Type {
  1349  	case TypeSet, TypeList:
  1350  		ws, es = m.validateList(k, raw, schema, c)
  1351  	case TypeMap:
  1352  		ws, es = m.validateMap(k, raw, schema, c)
  1353  	default:
  1354  		ws, es = m.validatePrimitive(k, raw, schema, c)
  1355  	}
  1356  
  1357  	if schema.Deprecated != "" {
  1358  		ws = append(ws, fmt.Sprintf(
  1359  			"%q: [DEPRECATED] %s", k, schema.Deprecated))
  1360  	}
  1361  
  1362  	if schema.Removed != "" {
  1363  		es = append(es, fmt.Errorf(
  1364  			"%q: [REMOVED] %s", k, schema.Removed))
  1365  	}
  1366  
  1367  	return ws, es
  1368  }
  1369  
  1370  // Zero returns the zero value for a type.
  1371  func (t ValueType) Zero() interface{} {
  1372  	switch t {
  1373  	case TypeInvalid:
  1374  		return nil
  1375  	case TypeBool:
  1376  		return false
  1377  	case TypeInt:
  1378  		return 0
  1379  	case TypeFloat:
  1380  		return 0.0
  1381  	case TypeString:
  1382  		return ""
  1383  	case TypeList:
  1384  		return []interface{}{}
  1385  	case TypeMap:
  1386  		return map[string]interface{}{}
  1387  	case TypeSet:
  1388  		return new(Set)
  1389  	case typeObject:
  1390  		return map[string]interface{}{}
  1391  	default:
  1392  		panic(fmt.Sprintf("unknown type %s", t))
  1393  	}
  1394  }