Skip to content

Commit

Permalink
terraform: merge interpolated inherited provider configurations [GH-418]
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Oct 18, 2014
1 parent f03ab17 commit 0a5e06d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions terraform/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions terraform/context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package terraform

import (
"bytes"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion terraform/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
provider "aws" {
from = "child"
to = "child"
}

resource "aws_instance" "foo" {
from = "child"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "child" {
source = "./child"
}

provider "aws" {
from = "${var.foo}"
}

0 comments on commit 0a5e06d

Please sign in to comment.