From 0a5e06d62cda2348d11f885ec282830c9a2d4159 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 18 Oct 2014 13:58:01 -0700 Subject: [PATCH] terraform: merge interpolated inherited provider configurations [GH-418] --- CHANGELOG.md | 1 + terraform/context.go | 12 ++++- terraform/context_test.go | 49 +++++++++++++++++++ terraform/graph.go | 2 +- .../child/main.tf | 8 +++ .../plan-module-provider-defaults-var/main.tf | 7 +++ 6 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 terraform/test-fixtures/plan-module-provider-defaults-var/child/main.tf create mode 100644 terraform/test-fixtures/plan-module-provider-defaults-var/main.tf diff --git a/CHANGELOG.md b/CHANGELOG.md index d6785e32b598..b20b9b9f7152 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ BUG FIXES: * core: Set types are validated to be sets. [GH-413] * core: Fix crash case when destroying with tainted resources. [GH-412] * core: Don't execute provisioners in some cases on destroy. + * core: Inherited provider configurations will be properly interpolated. [GH-418] * providers/aws: Refresh of launch configs and autoscale groups load the correct data and don't incorrectly recreate themselves. [GH-425] * providers/aws: Fix case where ELB would incorrectly plan to modify diff --git a/terraform/context.go b/terraform/context.go index 6fa7938734cc..c6f367fb5eb7 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -1203,9 +1203,17 @@ func (c *walkContext) genericWalkFn(cb genericWalkFunc) depgraph.WalkFunc { } for k, p := range sharedProvider.Providers { - // Merge the configurations to get what we use to configure with + // Interpolate our own configuration before merging + if sharedProvider.Config != nil { + rc := NewResourceConfig(sharedProvider.Config.RawConfig) + rc.interpolate(c, nil) + } + + // Merge the configurations to get what we use to configure + // with. We don't need to interpolate this because the + // lines above verify that all parents are interpolated + // properly. rc := sharedProvider.MergeConfig(false, cs[k]) - rc.interpolate(c, nil) log.Printf("[INFO] Configuring provider: %s", k) err := p.Configure(rc) diff --git a/terraform/context_test.go b/terraform/context_test.go index 16f1be98bf19..f3b8cd6efb99 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -1,6 +1,7 @@ package terraform import ( + "bytes" "fmt" "os" "reflect" @@ -2903,6 +2904,54 @@ func TestContextPlan_moduleProviderDefaults(t *testing.T) { } } +func TestContextPlan_moduleProviderDefaultsVar(t *testing.T) { + var l sync.Mutex + var calls []string + + m := testModule(t, "plan-module-provider-defaults-var") + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": func() (ResourceProvider, error) { + l.Lock() + defer l.Unlock() + + p := testProvider("aws") + p.ConfigureFn = func(c *ResourceConfig) error { + var buf bytes.Buffer + if v, ok := c.Get("from"); ok { + buf.WriteString(v.(string) + "\n") + } + if v, ok := c.Get("to"); ok { + buf.WriteString(v.(string) + "\n") + } + + calls = append(calls, buf.String()) + return nil + } + p.DiffFn = testDiffFn + return p, nil + }, + }, + Variables: map[string]string{ + "foo": "root", + }, + }) + + _, err := ctx.Plan(nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + expected := []string{ + "root\n", + "root\nchild\n", + } + if !reflect.DeepEqual(calls, expected) { + t.Fatalf("BAD: %#v", calls) + } +} + func TestContextPlan_moduleVar(t *testing.T) { m := testModule(t, "plan-module-var") p := testProvider("aws") diff --git a/terraform/graph.go b/terraform/graph.go index f79189195cfd..b52dacdd1f69 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -1597,7 +1597,7 @@ func (p *graphSharedProvider) MergeConfig( if override != nil { rawMap = override } else if p.Config != nil { - rawMap = p.Config.RawConfig.Raw + rawMap = p.Config.RawConfig.Config() } if rawMap == nil { rawMap = make(map[string]interface{}) diff --git a/terraform/test-fixtures/plan-module-provider-defaults-var/child/main.tf b/terraform/test-fixtures/plan-module-provider-defaults-var/child/main.tf new file mode 100644 index 000000000000..5ce4f55fe841 --- /dev/null +++ b/terraform/test-fixtures/plan-module-provider-defaults-var/child/main.tf @@ -0,0 +1,8 @@ +provider "aws" { + from = "child" + to = "child" +} + +resource "aws_instance" "foo" { + from = "child" +} diff --git a/terraform/test-fixtures/plan-module-provider-defaults-var/main.tf b/terraform/test-fixtures/plan-module-provider-defaults-var/main.tf new file mode 100644 index 000000000000..83b2411543c0 --- /dev/null +++ b/terraform/test-fixtures/plan-module-provider-defaults-var/main.tf @@ -0,0 +1,7 @@ +module "child" { + source = "./child" +} + +provider "aws" { + from = "${var.foo}" +}