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