terraform: configure provider aliases in the new apply graph

Found via a shadow graph failure:

Provider aliases weren't being configured by the new apply graph.

This was caused by the transform that attaches configs to provider nodes
not being able to handle aliases and therefore not attaching a config.
Added a test to this and fixed it.
This commit is contained in:
Mitchell Hashimoto 2016-11-04 16:35:48 -07:00
parent b7bee66df5
commit d7ed6637c1
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 81 additions and 3 deletions

View File

@ -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")

View File

@ -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:

View File

@ -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" }

View File

@ -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:]
@ -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