From d7ed6637c1027aa708b4db4484772d103060b088 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 4 Nov 2016 16:35:48 -0700 Subject: [PATCH] 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. --- terraform/context_apply_test.go | 54 +++++++++++++++++++ terraform/terraform_test.go | 8 +++ .../apply-provider-alias-configure/main.tf | 11 ++++ terraform/transform_attach_config_provider.go | 11 ++-- 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 terraform/test-fixtures/apply-provider-alias-configure/main.tf diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 337261616c..10b98fd2cf 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 23fe5aeeba..dc417309de 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 0000000000..9a9c438949 --- /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 4b41a2d0f3..10506ea060 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