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