2014-05-24 13:36:22 -05:00
|
|
|
// The config package is responsible for loading and validating the
|
|
|
|
// configuration.
|
2014-05-22 18:56:28 -05:00
|
|
|
package config
|
|
|
|
|
2014-05-23 23:58:06 -05:00
|
|
|
import (
|
2014-05-24 15:57:51 -05:00
|
|
|
"fmt"
|
2014-07-06 15:56:18 -05:00
|
|
|
"strconv"
|
2014-05-23 23:58:06 -05:00
|
|
|
"strings"
|
2014-07-03 12:14:17 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/multierror"
|
2014-05-23 23:58:06 -05:00
|
|
|
)
|
|
|
|
|
2014-05-22 18:56:28 -05:00
|
|
|
// Config is the configuration that comes from loading a collection
|
|
|
|
// of Terraform templates.
|
|
|
|
type Config struct {
|
2014-05-25 20:05:18 -05:00
|
|
|
ProviderConfigs map[string]*ProviderConfig
|
|
|
|
Resources []*Resource
|
2014-06-03 17:55:51 -05:00
|
|
|
Variables map[string]*Variable
|
2014-07-04 12:43:06 -05:00
|
|
|
Outputs map[string]*Output
|
2014-05-25 20:05:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProviderConfig is the configuration for a resource provider.
|
|
|
|
//
|
|
|
|
// For example, Terraform needs to set the AWS access keys for the AWS
|
|
|
|
// resource provider.
|
|
|
|
type ProviderConfig struct {
|
2014-06-12 19:40:59 -05:00
|
|
|
RawConfig *RawConfig
|
2014-05-22 18:56:28 -05:00
|
|
|
}
|
|
|
|
|
2014-05-23 23:58:06 -05:00
|
|
|
// A resource represents a single Terraform resource in the configuration.
|
|
|
|
// A Terraform resource is something that represents some component that
|
|
|
|
// can be created and managed, and has some properties associated with it.
|
2014-05-22 18:56:28 -05:00
|
|
|
type Resource struct {
|
2014-05-23 23:58:06 -05:00
|
|
|
Name string
|
|
|
|
Type string
|
2014-07-03 22:11:58 -05:00
|
|
|
Count int
|
2014-06-12 19:40:59 -05:00
|
|
|
RawConfig *RawConfig
|
2014-05-22 18:56:28 -05:00
|
|
|
}
|
|
|
|
|
2014-06-03 17:55:51 -05:00
|
|
|
// Variable is a variable defined within the configuration.
|
2014-05-22 18:56:28 -05:00
|
|
|
type Variable struct {
|
|
|
|
Default string
|
|
|
|
Description string
|
2014-06-03 17:55:51 -05:00
|
|
|
defaultSet bool
|
2014-05-22 18:56:28 -05:00
|
|
|
}
|
2014-05-23 23:58:06 -05:00
|
|
|
|
2014-07-04 12:43:06 -05:00
|
|
|
// Output is an output defined within the configuration. An output is
|
|
|
|
// resulting data that is highlighted by Terraform when finished.
|
|
|
|
type Output struct {
|
|
|
|
Name string
|
|
|
|
RawConfig *RawConfig
|
|
|
|
}
|
|
|
|
|
2014-05-23 23:58:06 -05:00
|
|
|
// An InterpolatedVariable is a variable that is embedded within a string
|
|
|
|
// in the configuration, such as "hello ${world}" (world in this case is
|
|
|
|
// an interpolated variable).
|
|
|
|
//
|
|
|
|
// These variables can come from a variety of sources, represented by
|
|
|
|
// implementations of this interface.
|
|
|
|
type InterpolatedVariable interface {
|
|
|
|
FullKey() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// A ResourceVariable is a variable that is referencing the field
|
|
|
|
// of a resource, such as "${aws_instance.foo.ami}"
|
|
|
|
type ResourceVariable struct {
|
2014-07-05 12:34:52 -05:00
|
|
|
Type string // Resource type, i.e. "aws_instance"
|
|
|
|
Name string // Resource name
|
|
|
|
Field string // Resource field
|
2014-07-06 15:46:56 -05:00
|
|
|
|
|
|
|
Multi bool // True if multi-variable: aws_instance.foo.*.id
|
|
|
|
Index int // Index for multi-variable: aws_instance.foo.1.id == 1
|
2014-05-23 23:58:06 -05:00
|
|
|
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
// A UserVariable is a variable that is referencing a user variable
|
|
|
|
// that is inputted from outside the configuration. This looks like
|
|
|
|
// "${var.foo}"
|
|
|
|
type UserVariable struct {
|
2014-05-24 13:41:19 -05:00
|
|
|
Name string
|
|
|
|
|
2014-05-24 14:51:31 -05:00
|
|
|
key string
|
2014-05-23 23:58:06 -05:00
|
|
|
}
|
|
|
|
|
2014-06-05 14:21:05 -05:00
|
|
|
// ProviderConfigName returns the name of the provider configuration in
|
|
|
|
// the given mapping that maps to the proper provider configuration
|
|
|
|
// for this resource.
|
2014-06-24 15:29:07 -05:00
|
|
|
func ProviderConfigName(t string, pcs map[string]*ProviderConfig) string {
|
2014-06-05 14:21:05 -05:00
|
|
|
lk := ""
|
|
|
|
for k, _ := range pcs {
|
2014-06-24 15:29:07 -05:00
|
|
|
if strings.HasPrefix(t, k) && len(k) > len(lk) {
|
2014-06-05 14:21:05 -05:00
|
|
|
lk = k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lk
|
|
|
|
}
|
|
|
|
|
2014-06-24 15:29:07 -05:00
|
|
|
// A unique identifier for this resource.
|
|
|
|
func (r *Resource) Id() string {
|
|
|
|
return fmt.Sprintf("%s.%s", r.Type, r.Name)
|
|
|
|
}
|
|
|
|
|
2014-06-05 14:47:28 -05:00
|
|
|
// Validate does some basic semantic checking of the configuration.
|
|
|
|
func (c *Config) Validate() error {
|
2014-07-02 23:06:26 -05:00
|
|
|
var errs []error
|
|
|
|
|
|
|
|
vars := c.allVariables()
|
|
|
|
|
2014-07-02 23:00:06 -05:00
|
|
|
// Check for references to user variables that do not actually
|
|
|
|
// exist and record those errors.
|
2014-07-04 12:53:36 -05:00
|
|
|
for source, vs := range vars {
|
|
|
|
for _, v := range vs {
|
|
|
|
uv, ok := v.(*UserVariable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := c.Variables[uv.Name]; !ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: unknown variable referenced: %s",
|
|
|
|
source,
|
|
|
|
uv.Name))
|
|
|
|
}
|
2014-07-02 23:00:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 23:06:26 -05:00
|
|
|
// Check that all references to resources are valid
|
2014-07-06 15:46:56 -05:00
|
|
|
resources := make(map[string]*Resource)
|
2014-07-02 23:06:26 -05:00
|
|
|
for _, r := range c.Resources {
|
2014-07-06 15:46:56 -05:00
|
|
|
resources[r.Id()] = r
|
2014-07-02 23:06:26 -05:00
|
|
|
}
|
2014-07-04 12:53:36 -05:00
|
|
|
for source, vs := range vars {
|
|
|
|
for _, v := range vs {
|
|
|
|
rv, ok := v.(*ResourceVariable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
id := fmt.Sprintf("%s.%s", rv.Type, rv.Name)
|
2014-07-06 15:46:56 -05:00
|
|
|
r, ok := resources[id]
|
|
|
|
if !ok {
|
2014-07-04 12:53:36 -05:00
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: unknown resource '%s' referenced in variable %s",
|
|
|
|
source,
|
|
|
|
id,
|
|
|
|
rv.FullKey()))
|
2014-07-06 15:46:56 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is a multi reference and resource has a single
|
|
|
|
// count, it is an error.
|
|
|
|
if r.Count > 1 && !rv.Multi {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: variable '%s' must specify index for multi-count "+
|
|
|
|
"resource %s",
|
|
|
|
source,
|
|
|
|
rv.FullKey(),
|
|
|
|
id))
|
|
|
|
continue
|
2014-07-04 12:53:36 -05:00
|
|
|
}
|
2014-07-02 23:06:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 12:53:36 -05:00
|
|
|
// Check that all outputs are valid
|
2014-07-04 12:57:09 -05:00
|
|
|
for _, o := range c.Outputs {
|
|
|
|
invalid := false
|
|
|
|
for k, _ := range o.RawConfig.Raw {
|
|
|
|
if k != "value" {
|
|
|
|
invalid = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if invalid {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: output should only have 'value' field", o.Name))
|
|
|
|
}
|
|
|
|
}
|
2014-07-04 12:53:36 -05:00
|
|
|
|
2014-07-02 23:00:06 -05:00
|
|
|
if len(errs) > 0 {
|
2014-07-03 12:14:17 -05:00
|
|
|
return &multierror.Error{Errors: errs}
|
2014-07-02 23:00:06 -05:00
|
|
|
}
|
2014-06-05 14:47:28 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-02 23:00:06 -05:00
|
|
|
// allVariables is a helper that returns a mapping of all the interpolated
|
|
|
|
// variables within the configuration. This is used to verify references
|
|
|
|
// are valid in the Validate step.
|
2014-07-04 12:53:36 -05:00
|
|
|
func (c *Config) allVariables() map[string][]InterpolatedVariable {
|
|
|
|
result := make(map[string][]InterpolatedVariable)
|
2014-07-02 23:00:06 -05:00
|
|
|
for n, pc := range c.ProviderConfigs {
|
|
|
|
source := fmt.Sprintf("provider config '%s'", n)
|
|
|
|
for _, v := range pc.RawConfig.Variables {
|
2014-07-04 12:53:36 -05:00
|
|
|
result[source] = append(result[source], v)
|
2014-07-02 23:00:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rc := range c.Resources {
|
|
|
|
source := fmt.Sprintf("resource '%s'", rc.Id())
|
|
|
|
for _, v := range rc.RawConfig.Variables {
|
2014-07-04 12:53:36 -05:00
|
|
|
result[source] = append(result[source], v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range c.Outputs {
|
|
|
|
source := fmt.Sprintf("output '%s'", o.Name)
|
|
|
|
for _, v := range o.RawConfig.Variables {
|
|
|
|
result[source] = append(result[source], v)
|
2014-07-02 23:00:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-06-03 17:55:51 -05:00
|
|
|
// Required tests whether a variable is required or not.
|
|
|
|
func (v *Variable) Required() bool {
|
|
|
|
return !v.defaultSet
|
|
|
|
}
|
|
|
|
|
2014-05-23 23:58:06 -05:00
|
|
|
func NewResourceVariable(key string) (*ResourceVariable, error) {
|
|
|
|
parts := strings.SplitN(key, ".", 3)
|
2014-07-05 12:34:52 -05:00
|
|
|
field := parts[2]
|
|
|
|
multi := false
|
2014-07-06 15:56:18 -05:00
|
|
|
var index int
|
|
|
|
|
|
|
|
if idx := strings.Index(field, "."); idx != -1 {
|
|
|
|
indexStr := field[:idx]
|
|
|
|
multi = indexStr == "*"
|
|
|
|
index = -1
|
|
|
|
|
|
|
|
if !multi {
|
|
|
|
indexInt, err := strconv.ParseInt(indexStr, 0, 0)
|
|
|
|
if err == nil {
|
|
|
|
multi = true
|
|
|
|
index = int(indexInt)
|
|
|
|
}
|
|
|
|
}
|
2014-07-05 12:34:52 -05:00
|
|
|
|
2014-07-06 15:56:18 -05:00
|
|
|
if multi {
|
|
|
|
field = field[idx+1:]
|
|
|
|
}
|
2014-07-05 12:34:52 -05:00
|
|
|
}
|
|
|
|
|
2014-05-23 23:58:06 -05:00
|
|
|
return &ResourceVariable{
|
|
|
|
Type: parts[0],
|
|
|
|
Name: parts[1],
|
2014-07-05 12:34:52 -05:00
|
|
|
Field: field,
|
|
|
|
Multi: multi,
|
2014-07-06 15:56:18 -05:00
|
|
|
Index: index,
|
2014-05-23 23:58:06 -05:00
|
|
|
key: key,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-05-24 15:57:51 -05:00
|
|
|
func (v *ResourceVariable) ResourceId() string {
|
|
|
|
return fmt.Sprintf("%s.%s", v.Type, v.Name)
|
|
|
|
}
|
|
|
|
|
2014-05-23 23:58:06 -05:00
|
|
|
func (v *ResourceVariable) FullKey() string {
|
|
|
|
return v.key
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserVariable(key string) (*UserVariable, error) {
|
|
|
|
name := key[len("var."):]
|
|
|
|
return &UserVariable{
|
|
|
|
key: key,
|
2014-05-24 13:41:19 -05:00
|
|
|
Name: name,
|
2014-05-23 23:58:06 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *UserVariable) FullKey() string {
|
|
|
|
return v.key
|
|
|
|
}
|