diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 337261616..10b98fd2c 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -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") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 23fe5aeeb..dc417309d 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -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 = ` Outputs: diff --git a/terraform/test-fixtures/apply-provider-alias-configure/main.tf b/terraform/test-fixtures/apply-provider-alias-configure/main.tf new file mode 100644 index 000000000..9a9c43894 --- /dev/null +++ b/terraform/test-fixtures/apply-provider-alias-configure/main.tf @@ -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" } diff --git a/terraform/transform_attach_config_provider.go b/terraform/transform_attach_config_provider.go index 4b41a2d0f..10506ea06 100644 --- a/terraform/transform_attach_config_provider.go +++ b/terraform/transform_attach_config_provider.go @@ -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