From b2fb3b1d0f277cc3da4c52203b699fbb0d5ff794 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 24 Jan 2017 15:36:45 -0800 Subject: [PATCH] terraform: import graph should setup parent refs to providers Fixes #11212 The import graph builder was missing the transform to setup links to parent providers, so provider inheritance didn't work properly. This adds that. This also removes the `PruneProviderTransform` since that has no value in this graph since we'll never add an unused provider. --- terraform/context_import_test.go | 47 +++++++++++++++++++ terraform/graph_builder_import.go | 6 +-- .../import-provider-inherit/child/main.tf | 1 + .../import-provider-inherit/main.tf | 5 ++ 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 terraform/test-fixtures/import-provider-inherit/child/main.tf create mode 100644 terraform/test-fixtures/import-provider-inherit/main.tf diff --git a/terraform/context_import_test.go b/terraform/context_import_test.go index de4df0a83..3d87622ad 100644 --- a/terraform/context_import_test.go +++ b/terraform/context_import_test.go @@ -208,6 +208,53 @@ func TestContextImport_moduleProvider(t *testing.T) { } } +// Test that import sets up the graph properly for provider inheritance +func TestContextImport_providerInherit(t *testing.T) { + p := testProvider("aws") + ctx := testContext2(t, &ContextOpts{ + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + p.ImportStateReturn = []*InstanceState{ + &InstanceState{ + ID: "foo", + Ephemeral: EphemeralState{Type: "aws_instance"}, + }, + } + + configured := false + p.ConfigureFn = func(c *ResourceConfig) error { + configured = true + + if v, ok := c.Get("foo"); !ok || v.(string) != "bar" { + return fmt.Errorf("bad") + } + + return nil + } + + m := testModule(t, "import-provider-inherit") + + _, err := ctx.Import(&ImportOpts{ + Module: m, + Targets: []*ImportTarget{ + &ImportTarget{ + Addr: "module.child.aws_instance.foo", + ID: "bar", + }, + }, + }) + if err != nil { + t.Fatalf("err: %s", err) + } + + if !configured { + t.Fatal("didn't configure provider") + } +} + // Test that import will interpolate provider configuration and use // that configuration for import. func TestContextImport_providerVarConfig(t *testing.T) { diff --git a/terraform/graph_builder_import.go b/terraform/graph_builder_import.go index 46bd97746..7fa76ded7 100644 --- a/terraform/graph_builder_import.go +++ b/terraform/graph_builder_import.go @@ -47,7 +47,7 @@ func (b *ImportGraphBuilder) Steps() []GraphTransformer { steps := []GraphTransformer{ // Create all our resources from the configuration and state - &ConfigTransformerOld{Module: mod}, + &ConfigTransformer{Module: mod}, // Add the import steps &ImportStateTransformer{Targets: b.ImportTargets}, @@ -55,8 +55,8 @@ func (b *ImportGraphBuilder) Steps() []GraphTransformer { // Provider-related transformations &MissingProviderTransformer{Providers: b.Providers, Concrete: concreteProvider}, &ProviderTransformer{}, - &DisableProviderTransformerOld{}, - &PruneProviderTransformer{}, + &DisableProviderTransformer{}, + &ParentProviderTransformer{}, &AttachProviderConfigTransformer{Module: mod}, // This validates that the providers only depend on variables diff --git a/terraform/test-fixtures/import-provider-inherit/child/main.tf b/terraform/test-fixtures/import-provider-inherit/child/main.tf new file mode 100644 index 000000000..b7db25411 --- /dev/null +++ b/terraform/test-fixtures/import-provider-inherit/child/main.tf @@ -0,0 +1 @@ +# Empty diff --git a/terraform/test-fixtures/import-provider-inherit/main.tf b/terraform/test-fixtures/import-provider-inherit/main.tf new file mode 100644 index 000000000..ae152d73a --- /dev/null +++ b/terraform/test-fixtures/import-provider-inherit/main.tf @@ -0,0 +1,5 @@ +provider "aws" { + foo = "bar" +} + +module "child" { source = "./child" }