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