github.com/rmenn/terraform@v0.3.8-0.20150225065417-fc84b3a78802/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  	Elem interface{}
   103  
   104  	// The following fields are only valid for a TypeSet type.
   105  	//
   106  	// Set defines a function to determine the unique ID of an item so that
   107  	// a proper set can be built.
   108  	Set SchemaSetFunc
   109  
   110  	// ComputedWhen is a set of queries on the configuration. Whenever any
   111  	// of these things is changed, it will require a recompute (this requires
   112  	// that Computed is set to true).
   113  	//
   114  	// NOTE: This currently does not work.
   115  	ComputedWhen []string
   116  }
   117  
   118  // SchemaDefaultFunc is a function called to return a default value for
   119  // a field.
   120  type SchemaDefaultFunc func() (interface{}, error)
   121  
   122  // EnvDefaultFunc is a helper function that returns the value of the
   123  // given environment variable, if one exists, or the default value
   124  // otherwise.
   125  func EnvDefaultFunc(k string, dv interface{}) SchemaDefaultFunc {
   126  	return func() (interface{}, error) {
   127  		if v := os.Getenv(k); v != "" {
   128  			return v, nil
   129  		}
   130  
   131  		return dv, nil
   132  	}
   133  }
   134  
   135  // MultiEnvDefaultFunc is a helper function that returns the value of the first
   136  // environment variable in the given list that returns a non-empty value. If
   137  // none of the environment variables return a value, the default value is
   138  // returned.
   139  func MultiEnvDefaultFunc(ks []string, dv interface{}) SchemaDefaultFunc {
   140  	return func() (interface{}, error) {
   141  		for _, k := range ks {
   142  			if v := os.Getenv(k); v != "" {
   143  				return v, nil
   144  			}
   145  		}
   146  		return dv, nil
   147  	}
   148  }
   149  
   150  // SchemaSetFunc is a function that must return a unique ID for the given
   151  // element. This unique ID is used to store the element in a hash.
   152  type SchemaSetFunc func(interface{}) int
   153  
   154  // SchemaStateFunc is a function used to convert some type to a string
   155  // to be stored in the state.
   156  type SchemaStateFunc func(interface{}) string
   157  
   158  func (s *Schema) GoString() string {
   159  	return fmt.Sprintf("*%#v", *s)
   160  }
   161  
   162  // Returns a default value for this schema by either reading Default or
   163  // evaluating DefaultFunc. If neither of these are defined, returns nil.
   164  func (s *Schema) DefaultValue() (interface{}, error) {
   165  	if s.Default != nil {
   166  		return s.Default, nil
   167  	}
   168  
   169  	if s.DefaultFunc != nil {
   170  		defaultValue, err := s.DefaultFunc()
   171  		if err != nil {
   172  			return nil, fmt.Errorf("error loading default: %s", err)
   173  		}
   174  		return defaultValue, nil
   175  	}
   176  
   177  	return nil, nil
   178  }
   179  
   180  func (s *Schema) finalizeDiff(
   181  	d *terraform.ResourceAttrDiff) *terraform.ResourceAttrDiff {
   182  	if d == nil {
   183  		return d
   184  	}
   185  
   186  	if d.NewRemoved {
   187  		return d
   188  	}
   189  
   190  	if s.Computed {
   191  		if d.Old != "" && d.New == "" {
   192  			// This is a computed value with an old value set already,
   193  			// just let it go.
   194  			return nil
   195  		}
   196  
   197  		if d.New == "" {
   198  			// Computed attribute without a new value set
   199  			d.NewComputed = true
   200  		}
   201  	}
   202  
   203  	if s.ForceNew {
   204  		// Force new, set it to true in the diff
   205  		d.RequiresNew = true
   206  	}
   207  
   208  	return d
   209  }
   210  
   211  // schemaMap is a wrapper that adds nice functions on top of schemas.
   212  type schemaMap map[string]*Schema
   213  
   214  // Data returns a ResourceData for the given schema, state, and diff.
   215  //
   216  // The diff is optional.
   217  func (m schemaMap) Data(
   218  	s *terraform.InstanceState,
   219  	d *terraform.InstanceDiff) (*ResourceData, error) {
   220  	return &ResourceData{
   221  		schema: m,
   222  		state:  s,
   223  		diff:   d,
   224  	}, nil
   225  }
   226  
   227  // Diff returns the diff for a resource given the schema map,
   228  // state, and configuration.
   229  func (m schemaMap) Diff(
   230  	s *terraform.InstanceState,
   231  	c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
   232  	result := new(terraform.InstanceDiff)
   233  	result.Attributes = make(map[string]*terraform.ResourceAttrDiff)
   234  
   235  	d := &ResourceData{
   236  		schema: m,
   237  		state:  s,
   238  		config: c,
   239  	}
   240  
   241  	for k, schema := range m {
   242  		err := m.diff(k, schema, result, d, false)
   243  		if err != nil {
   244  			return nil, err
   245  		}
   246  	}
   247  
   248  	// If the diff requires a new resource, then we recompute the diff
   249  	// so we have the complete new resource diff, and preserve the
   250  	// RequiresNew fields where necessary so the user knows exactly what
   251  	// caused that.
   252  	if result.RequiresNew() {
   253  		// Create the new diff
   254  		result2 := new(terraform.InstanceDiff)
   255  		result2.Attributes = make(map[string]*terraform.ResourceAttrDiff)
   256  
   257  		// Reset the data to not contain state. We have to call init()
   258  		// again in order to reset the FieldReaders.
   259  		d.state = nil
   260  		d.init()
   261  
   262  		// Perform the diff again
   263  		for k, schema := range m {
   264  			err := m.diff(k, schema, result2, d, false)
   265  			if err != nil {
   266  				return nil, err
   267  			}
   268  		}
   269  
   270  		// Force all the fields to not force a new since we know what we
   271  		// want to force new.
   272  		for k, attr := range result2.Attributes {
   273  			if attr == nil {
   274  				continue
   275  			}
   276  
   277  			if attr.RequiresNew {
   278  				attr.RequiresNew = false
   279  			}
   280  
   281  			if s != nil {
   282  				attr.Old = s.Attributes[k]
   283  			}
   284  		}
   285  
   286  		// Now copy in all the requires new diffs...
   287  		for k, attr := range result.Attributes {
   288  			if attr == nil {
   289  				continue
   290  			}
   291  
   292  			newAttr, ok := result2.Attributes[k]
   293  			if !ok {
   294  				newAttr = attr
   295  			}
   296  
   297  			if attr.RequiresNew {
   298  				newAttr.RequiresNew = true
   299  			}
   300  
   301  			result2.Attributes[k] = newAttr
   302  		}
   303  
   304  		// And set the diff!
   305  		result = result2
   306  	}
   307  
   308  	// Remove any nil diffs just to keep things clean
   309  	for k, v := range result.Attributes {
   310  		if v == nil {
   311  			delete(result.Attributes, k)
   312  		}
   313  	}
   314  
   315  	// Go through and detect all of the ComputedWhens now that we've
   316  	// finished the diff.
   317  	// TODO
   318  
   319  	if result.Empty() {
   320  		// If we don't have any diff elements, just return nil
   321  		return nil, nil
   322  	}
   323  
   324  	return result, nil
   325  }
   326  
   327  // Input implements the terraform.ResourceProvider method by asking
   328  // for input for required configuration keys that don't have a value.
   329  func (m schemaMap) Input(
   330  	input terraform.UIInput,
   331  	c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) {
   332  	keys := make([]string, 0, len(m))
   333  	for k, _ := range m {
   334  		keys = append(keys, k)
   335  	}
   336  	sort.Strings(keys)
   337  
   338  	for _, k := range keys {
   339  		v := m[k]
   340  
   341  		// Skip things that don't require config, if that is even valid
   342  		// for a provider schema.
   343  		if !v.Required && !v.Optional {
   344  			continue
   345  		}
   346  
   347  		// Skip things that have a value of some sort already
   348  		if _, ok := c.Raw[k]; ok {
   349  			continue
   350  		}
   351  
   352  		// Skip if it has a default value
   353  		defaultValue, err := v.DefaultValue()
   354  		if err != nil {
   355  			return nil, fmt.Errorf("%s: error loading default: %s", k, err)
   356  		}
   357  		if defaultValue != nil {
   358  			continue
   359  		}
   360  
   361  		var value interface{}
   362  		switch v.Type {
   363  		case TypeBool:
   364  			fallthrough
   365  		case TypeInt:
   366  			fallthrough
   367  		case TypeFloat:
   368  			fallthrough
   369  		case TypeString:
   370  			value, err = m.inputString(input, k, v)
   371  		default:
   372  			panic(fmt.Sprintf("Unknown type for input: %#v", v.Type))
   373  		}
   374  
   375  		if err != nil {
   376  			return nil, fmt.Errorf(
   377  				"%s: %s", k, err)
   378  		}
   379  
   380  		c.Config[k] = value
   381  	}
   382  
   383  	return c, nil
   384  }
   385  
   386  // Validate validates the configuration against this schema mapping.
   387  func (m schemaMap) Validate(c *terraform.ResourceConfig) ([]string, []error) {
   388  	return m.validateObject("", m, c)
   389  }
   390  
   391  // InternalValidate validates the format of this schema. This should be called
   392  // from a unit test (and not in user-path code) to verify that a schema
   393  // is properly built.
   394  func (m schemaMap) InternalValidate() error {
   395  	for k, v := range m {
   396  		if v.Type == TypeInvalid {
   397  			return fmt.Errorf("%s: Type must be specified", k)
   398  		}
   399  
   400  		if v.Optional && v.Required {
   401  			return fmt.Errorf("%s: Optional or Required must be set, not both", k)
   402  		}
   403  
   404  		if v.Required && v.Computed {
   405  			return fmt.Errorf("%s: Cannot be both Required and Computed", k)
   406  		}
   407  
   408  		if !v.Required && !v.Optional && !v.Computed {
   409  			return fmt.Errorf("%s: One of optional, required, or computed must be set", k)
   410  		}
   411  
   412  		if v.Computed && v.Default != nil {
   413  			return fmt.Errorf("%s: Default must be nil if computed", k)
   414  		}
   415  
   416  		if v.Required && v.Default != nil {
   417  			return fmt.Errorf("%s: Default cannot be set with Required", k)
   418  		}
   419  
   420  		if len(v.ComputedWhen) > 0 && !v.Computed {
   421  			return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k)
   422  		}
   423  
   424  		if v.Type == TypeList || v.Type == TypeSet {
   425  			if v.Elem == nil {
   426  				return fmt.Errorf("%s: Elem must be set for lists", k)
   427  			}
   428  
   429  			if v.Default != nil {
   430  				return fmt.Errorf("%s: Default is not valid for lists or sets", k)
   431  			}
   432  
   433  			if v.Type == TypeList && v.Set != nil {
   434  				return fmt.Errorf("%s: Set can only be set for TypeSet", k)
   435  			} else if v.Type == TypeSet && v.Set == nil {
   436  				return fmt.Errorf("%s: Set must be set", k)
   437  			}
   438  
   439  			switch t := v.Elem.(type) {
   440  			case *Resource:
   441  				if err := t.InternalValidate(); err != nil {
   442  					return err
   443  				}
   444  			case *Schema:
   445  				bad := t.Computed || t.Optional || t.Required
   446  				if bad {
   447  					return fmt.Errorf(
   448  						"%s: Elem must have only Type set", k)
   449  				}
   450  			}
   451  		}
   452  	}
   453  
   454  	return nil
   455  }
   456  
   457  func (m schemaMap) diff(
   458  	k string,
   459  	schema *Schema,
   460  	diff *terraform.InstanceDiff,
   461  	d *ResourceData,
   462  	all bool) error {
   463  	var err error
   464  	switch schema.Type {
   465  	case TypeBool:
   466  		fallthrough
   467  	case TypeInt:
   468  		fallthrough
   469  	case TypeFloat:
   470  		fallthrough
   471  	case TypeString:
   472  		err = m.diffString(k, schema, diff, d, all)
   473  	case TypeList:
   474  		err = m.diffList(k, schema, diff, d, all)
   475  	case TypeMap:
   476  		err = m.diffMap(k, schema, diff, d, all)
   477  	case TypeSet:
   478  		err = m.diffSet(k, schema, diff, d, all)
   479  	default:
   480  		err = fmt.Errorf("%s: unknown type %#v", k, schema.Type)
   481  	}
   482  
   483  	return err
   484  }
   485  
   486  func (m schemaMap) diffList(
   487  	k string,
   488  	schema *Schema,
   489  	diff *terraform.InstanceDiff,
   490  	d *ResourceData,
   491  	all bool) error {
   492  	o, n, _, computedList := d.diffChange(k)
   493  	if computedList {
   494  		n = nil
   495  	}
   496  	nSet := n != nil
   497  
   498  	// If we have an old value and no new value is set or will be
   499  	// computed once all variables can be interpolated and we're
   500  	// computed, then nothing has changed.
   501  	if o != nil && n == nil && !computedList && schema.Computed {
   502  		return nil
   503  	}
   504  
   505  	if o == nil {
   506  		o = []interface{}{}
   507  	}
   508  	if n == nil {
   509  		n = []interface{}{}
   510  	}
   511  	if s, ok := o.(*Set); ok {
   512  		o = s.List()
   513  	}
   514  	if s, ok := n.(*Set); ok {
   515  		n = s.List()
   516  	}
   517  	os := o.([]interface{})
   518  	vs := n.([]interface{})
   519  
   520  	// If the new value was set, and the two are equal, then we're done.
   521  	// We have to do this check here because sets might be NOT
   522  	// reflect.DeepEqual so we need to wait until we get the []interface{}
   523  	if !all && nSet && reflect.DeepEqual(os, vs) {
   524  		return nil
   525  	}
   526  
   527  	// Get the counts
   528  	oldLen := len(os)
   529  	newLen := len(vs)
   530  	oldStr := strconv.FormatInt(int64(oldLen), 10)
   531  
   532  	// If the whole list is computed, then say that the # is computed
   533  	if computedList {
   534  		diff.Attributes[k+".#"] = &terraform.ResourceAttrDiff{
   535  			Old:         oldStr,
   536  			NewComputed: true,
   537  		}
   538  		return nil
   539  	}
   540  
   541  	// If the counts are not the same, then record that diff
   542  	changed := oldLen != newLen
   543  	computed := oldLen == 0 && newLen == 0 && schema.Computed
   544  	if changed || computed || all {
   545  		countSchema := &Schema{
   546  			Type:     TypeInt,
   547  			Computed: schema.Computed,
   548  			ForceNew: schema.ForceNew,
   549  		}
   550  
   551  		newStr := ""
   552  		if !computed {
   553  			newStr = strconv.FormatInt(int64(newLen), 10)
   554  		} else {
   555  			oldStr = ""
   556  		}
   557  
   558  		diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{
   559  			Old: oldStr,
   560  			New: newStr,
   561  		})
   562  	}
   563  
   564  	// Figure out the maximum
   565  	maxLen := oldLen
   566  	if newLen > maxLen {
   567  		maxLen = newLen
   568  	}
   569  
   570  	switch t := schema.Elem.(type) {
   571  	case *Resource:
   572  		// This is a complex resource
   573  		for i := 0; i < maxLen; i++ {
   574  			for k2, schema := range t.Schema {
   575  				subK := fmt.Sprintf("%s.%d.%s", k, i, k2)
   576  				err := m.diff(subK, schema, diff, d, all)
   577  				if err != nil {
   578  					return err
   579  				}
   580  			}
   581  		}
   582  	case *Schema:
   583  		// Copy the schema so that we can set Computed/ForceNew from
   584  		// the parent schema (the TypeList).
   585  		t2 := *t
   586  		t2.ForceNew = schema.ForceNew
   587  
   588  		// This is just a primitive element, so go through each and
   589  		// just diff each.
   590  		for i := 0; i < maxLen; i++ {
   591  			subK := fmt.Sprintf("%s.%d", k, i)
   592  			err := m.diff(subK, &t2, diff, d, all)
   593  			if err != nil {
   594  				return err
   595  			}
   596  		}
   597  	default:
   598  		return fmt.Errorf("%s: unknown element type (internal)", k)
   599  	}
   600  
   601  	return nil
   602  }
   603  
   604  func (m schemaMap) diffMap(
   605  	k string,
   606  	schema *Schema,
   607  	diff *terraform.InstanceDiff,
   608  	d *ResourceData,
   609  	all bool) error {
   610  	prefix := k + "."
   611  
   612  	// First get all the values from the state
   613  	var stateMap, configMap map[string]string
   614  	o, n, _, _ := d.diffChange(k)
   615  	if err := mapstructure.WeakDecode(o, &stateMap); err != nil {
   616  		return fmt.Errorf("%s: %s", k, err)
   617  	}
   618  	if err := mapstructure.WeakDecode(n, &configMap); err != nil {
   619  		return fmt.Errorf("%s: %s", k, err)
   620  	}
   621  
   622  	// Delete any count values, since we don't use those
   623  	delete(configMap, "#")
   624  	delete(stateMap, "#")
   625  
   626  	// Check if the number of elements has changed. If we're computing
   627  	// a list and there isn't a config, then it hasn't changed.
   628  	oldLen, newLen := len(stateMap), len(configMap)
   629  	changed := oldLen != newLen
   630  	if oldLen != 0 && newLen == 0 && schema.Computed {
   631  		changed = false
   632  	}
   633  	computed := oldLen == 0 && newLen == 0 && schema.Computed
   634  	if changed || computed {
   635  		countSchema := &Schema{
   636  			Type:     TypeInt,
   637  			Computed: schema.Computed,
   638  			ForceNew: schema.ForceNew,
   639  		}
   640  
   641  		oldStr := strconv.FormatInt(int64(oldLen), 10)
   642  		newStr := ""
   643  		if !computed {
   644  			newStr = strconv.FormatInt(int64(newLen), 10)
   645  		} else {
   646  			oldStr = ""
   647  		}
   648  
   649  		diff.Attributes[k+".#"] = countSchema.finalizeDiff(
   650  			&terraform.ResourceAttrDiff{
   651  				Old: oldStr,
   652  				New: newStr,
   653  			},
   654  		)
   655  	}
   656  
   657  	// If the new map is nil and we're computed, then ignore it.
   658  	if n == nil && schema.Computed {
   659  		return nil
   660  	}
   661  
   662  	// Now we compare, preferring values from the config map
   663  	for k, v := range configMap {
   664  		old, ok := stateMap[k]
   665  		delete(stateMap, k)
   666  
   667  		if old == v && ok && !all {
   668  			continue
   669  		}
   670  
   671  		diff.Attributes[prefix+k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
   672  			Old: old,
   673  			New: v,
   674  		})
   675  	}
   676  	for k, v := range stateMap {
   677  		diff.Attributes[prefix+k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
   678  			Old:        v,
   679  			NewRemoved: true,
   680  		})
   681  	}
   682  
   683  	return nil
   684  }
   685  
   686  func (m schemaMap) diffSet(
   687  	k string,
   688  	schema *Schema,
   689  	diff *terraform.InstanceDiff,
   690  	d *ResourceData,
   691  	all bool) error {
   692  	o, n, _, computedSet := d.diffChange(k)
   693  	if computedSet {
   694  		n = nil
   695  	}
   696  	nSet := n != nil
   697  
   698  	// If we have an old value and no new value is set or will be
   699  	// computed once all variables can be interpolated and we're
   700  	// computed, then nothing has changed.
   701  	if o != nil && n == nil && !computedSet && schema.Computed {
   702  		return nil
   703  	}
   704  
   705  	if o == nil {
   706  		o = &Set{F: schema.Set}
   707  	}
   708  	if n == nil {
   709  		n = &Set{F: schema.Set}
   710  	}
   711  	os := o.(*Set)
   712  	ns := n.(*Set)
   713  
   714  	// If the new value was set, compare the listCode's to determine if
   715  	// the two are equal. Comparing listCode's instead of the actuall values
   716  	// is needed because there could be computed values in the set which
   717  	// would result in false positives while comparing.
   718  	if !all && nSet && reflect.DeepEqual(os.listCode(), ns.listCode()) {
   719  		return nil
   720  	}
   721  
   722  	// Get the counts
   723  	oldLen := os.Len()
   724  	newLen := ns.Len()
   725  	oldStr := strconv.Itoa(oldLen)
   726  	newStr := strconv.Itoa(newLen)
   727  
   728  	// If the set computed then say that the # is computed
   729  	if computedSet || (schema.Computed && !nSet) {
   730  		// If # already exists, equals 0 and no new set is supplied, there
   731  		// is nothing to record in the diff
   732  		count, ok := d.GetOk(k + ".#")
   733  		if ok && count.(int) == 0 && !nSet && !computedSet {
   734  			return nil
   735  		}
   736  
   737  		// Set the count but make sure that if # does not exist, we don't
   738  		// use the zeroed value
   739  		countStr := strconv.Itoa(count.(int))
   740  		if !ok {
   741  			countStr = ""
   742  		}
   743  
   744  		diff.Attributes[k+".#"] = &terraform.ResourceAttrDiff{
   745  			Old:         countStr,
   746  			NewComputed: true,
   747  		}
   748  		return nil
   749  	}
   750  
   751  	// If the counts are not the same, then record that diff
   752  	changed := oldLen != newLen
   753  	if changed || all {
   754  		countSchema := &Schema{
   755  			Type:     TypeInt,
   756  			Computed: schema.Computed,
   757  			ForceNew: schema.ForceNew,
   758  		}
   759  
   760  		diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{
   761  			Old: oldStr,
   762  			New: newStr,
   763  		})
   764  	}
   765  
   766  	for _, code := range ns.listCode() {
   767  		switch t := schema.Elem.(type) {
   768  		case *Resource:
   769  			// This is a complex resource
   770  			for k2, schema := range t.Schema {
   771  				subK := fmt.Sprintf("%s.%d.%s", k, code, k2)
   772  				subK = strings.Replace(subK, "-", "~", -1)
   773  				err := m.diff(subK, schema, diff, d, true)
   774  				if err != nil {
   775  					return err
   776  				}
   777  			}
   778  		case *Schema:
   779  			// Copy the schema so that we can set Computed/ForceNew from
   780  			// the parent schema (the TypeSet).
   781  			t2 := *t
   782  			t2.ForceNew = schema.ForceNew
   783  
   784  			// This is just a primitive element, so go through each and
   785  			// just diff each.
   786  			subK := fmt.Sprintf("%s.%d", k, code)
   787  			subK = strings.Replace(subK, "-", "~", -1)
   788  			err := m.diff(subK, &t2, diff, d, true)
   789  			if err != nil {
   790  				return err
   791  			}
   792  		default:
   793  			return fmt.Errorf("%s: unknown element type (internal)", k)
   794  		}
   795  	}
   796  
   797  	return nil
   798  }
   799  
   800  func (m schemaMap) diffString(
   801  	k string,
   802  	schema *Schema,
   803  	diff *terraform.InstanceDiff,
   804  	d *ResourceData,
   805  	all bool) error {
   806  	var originalN interface{}
   807  	var os, ns string
   808  	o, n, _, _ := d.diffChange(k)
   809  	if schema.StateFunc != nil {
   810  		originalN = n
   811  		n = schema.StateFunc(n)
   812  	}
   813  	nraw := n
   814  	if nraw == nil && o != nil {
   815  		nraw = schema.Type.Zero()
   816  	}
   817  	if err := mapstructure.WeakDecode(o, &os); err != nil {
   818  		return fmt.Errorf("%s: %s", k, err)
   819  	}
   820  	if err := mapstructure.WeakDecode(nraw, &ns); err != nil {
   821  		return fmt.Errorf("%s: %s", k, err)
   822  	}
   823  
   824  	if os == ns && !all {
   825  		// They're the same value. If there old value is not blank or we
   826  		// have an ID, then return right away since we're already setup.
   827  		if os != "" || d.Id() != "" {
   828  			return nil
   829  		}
   830  
   831  		// Otherwise, only continue if we're computed
   832  		if !schema.Computed {
   833  			return nil
   834  		}
   835  	}
   836  
   837  	removed := false
   838  	if o != nil && n == nil {
   839  		removed = true
   840  	}
   841  	if removed && schema.Computed {
   842  		return nil
   843  	}
   844  
   845  	diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
   846  		Old:        os,
   847  		New:        ns,
   848  		NewExtra:   originalN,
   849  		NewRemoved: removed,
   850  	})
   851  
   852  	return nil
   853  }
   854  
   855  func (m schemaMap) inputString(
   856  	input terraform.UIInput,
   857  	k string,
   858  	schema *Schema) (interface{}, error) {
   859  	result, err := input.Input(&terraform.InputOpts{
   860  		Id:          k,
   861  		Query:       k,
   862  		Description: schema.Description,
   863  		Default:     schema.InputDefault,
   864  	})
   865  
   866  	return result, err
   867  }
   868  
   869  func (m schemaMap) validate(
   870  	k string,
   871  	schema *Schema,
   872  	c *terraform.ResourceConfig) ([]string, []error) {
   873  	raw, ok := c.Get(k)
   874  	if !ok && schema.DefaultFunc != nil {
   875  		// We have a dynamic default. Check if we have a value.
   876  		var err error
   877  		raw, err = schema.DefaultFunc()
   878  		if err != nil {
   879  			return nil, []error{fmt.Errorf(
   880  				"%s, error loading default: %s", k, err)}
   881  		}
   882  
   883  		// We're okay as long as we had a value set
   884  		ok = raw != nil
   885  	}
   886  	if !ok {
   887  		if schema.Required {
   888  			return nil, []error{fmt.Errorf(
   889  				"%s: required field is not set", k)}
   890  		}
   891  
   892  		return nil, nil
   893  	}
   894  
   895  	if !schema.Required && !schema.Optional {
   896  		// This is a computed-only field
   897  		return nil, []error{fmt.Errorf(
   898  			"%s: this field cannot be set", k)}
   899  	}
   900  
   901  	return m.validateType(k, raw, schema, c)
   902  }
   903  
   904  func (m schemaMap) validateList(
   905  	k string,
   906  	raw interface{},
   907  	schema *Schema,
   908  	c *terraform.ResourceConfig) ([]string, []error) {
   909  	// We use reflection to verify the slice because you can't
   910  	// case to []interface{} unless the slice is exactly that type.
   911  	rawV := reflect.ValueOf(raw)
   912  	if rawV.Kind() != reflect.Slice {
   913  		return nil, []error{fmt.Errorf(
   914  			"%s: should be a list", k)}
   915  	}
   916  
   917  	// Now build the []interface{}
   918  	raws := make([]interface{}, rawV.Len())
   919  	for i, _ := range raws {
   920  		raws[i] = rawV.Index(i).Interface()
   921  	}
   922  
   923  	var ws []string
   924  	var es []error
   925  	for i, raw := range raws {
   926  		key := fmt.Sprintf("%s.%d", k, i)
   927  
   928  		var ws2 []string
   929  		var es2 []error
   930  		switch t := schema.Elem.(type) {
   931  		case *Resource:
   932  			// This is a sub-resource
   933  			ws2, es2 = m.validateObject(key, t.Schema, c)
   934  		case *Schema:
   935  			ws2, es2 = m.validateType(key, raw, t, c)
   936  		}
   937  
   938  		if len(ws2) > 0 {
   939  			ws = append(ws, ws2...)
   940  		}
   941  		if len(es2) > 0 {
   942  			es = append(es, es2...)
   943  		}
   944  	}
   945  
   946  	return ws, es
   947  }
   948  
   949  func (m schemaMap) validateMap(
   950  	k string,
   951  	raw interface{},
   952  	schema *Schema,
   953  	c *terraform.ResourceConfig) ([]string, []error) {
   954  	// We use reflection to verify the slice because you can't
   955  	// case to []interface{} unless the slice is exactly that type.
   956  	rawV := reflect.ValueOf(raw)
   957  	switch rawV.Kind() {
   958  	case reflect.Map:
   959  	case reflect.Slice:
   960  	default:
   961  		return nil, []error{fmt.Errorf(
   962  			"%s: should be a map", k)}
   963  	}
   964  
   965  	// If it is not a slice, it is valid
   966  	if rawV.Kind() != reflect.Slice {
   967  		return nil, nil
   968  	}
   969  
   970  	// It is a slice, verify that all the elements are maps
   971  	raws := make([]interface{}, rawV.Len())
   972  	for i, _ := range raws {
   973  		raws[i] = rawV.Index(i).Interface()
   974  	}
   975  
   976  	for _, raw := range raws {
   977  		v := reflect.ValueOf(raw)
   978  		if v.Kind() != reflect.Map {
   979  			return nil, []error{fmt.Errorf(
   980  				"%s: should be a map", k)}
   981  		}
   982  	}
   983  
   984  	return nil, nil
   985  }
   986  
   987  func (m schemaMap) validateObject(
   988  	k string,
   989  	schema map[string]*Schema,
   990  	c *terraform.ResourceConfig) ([]string, []error) {
   991  	var ws []string
   992  	var es []error
   993  	for subK, s := range schema {
   994  		key := subK
   995  		if k != "" {
   996  			key = fmt.Sprintf("%s.%s", k, subK)
   997  		}
   998  
   999  		ws2, es2 := m.validate(key, s, c)
  1000  		if len(ws2) > 0 {
  1001  			ws = append(ws, ws2...)
  1002  		}
  1003  		if len(es2) > 0 {
  1004  			es = append(es, es2...)
  1005  		}
  1006  	}
  1007  
  1008  	// Detect any extra/unknown keys and report those as errors.
  1009  	raw, _ := c.Get(k)
  1010  	if m, ok := raw.(map[string]interface{}); ok {
  1011  		for subk, _ := range m {
  1012  			if _, ok := schema[subk]; !ok {
  1013  				es = append(es, fmt.Errorf(
  1014  					"%s: invalid or unknown key: %s", k, subk))
  1015  			}
  1016  		}
  1017  	}
  1018  
  1019  	return ws, es
  1020  }
  1021  
  1022  func (m schemaMap) validatePrimitive(
  1023  	k string,
  1024  	raw interface{},
  1025  	schema *Schema,
  1026  	c *terraform.ResourceConfig) ([]string, []error) {
  1027  	if c.IsComputed(k) {
  1028  		// If the key is being computed, then it is not an error
  1029  		return nil, nil
  1030  	}
  1031  
  1032  	switch schema.Type {
  1033  	case TypeBool:
  1034  		// Verify that we can parse this as the correct type
  1035  		var n bool
  1036  		if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1037  			return nil, []error{err}
  1038  		}
  1039  	case TypeInt:
  1040  		// Verify that we can parse this as an int
  1041  		var n int
  1042  		if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1043  			return nil, []error{err}
  1044  		}
  1045  	case TypeFloat:
  1046  		// Verify that we can parse this as an int
  1047  		var n float64
  1048  		if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1049  			return nil, []error{err}
  1050  		}
  1051  	case TypeString:
  1052  		// Verify that we can parse this as a string
  1053  		var n string
  1054  		if err := mapstructure.WeakDecode(raw, &n); err != nil {
  1055  			return nil, []error{err}
  1056  		}
  1057  	default:
  1058  		panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
  1059  	}
  1060  
  1061  	return nil, nil
  1062  }
  1063  
  1064  func (m schemaMap) validateType(
  1065  	k string,
  1066  	raw interface{},
  1067  	schema *Schema,
  1068  	c *terraform.ResourceConfig) ([]string, []error) {
  1069  	switch schema.Type {
  1070  	case TypeSet:
  1071  		fallthrough
  1072  	case TypeList:
  1073  		return m.validateList(k, raw, schema, c)
  1074  	case TypeMap:
  1075  		return m.validateMap(k, raw, schema, c)
  1076  	default:
  1077  		return m.validatePrimitive(k, raw, schema, c)
  1078  	}
  1079  }
  1080  
  1081  // Zero returns the zero value for a type.
  1082  func (t ValueType) Zero() interface{} {
  1083  	switch t {
  1084  	case TypeInvalid:
  1085  		return nil
  1086  	case TypeBool:
  1087  		return false
  1088  	case TypeInt:
  1089  		return 0
  1090  	case TypeFloat:
  1091  		return 0.0
  1092  	case TypeString:
  1093  		return ""
  1094  	case TypeList:
  1095  		return []interface{}{}
  1096  	case TypeMap:
  1097  		return map[string]interface{}{}
  1098  	case TypeSet:
  1099  		return new(Set)
  1100  	case typeObject:
  1101  		return map[string]interface{}{}
  1102  	default:
  1103  		panic(fmt.Sprintf("unknown type %s", t))
  1104  	}
  1105  }