diff --git a/config/config.go b/config/config.go index adacf5ec9..99f2a66cb 100644 --- a/config/config.go +++ b/config/config.go @@ -283,13 +283,18 @@ func (r *Resource) mergerMerge(m merger) merger { // DefaultsMap returns a map of default values for this variable. func (v *Variable) DefaultsMap() map[string]string { + n := fmt.Sprintf("var.%s", v.Name) + switch v.Type() { case VariableTypeString: - return map[string]string{v.Name: v.Default.(string)} + return map[string]string{n: v.Default.(string)} case VariableTypeMap: - return flatmap.Flatten(map[string]interface{}{ - v.Name: v.Default.(map[string]string), + result := flatmap.Flatten(map[string]interface{}{ + n: v.Default.(map[string]string), }) + result[n] = v.Name + + return result default: return nil } diff --git a/config/config_test.go b/config/config_test.go index 40f54b5c7..3fe651743 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -104,7 +104,7 @@ func TestVariableDefaultsMap(t *testing.T) { }{ { "foo", - map[string]string{"foo": "foo"}, + map[string]string{"var.foo": "foo"}, }, { @@ -113,8 +113,9 @@ func TestVariableDefaultsMap(t *testing.T) { "bar": "baz", }, map[string]string{ - "foo.foo": "bar", - "foo.bar": "baz", + "var.foo": "foo", + "var.foo.foo": "bar", + "var.foo.bar": "baz", }, }, } diff --git a/config/loader_libucl.go b/config/loader_libucl.go index 176c588bd..cd14774fd 100644 --- a/config/loader_libucl.go +++ b/config/loader_libucl.go @@ -51,6 +51,20 @@ func (t *libuclConfigurable) Config() (*Config, error) { if len(rawConfig.Variable) > 0 { config.Variables = make([]*Variable, 0, len(rawConfig.Variable)) for k, v := range rawConfig.Variable { + // Defaults turn into a slice of map[string]interface{} and + // we need to make sure to convert that down into the + // proper type for Config. + if ms, ok := v.Default.([]map[string]interface{}); ok { + def := make(map[string]interface{}) + for _, m := range ms { + for k, v := range m { + def[k] = v + } + } + + v.Default = def + } + newVar := &Variable{ Name: k, Default: v.Default, diff --git a/terraform/context.go b/terraform/context.go index 1fa95ae0d..5555f2e98 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -30,6 +30,7 @@ type Context struct { providers map[string]ResourceProviderFactory provisioners map[string]ResourceProvisionerFactory variables map[string]string + defaultVars map[string]string l sync.Mutex // Lock acquired during any task parCh chan struct{} // Semaphore used to limit parallelism @@ -72,6 +73,14 @@ func NewContext(opts *ContextOpts) *Context { } parCh := make(chan struct{}, par) + // Calculate all the default variables + defaultVars := make(map[string]string) + for _, v := range opts.Config.Variables { + for k, val := range v.DefaultsMap() { + defaultVars[k] = val + } + } + return &Context{ config: opts.Config, diff: opts.Diff, @@ -80,6 +89,7 @@ func NewContext(opts *ContextOpts) *Context { providers: opts.Providers, provisioners: opts.Provisioners, variables: opts.Variables, + defaultVars: defaultVars, parCh: parCh, sh: sh, @@ -296,8 +306,13 @@ func (c *Context) computeVars(raw *config.RawConfig) error { return nil } - // Go through each variable and find it + // Start building up the variables. First, defaults vs := make(map[string]string) + for k, v := range c.defaultVars { + vs[k] = v + } + + // Next, the actual computed variables for n, rawV := range raw.Variables { switch v := rawV.(type) { case *config.ResourceVariable: @@ -314,7 +329,10 @@ func (c *Context) computeVars(raw *config.RawConfig) error { vs[n] = attr case *config.UserVariable: - vs[n] = c.variables[v.Name] + val, ok := c.variables[v.Name] + if ok { + vs[n] = val + } } } diff --git a/terraform/context_test.go b/terraform/context_test.go index 40143f36d..504941b64 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -1045,7 +1045,7 @@ func TestContextApply_vars(t *testing.T) { "aws": testProviderFuncFixed(p), }, Variables: map[string]string{ - "foo": "bar", + "foo": "us-west-2", }, }) diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 635148d93..ac452148f 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -221,7 +221,8 @@ aws_instance.foo: const testTerraformApplyVarsStr = ` aws_instance.bar: ID = foo - foo = bar + bar = foo + foo = us-west-2 type = aws_instance aws_instance.foo: ID = foo diff --git a/terraform/test-fixtures/apply-vars/main.tf b/terraform/test-fixtures/apply-vars/main.tf index f5c9c684f..986954562 100644 --- a/terraform/test-fixtures/apply-vars/main.tf +++ b/terraform/test-fixtures/apply-vars/main.tf @@ -1,7 +1,15 @@ +variable "amis" { + default = { + "us-east-1": "foo", + "us-west-2": "foo", + } +} + resource "aws_instance" "foo" { num = "2" } resource "aws_instance" "bar" { foo = "${var.foo}" + bar = "${lookup(var.amis, var.foo)}" }