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
This commit is contained in:
Kristin Laemmert 2020-02-12 14:00:08 -05:00 committed by GitHub
parent a765d69fb0
commit 80862f3436
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 3 deletions

View File

@ -333,6 +333,63 @@ func TestImport_providerConfigWithVar(t *testing.T) {
testStateOutput(t, statePath, testImportStr) 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) { func TestImport_providerConfigWithVarDefault(t *testing.T) {
defer testChdir(t, testFixturePath("import-provider-var-default"))() defer testChdir(t, testFixturePath("import-provider-var-default"))()

View File

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

View File

@ -66,9 +66,6 @@ func (b *ImportGraphBuilder) Steps() []GraphTransformer {
TransformProviders(b.Components.ResourceProviders(), concreteProvider, config), TransformProviders(b.Components.ResourceProviders(), concreteProvider, config),
// This validates that the providers only depend on variables
&ImportProviderValidateTransformer{},
// Add the local values // Add the local values
&LocalTransformer{Config: b.Config}, &LocalTransformer{Config: b.Config},
@ -86,6 +83,9 @@ func (b *ImportGraphBuilder) Steps() []GraphTransformer {
// have to connect again later for providers and so on. // have to connect again later for providers and so on.
&ReferenceTransformer{}, &ReferenceTransformer{},
// This validates that the providers only depend on variables
&ImportProviderValidateTransformer{},
// Close opened plugin connections // Close opened plugin connections
&CloseProviderTransformer{}, &CloseProviderTransformer{},