Skip to content

Commit

Permalink
Merge pull request #9894 from hashicorp/b-provider-alias
Browse files Browse the repository at this point in the history
terraform: configure provider aliases in the new apply graph
  • Loading branch information
mitchellh authored Nov 7, 2016
2 parents 1beda4c + d7ed663 commit f580a8e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
54 changes: 54 additions & 0 deletions terraform/context_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,60 @@ func TestContext2Apply_providerAlias(t *testing.T) {
}
}

// Two providers that are configured should both be configured prior to apply
func TestContext2Apply_providerAliasConfigure(t *testing.T) {
m := testModule(t, "apply-provider-alias-configure")

p2 := testProvider("another")
p2.ApplyFn = testApplyFn
p2.DiffFn = testDiffFn

ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"another": testProviderFuncFixed(p2),
},
})

if p, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
} else {
t.Logf(p.String())
}

// Configure to record calls AFTER Plan above
var configCount int32
p2.ConfigureFn = func(c *ResourceConfig) error {
atomic.AddInt32(&configCount, 1)

foo, ok := c.Get("foo")
if !ok {
return fmt.Errorf("foo is not found")
}

if foo != "bar" {
return fmt.Errorf("foo: %#v", foo)
}

return nil
}

state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}

if configCount != 2 {
t.Fatalf("provider config expected 2 calls, got: %d", configCount)
}

actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testTerraformApplyProviderAliasConfigStr)
if actual != expected {
t.Fatalf("bad: \n%s", actual)
}
}

// GH-2870
func TestContext2Apply_providerWarning(t *testing.T) {
m := testModule(t, "apply-provider-warning")
Expand Down
8 changes: 8 additions & 0 deletions terraform/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ aws_instance.foo:
type = aws_instance
`

const testTerraformApplyProviderAliasConfigStr = `
another_instance.bar:
ID = foo
provider = another.two
another_instance.foo:
ID = foo
`

const testTerraformApplyEmptyModuleStr = `
<no state>
Outputs:
Expand Down
11 changes: 11 additions & 0 deletions terraform/test-fixtures/apply-provider-alias-configure/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
provider "another" {
foo = "bar"
}

provider "another" {
alias = "two"
foo = "bar"
}

resource "another_instance" "foo" {}
resource "another_instance" "bar" { provider = "another.two" }
11 changes: 8 additions & 3 deletions terraform/transform_attach_config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ func (t *AttachProviderConfigTransformer) attachProviders(g *Graph) error {
continue
}

// TODO: aliases?

// Determine what we're looking for
path := normalizeModulePath(apn.Path())
path = path[1:]
Expand All @@ -63,7 +61,14 @@ func (t *AttachProviderConfigTransformer) attachProviders(g *Graph) error {

// Go through the provider configs to find the matching config
for _, p := range tree.Config().ProviderConfigs {
if p.Name == name {
// Build the name, which is "name.alias" if an alias exists
current := p.Name
if p.Alias != "" {
current += "." + p.Alias
}

// If the configs match then attach!
if current == name {
log.Printf("[TRACE] Attaching provider config: %#v", p)
apn.AttachProvider(p)
break
Expand Down

0 comments on commit f580a8e

Please sign in to comment.