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