From 80862f3436fa398ea6ba46d88fe39f4803185450 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Wed, 12 Feb 2020 14:00:08 -0500 Subject: [PATCH] command/import: attach references before validating provider (#22862) There was an order-of-operations bug where the import graph builder was validating that the provider did not have any resource references before references were actually being attached. This PR fixes the order of operations and adds a test (in the command package). Fixes #22804 --- command/import_test.go | 57 +++++++++++++++++++ .../import-provider-datasource/main.tf | 13 +++++ terraform/graph_builder_import.go | 6 +- 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 command/testdata/import-provider-datasource/main.tf diff --git a/command/import_test.go b/command/import_test.go index d9faea46b8..43d54e874f 100644 --- a/command/import_test.go +++ b/command/import_test.go @@ -333,6 +333,63 @@ func TestImport_providerConfigWithVar(t *testing.T) { testStateOutput(t, statePath, testImportStr) } +func TestImport_providerConfigWithDataSource(t *testing.T) { + defer testChdir(t, testFixturePath("import-provider-datasource"))() + + statePath := testTempFile(t) + + p := testProvider() + ui := new(cli.MockUi) + c := &ImportCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(p), + Ui: ui, + }, + } + + p.ImportResourceStateFn = nil + p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ + ImportedResources: []providers.ImportedResource{ + { + TypeName: "test_instance", + State: cty.ObjectVal(map[string]cty.Value{ + "id": cty.StringVal("yay"), + }), + }, + }, + } + p.GetSchemaReturn = &terraform.ProviderSchema{ + Provider: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "foo": {Type: cty.String, Optional: true}, + }, + }, + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + }, + }, + }, + DataSources: map[string]*configschema.Block{ + "test_data": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + }, + }, + }, + } + + args := []string{ + "-state", statePath, + "test_instance.foo", + "bar", + } + if code := c.Run(args); code != 1 { + t.Fatalf("bad, wanted error: %d\n\n%s", code, ui.ErrorWriter.String()) + } +} + func TestImport_providerConfigWithVarDefault(t *testing.T) { defer testChdir(t, testFixturePath("import-provider-var-default"))() diff --git a/command/testdata/import-provider-datasource/main.tf b/command/testdata/import-provider-datasource/main.tf new file mode 100644 index 0000000000..3b9fa3a3e8 --- /dev/null +++ b/command/testdata/import-provider-datasource/main.tf @@ -0,0 +1,13 @@ +provider "test" { + foo = data.test_data.key.id +} + +provider "test" { + alias = "credentials" +} + +data "test_data" "key" { + provider = test.credentials +} + +resource "test_instance" "foo" {} diff --git a/terraform/graph_builder_import.go b/terraform/graph_builder_import.go index 49879e4ebf..d5ad5f262e 100644 --- a/terraform/graph_builder_import.go +++ b/terraform/graph_builder_import.go @@ -66,9 +66,6 @@ func (b *ImportGraphBuilder) Steps() []GraphTransformer { TransformProviders(b.Components.ResourceProviders(), concreteProvider, config), - // This validates that the providers only depend on variables - &ImportProviderValidateTransformer{}, - // Add the local values &LocalTransformer{Config: b.Config}, @@ -86,6 +83,9 @@ func (b *ImportGraphBuilder) Steps() []GraphTransformer { // have to connect again later for providers and so on. &ReferenceTransformer{}, + // This validates that the providers only depend on variables + &ImportProviderValidateTransformer{}, + // Close opened plugin connections &CloseProviderTransformer{},