github.com/arvindram03/terraform@v0.3.7-0.20150212015210-408f838db36d/config/config.go (about)

     1  // The config package is responsible for loading and validating the
     2  // configuration.
     3  package config
     4  
     5  import (
     6  	"fmt"
     7  	"regexp"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/hashicorp/terraform/config/lang"
    12  	"github.com/hashicorp/terraform/config/lang/ast"
    13  	"github.com/hashicorp/terraform/flatmap"
    14  	"github.com/hashicorp/terraform/helper/multierror"
    15  	"github.com/mitchellh/mapstructure"
    16  	"github.com/mitchellh/reflectwalk"
    17  )
    18  
    19  // NameRegexp is the regular expression that all names (modules, providers,
    20  // resources, etc.) must follow.
    21  var NameRegexp = regexp.MustCompile(`\A[A-Za-z0-9\-\_]+\z`)
    22  
    23  // Config is the configuration that comes from loading a collection
    24  // of Terraform templates.
    25  type Config struct {
    26  	// Dir is the path to the directory where this configuration was
    27  	// loaded from. If it is blank, this configuration wasn't loaded from
    28  	// any meaningful directory.
    29  	Dir string
    30  
    31  	Modules         []*Module
    32  	ProviderConfigs []*ProviderConfig
    33  	Resources       []*Resource
    34  	Variables       []*Variable
    35  	Outputs         []*Output
    36  
    37  	// The fields below can be filled in by loaders for validation
    38  	// purposes.
    39  	unknownKeys []string
    40  }
    41  
    42  // Module is a module used within a configuration.
    43  //
    44  // This does not represent a module itself, this represents a module
    45  // call-site within an existing configuration.
    46  type Module struct {
    47  	Name      string
    48  	Source    string
    49  	RawConfig *RawConfig
    50  }
    51  
    52  // ProviderConfig is the configuration for a resource provider.
    53  //
    54  // For example, Terraform needs to set the AWS access keys for the AWS
    55  // resource provider.
    56  type ProviderConfig struct {
    57  	Name      string
    58  	RawConfig *RawConfig
    59  }
    60  
    61  // A resource represents a single Terraform resource in the configuration.
    62  // A Terraform resource is something that represents some component that
    63  // can be created and managed, and has some properties associated with it.
    64  type Resource struct {
    65  	Name         string
    66  	Type         string
    67  	RawCount     *RawConfig
    68  	RawConfig    *RawConfig
    69  	Provisioners []*Provisioner
    70  	DependsOn    []string
    71  	Lifecycle    ResourceLifecycle
    72  }
    73  
    74  // ResourceLifecycle is used to store the lifecycle tuning parameters
    75  // to allow customized behavior
    76  type ResourceLifecycle struct {
    77  	CreateBeforeDestroy bool `hcl:"create_before_destroy"`
    78  }
    79  
    80  // Provisioner is a configured provisioner step on a resource.
    81  type Provisioner struct {
    82  	Type      string
    83  	RawConfig *RawConfig
    84  	ConnInfo  *RawConfig
    85  }
    86  
    87  // Variable is a variable defined within the configuration.
    88  type Variable struct {
    89  	Name        string
    90  	Default     interface{}
    91  	Description string
    92  }
    93  
    94  // Output is an output defined within the configuration. An output is
    95  // resulting data that is highlighted by Terraform when finished.
    96  type Output struct {
    97  	Name      string
    98  	RawConfig *RawConfig
    99  }
   100  
   101  // VariableType is the type of value a variable is holding, and returned
   102  // by the Type() function on variables.
   103  type VariableType byte
   104  
   105  const (
   106  	VariableTypeUnknown VariableType = iota
   107  	VariableTypeString
   108  	VariableTypeMap
   109  )
   110  
   111  // ProviderConfigName returns the name of the provider configuration in
   112  // the given mapping that maps to the proper provider configuration
   113  // for this resource.
   114  func ProviderConfigName(t string, pcs []*ProviderConfig) string {
   115  	lk := ""
   116  	for _, v := range pcs {
   117  		k := v.Name
   118  		if strings.HasPrefix(t, k) && len(k) > len(lk) {
   119  			lk = k
   120  		}
   121  	}
   122  
   123  	return lk
   124  }
   125  
   126  // A unique identifier for this module.
   127  func (r *Module) Id() string {
   128  	return fmt.Sprintf("%s", r.Name)
   129  }
   130  
   131  // Count returns the count of this resource.
   132  func (r *Resource) Count() (int, error) {
   133  	v, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0)
   134  	if err != nil {
   135  		return 0, err
   136  	}
   137  
   138  	return int(v), nil
   139  }
   140  
   141  // A unique identifier for this resource.
   142  func (r *Resource) Id() string {
   143  	return fmt.Sprintf("%s.%s", r.Type, r.Name)
   144  }
   145  
   146  // Validate does some basic semantic checking of the configuration.
   147  func (c *Config) Validate() error {
   148  	if c == nil {
   149  		return nil
   150  	}
   151  
   152  	var errs []error
   153  
   154  	for _, k := range c.unknownKeys {
   155  		errs = append(errs, fmt.Errorf(
   156  			"Unknown root level key: %s", k))
   157  	}
   158  
   159  	vars := c.InterpolatedVariables()
   160  	varMap := make(map[string]*Variable)
   161  	for _, v := range c.Variables {
   162  		varMap[v.Name] = v
   163  	}
   164  
   165  	for _, v := range c.Variables {
   166  		if v.Type() == VariableTypeUnknown {
   167  			errs = append(errs, fmt.Errorf(
   168  				"Variable '%s': must be string or mapping",
   169  				v.Name))
   170  			continue
   171  		}
   172  
   173  		interp := false
   174  		fn := func(ast.Node) (string, error) {
   175  			interp = true
   176  			return "", nil
   177  		}
   178  
   179  		w := &interpolationWalker{F: fn}
   180  		if v.Default != nil {
   181  			if err := reflectwalk.Walk(v.Default, w); err == nil {
   182  				if interp {
   183  					errs = append(errs, fmt.Errorf(
   184  						"Variable '%s': cannot contain interpolations",
   185  						v.Name))
   186  				}
   187  			}
   188  		}
   189  	}
   190  
   191  	// Check for references to user variables that do not actually
   192  	// exist and record those errors.
   193  	for source, vs := range vars {
   194  		for _, v := range vs {
   195  			uv, ok := v.(*UserVariable)
   196  			if !ok {
   197  				continue
   198  			}
   199  
   200  			if _, ok := varMap[uv.Name]; !ok {
   201  				errs = append(errs, fmt.Errorf(
   202  					"%s: unknown variable referenced: %s",
   203  					source,
   204  					uv.Name))
   205  			}
   206  		}
   207  	}
   208  
   209  	// Check that all count variables are valid.
   210  	for source, vs := range vars {
   211  		for _, rawV := range vs {
   212  			switch v := rawV.(type) {
   213  			case *CountVariable:
   214  				if v.Type == CountValueInvalid {
   215  					errs = append(errs, fmt.Errorf(
   216  						"%s: invalid count variable: %s",
   217  						source,
   218  						v.FullKey()))
   219  				}
   220  			case *PathVariable:
   221  				if v.Type == PathValueInvalid {
   222  					errs = append(errs, fmt.Errorf(
   223  						"%s: invalid path variable: %s",
   224  						source,
   225  						v.FullKey()))
   226  				}
   227  			}
   228  		}
   229  	}
   230  
   231  	// Check that all references to modules are valid
   232  	modules := make(map[string]*Module)
   233  	dupped := make(map[string]struct{})
   234  	for _, m := range c.Modules {
   235  		// Check for duplicates
   236  		if _, ok := modules[m.Id()]; ok {
   237  			if _, ok := dupped[m.Id()]; !ok {
   238  				dupped[m.Id()] = struct{}{}
   239  
   240  				errs = append(errs, fmt.Errorf(
   241  					"%s: module repeated multiple times",
   242  					m.Id()))
   243  			}
   244  
   245  			// Already seen this module, just skip it
   246  			continue
   247  		}
   248  
   249  		modules[m.Id()] = m
   250  
   251  		// Check that the source has no interpolations
   252  		rc, err := NewRawConfig(map[string]interface{}{
   253  			"root": m.Source,
   254  		})
   255  		if err != nil {
   256  			errs = append(errs, fmt.Errorf(
   257  				"%s: module source error: %s",
   258  				m.Id(), err))
   259  		} else if len(rc.Interpolations) > 0 {
   260  			errs = append(errs, fmt.Errorf(
   261  				"%s: module source cannot contain interpolations",
   262  				m.Id()))
   263  		}
   264  
   265  		// Check that the name matches our regexp
   266  		if !NameRegexp.Match([]byte(m.Name)) {
   267  			errs = append(errs, fmt.Errorf(
   268  				"%s: module name can only contain letters, numbers, "+
   269  					"dashes, and underscores",
   270  				m.Id()))
   271  		}
   272  
   273  		// Check that the configuration can all be strings
   274  		raw := make(map[string]interface{})
   275  		for k, v := range m.RawConfig.Raw {
   276  			var strVal string
   277  			if err := mapstructure.WeakDecode(v, &strVal); err != nil {
   278  				errs = append(errs, fmt.Errorf(
   279  					"%s: variable %s must be a string value",
   280  					m.Id(), k))
   281  			}
   282  			raw[k] = strVal
   283  		}
   284  
   285  		// Update the raw configuration to only contain the string values
   286  		m.RawConfig, err = NewRawConfig(raw)
   287  		if err != nil {
   288  			errs = append(errs, fmt.Errorf(
   289  				"%s: can't initialize configuration: %s",
   290  				m.Id(), err))
   291  		}
   292  	}
   293  	dupped = nil
   294  
   295  	// Check that all variables for modules reference modules that
   296  	// exist.
   297  	for source, vs := range vars {
   298  		for _, v := range vs {
   299  			mv, ok := v.(*ModuleVariable)
   300  			if !ok {
   301  				continue
   302  			}
   303  
   304  			if _, ok := modules[mv.Name]; !ok {
   305  				errs = append(errs, fmt.Errorf(
   306  					"%s: unknown module referenced: %s",
   307  					source,
   308  					mv.Name))
   309  			}
   310  		}
   311  	}
   312  
   313  	// Check that all references to resources are valid
   314  	resources := make(map[string]*Resource)
   315  	dupped = make(map[string]struct{})
   316  	for _, r := range c.Resources {
   317  		if _, ok := resources[r.Id()]; ok {
   318  			if _, ok := dupped[r.Id()]; !ok {
   319  				dupped[r.Id()] = struct{}{}
   320  
   321  				errs = append(errs, fmt.Errorf(
   322  					"%s: resource repeated multiple times",
   323  					r.Id()))
   324  			}
   325  		}
   326  
   327  		resources[r.Id()] = r
   328  	}
   329  	dupped = nil
   330  
   331  	// Validate resources
   332  	for n, r := range resources {
   333  		// Verify count variables
   334  		for _, v := range r.RawCount.Variables {
   335  			switch v.(type) {
   336  			case *CountVariable:
   337  				errs = append(errs, fmt.Errorf(
   338  					"%s: resource count can't reference count variable: %s",
   339  					n,
   340  					v.FullKey()))
   341  			case *ModuleVariable:
   342  				errs = append(errs, fmt.Errorf(
   343  					"%s: resource count can't reference module variable: %s",
   344  					n,
   345  					v.FullKey()))
   346  			case *ResourceVariable:
   347  				errs = append(errs, fmt.Errorf(
   348  					"%s: resource count can't reference resource variable: %s",
   349  					n,
   350  					v.FullKey()))
   351  			case *UserVariable:
   352  				// Good
   353  			default:
   354  				panic("Unknown type in count var: " + n)
   355  			}
   356  		}
   357  
   358  		// Interpolate with a fixed number to verify that its a number.
   359  		r.RawCount.interpolate(func(root ast.Node) (string, error) {
   360  			// Execute the node but transform the AST so that it returns
   361  			// a fixed value of "5" for all interpolations.
   362  			out, _, err := lang.Eval(
   363  				lang.FixedValueTransform(
   364  					root, &ast.LiteralNode{Value: "5", Typex: ast.TypeString}),
   365  				nil)
   366  			if err != nil {
   367  				return "", err
   368  			}
   369  
   370  			return out.(string), nil
   371  		})
   372  		_, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0)
   373  		if err != nil {
   374  			errs = append(errs, fmt.Errorf(
   375  				"%s: resource count must be an integer",
   376  				n))
   377  		}
   378  		r.RawCount.init()
   379  
   380  		// Verify depends on points to resources that all exist
   381  		for _, d := range r.DependsOn {
   382  			if _, ok := resources[d]; !ok {
   383  				errs = append(errs, fmt.Errorf(
   384  					"%s: resource depends on non-existent resource '%s'",
   385  					n, d))
   386  			}
   387  		}
   388  	}
   389  
   390  	for source, vs := range vars {
   391  		for _, v := range vs {
   392  			rv, ok := v.(*ResourceVariable)
   393  			if !ok {
   394  				continue
   395  			}
   396  
   397  			id := fmt.Sprintf("%s.%s", rv.Type, rv.Name)
   398  			if _, ok := resources[id]; !ok {
   399  				errs = append(errs, fmt.Errorf(
   400  					"%s: unknown resource '%s' referenced in variable %s",
   401  					source,
   402  					id,
   403  					rv.FullKey()))
   404  				continue
   405  			}
   406  		}
   407  	}
   408  
   409  	// Check that all outputs are valid
   410  	for _, o := range c.Outputs {
   411  		invalid := false
   412  		for k, _ := range o.RawConfig.Raw {
   413  			if k != "value" {
   414  				invalid = true
   415  				break
   416  			}
   417  		}
   418  		if invalid {
   419  			errs = append(errs, fmt.Errorf(
   420  				"%s: output should only have 'value' field", o.Name))
   421  		}
   422  	}
   423  
   424  	// Check that all variables are in the proper context
   425  	for source, rc := range c.rawConfigs() {
   426  		walker := &interpolationWalker{
   427  			ContextF: c.validateVarContextFn(source, &errs),
   428  		}
   429  		if err := reflectwalk.Walk(rc.Raw, walker); err != nil {
   430  			errs = append(errs, fmt.Errorf(
   431  				"%s: error reading config: %s", source, err))
   432  		}
   433  	}
   434  
   435  	if len(errs) > 0 {
   436  		return &multierror.Error{Errors: errs}
   437  	}
   438  
   439  	return nil
   440  }
   441  
   442  // InterpolatedVariables is a helper that returns a mapping of all the interpolated
   443  // variables within the configuration. This is used to verify references
   444  // are valid in the Validate step.
   445  func (c *Config) InterpolatedVariables() map[string][]InterpolatedVariable {
   446  	result := make(map[string][]InterpolatedVariable)
   447  	for source, rc := range c.rawConfigs() {
   448  		for _, v := range rc.Variables {
   449  			result[source] = append(result[source], v)
   450  		}
   451  	}
   452  	return result
   453  }
   454  
   455  // rawConfigs returns all of the RawConfigs that are available keyed by
   456  // a human-friendly source.
   457  func (c *Config) rawConfigs() map[string]*RawConfig {
   458  	result := make(map[string]*RawConfig)
   459  	for _, pc := range c.ProviderConfigs {
   460  		source := fmt.Sprintf("provider config '%s'", pc.Name)
   461  		result[source] = pc.RawConfig
   462  	}
   463  
   464  	for _, rc := range c.Resources {
   465  		source := fmt.Sprintf("resource '%s'", rc.Id())
   466  		result[source+" count"] = rc.RawCount
   467  		result[source+" config"] = rc.RawConfig
   468  	}
   469  
   470  	for _, o := range c.Outputs {
   471  		source := fmt.Sprintf("output '%s'", o.Name)
   472  		result[source] = o.RawConfig
   473  	}
   474  
   475  	return result
   476  }
   477  
   478  func (c *Config) validateVarContextFn(
   479  	source string, errs *[]error) interpolationWalkerContextFunc {
   480  	return func(loc reflectwalk.Location, node ast.Node) {
   481  		// If we're in a slice element, then its fine, since you can do
   482  		// anything in there.
   483  		if loc == reflectwalk.SliceElem {
   484  			return
   485  		}
   486  
   487  		// Otherwise, let's check if there is a splat resource variable
   488  		// at the top level in here. We do this by doing a transform that
   489  		// replaces everything with a noop node unless its a variable
   490  		// access or concat. This should turn the AST into a flat tree
   491  		// of Concat(Noop, ...). If there are any variables left that are
   492  		// multi-access, then its still broken.
   493  		node = node.Accept(func(n ast.Node) ast.Node {
   494  			// If it is a concat or variable access, we allow it.
   495  			switch n.(type) {
   496  			case *ast.Concat:
   497  				return n
   498  			case *ast.VariableAccess:
   499  				return n
   500  			}
   501  
   502  			// Otherwise, noop
   503  			return &noopNode{}
   504  		})
   505  
   506  		vars, err := DetectVariables(node)
   507  		if err != nil {
   508  			// Ignore it since this will be caught during parse. This
   509  			// actually probably should never happen by the time this
   510  			// is called, but its okay.
   511  			return
   512  		}
   513  
   514  		for _, v := range vars {
   515  			rv, ok := v.(*ResourceVariable)
   516  			if !ok {
   517  				return
   518  			}
   519  
   520  			if rv.Multi && rv.Index == -1 {
   521  				*errs = append(*errs, fmt.Errorf(
   522  					"%s: multi-variable must be in a slice", source))
   523  			}
   524  		}
   525  	}
   526  }
   527  
   528  func (m *Module) mergerName() string {
   529  	return m.Id()
   530  }
   531  
   532  func (m *Module) mergerMerge(other merger) merger {
   533  	m2 := other.(*Module)
   534  
   535  	result := *m
   536  	result.Name = m2.Name
   537  	result.RawConfig = result.RawConfig.merge(m2.RawConfig)
   538  
   539  	if m2.Source != "" {
   540  		result.Source = m2.Source
   541  	}
   542  
   543  	return &result
   544  }
   545  
   546  func (o *Output) mergerName() string {
   547  	return o.Name
   548  }
   549  
   550  func (o *Output) mergerMerge(m merger) merger {
   551  	o2 := m.(*Output)
   552  
   553  	result := *o
   554  	result.Name = o2.Name
   555  	result.RawConfig = result.RawConfig.merge(o2.RawConfig)
   556  
   557  	return &result
   558  }
   559  
   560  func (c *ProviderConfig) mergerName() string {
   561  	return c.Name
   562  }
   563  
   564  func (c *ProviderConfig) mergerMerge(m merger) merger {
   565  	c2 := m.(*ProviderConfig)
   566  
   567  	result := *c
   568  	result.Name = c2.Name
   569  	result.RawConfig = result.RawConfig.merge(c2.RawConfig)
   570  
   571  	return &result
   572  }
   573  
   574  func (r *Resource) mergerName() string {
   575  	return fmt.Sprintf("%s.%s", r.Type, r.Name)
   576  }
   577  
   578  func (r *Resource) mergerMerge(m merger) merger {
   579  	r2 := m.(*Resource)
   580  
   581  	result := *r
   582  	result.Name = r2.Name
   583  	result.Type = r2.Type
   584  	result.RawConfig = result.RawConfig.merge(r2.RawConfig)
   585  
   586  	if r2.RawCount.Value() != "1" {
   587  		result.RawCount = r2.RawCount
   588  	}
   589  
   590  	if len(r2.Provisioners) > 0 {
   591  		result.Provisioners = r2.Provisioners
   592  	}
   593  
   594  	return &result
   595  }
   596  
   597  // DefaultsMap returns a map of default values for this variable.
   598  func (v *Variable) DefaultsMap() map[string]string {
   599  	if v.Default == nil {
   600  		return nil
   601  	}
   602  
   603  	n := fmt.Sprintf("var.%s", v.Name)
   604  	switch v.Type() {
   605  	case VariableTypeString:
   606  		return map[string]string{n: v.Default.(string)}
   607  	case VariableTypeMap:
   608  		result := flatmap.Flatten(map[string]interface{}{
   609  			n: v.Default.(map[string]string),
   610  		})
   611  		result[n] = v.Name
   612  
   613  		return result
   614  	default:
   615  		return nil
   616  	}
   617  }
   618  
   619  // Merge merges two variables to create a new third variable.
   620  func (v *Variable) Merge(v2 *Variable) *Variable {
   621  	// Shallow copy the variable
   622  	result := *v
   623  
   624  	// The names should be the same, but the second name always wins.
   625  	result.Name = v2.Name
   626  
   627  	if v2.Default != nil {
   628  		result.Default = v2.Default
   629  	}
   630  	if v2.Description != "" {
   631  		result.Description = v2.Description
   632  	}
   633  
   634  	return &result
   635  }
   636  
   637  // Type returns the type of varialbe this is.
   638  func (v *Variable) Type() VariableType {
   639  	if v.Default == nil {
   640  		return VariableTypeString
   641  	}
   642  
   643  	var strVal string
   644  	if err := mapstructure.WeakDecode(v.Default, &strVal); err == nil {
   645  		v.Default = strVal
   646  		return VariableTypeString
   647  	}
   648  
   649  	var m map[string]string
   650  	if err := mapstructure.WeakDecode(v.Default, &m); err == nil {
   651  		v.Default = m
   652  		return VariableTypeMap
   653  	}
   654  
   655  	return VariableTypeUnknown
   656  }
   657  
   658  func (v *Variable) mergerName() string {
   659  	return v.Name
   660  }
   661  
   662  func (v *Variable) mergerMerge(m merger) merger {
   663  	return v.Merge(m.(*Variable))
   664  }
   665  
   666  // Required tests whether a variable is required or not.
   667  func (v *Variable) Required() bool {
   668  	return v.Default == nil
   669  }