Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command/import: Fix allow-missing-config option #25352

Merged
merged 1 commit into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions command/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ func TestImport_providerConfigWithVarFile(t *testing.T) {
testStateOutput(t, statePath, testImportStr)
}

func TestImport_disallowMissingResourceConfig(t *testing.T) {
func TestImport_allowMissingResourceConfig(t *testing.T) {
defer testChdir(t, testFixturePath("import-missing-resource-config"))()

statePath := testTempFile(t)
Expand Down Expand Up @@ -643,15 +643,15 @@ func TestImport_disallowMissingResourceConfig(t *testing.T) {
"bar",
}

if code := c.Run(args); code != 1 {
t.Fatalf("import succeeded; expected failure")
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}

msg := ui.ErrorWriter.String()

if want := `Error: Resource test_instance.foo not found in the configuration.`; !strings.Contains(msg, want) {
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
if !p.ImportResourceStateCalled {
t.Fatal("ImportResourceState should be called")
}

testStateOutput(t, statePath, testImportStr)
}

func TestImport_emptyConfig(t *testing.T) {
Expand Down
34 changes: 0 additions & 34 deletions terraform/context_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,6 @@ func TestContextImport_basic(t *testing.T) {
}
}

// Importing a resource which does not exist in the configuration results in an error
func TestContextImport_basic_errpr(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
},
})

p.ImportStateReturn = []*InstanceState{
&InstanceState{
ID: "foo",
Ephemeral: EphemeralState{Type: "aws_instance"},
},
}

_, diags := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "test", addrs.NoKey,
),
ID: "bar",
},
},
})

if !diags.HasErrors() {
t.Fatal("should error")
}
}

func TestContextImport_countIndex(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
Expand Down
34 changes: 15 additions & 19 deletions terraform/transform_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,23 @@ func (t *ImportStateTransformer) Transform(g *Graph) error {
return fmt.Errorf("Module %s not found.", target.Addr.Module.Module())
}

// Get the resource config
rsCfg := modCfg.Module.ResourceByAddr(target.Addr.Resource.Resource)
if rsCfg == nil {
return fmt.Errorf("Resource %s not found in the configuration.", target.Addr)
}

// Get the provider FQN for the resource from the resource configuration
providerFqn := rsCfg.Provider

// This is only likely to happen in misconfigured tests.
if rsCfg == nil {
return fmt.Errorf("provider for resource %s not found in the configuration.", target.Addr)
providerAddr := addrs.AbsProviderConfig{
Module: target.Addr.Module.Module(),
}

// Get the provider local config for the resource
localpCfg := rsCfg.ProviderConfigAddr()

providerAddr := addrs.AbsProviderConfig{
Provider: providerFqn,
Alias: localpCfg.Alias,
Module: target.Addr.Module.Module(),
// Try to find the resource config
rsCfg := modCfg.Module.ResourceByAddr(target.Addr.Resource.Resource)
if rsCfg != nil {
// Get the provider FQN for the resource from the resource configuration
providerAddr.Provider = rsCfg.Provider

// Get the alias from the resource's provider local config
providerAddr.Alias = rsCfg.ProviderConfigAddr().Alias
} else {
// Resource has no matching config, so use an implied provider
// based on the resource type
rsProviderType := target.Addr.Resource.Resource.ImpliedProvider()
providerAddr.Provider = modCfg.Module.ImpliedProviderForUnqualifiedType(rsProviderType)
}

node := &graphNodeImportState{
Expand Down