Skip to content

Commit be8bc25

Browse files
committed
command/import: Fix allow-missing-config option
We previously intentionally removed support for the allow-missing-config option to terraform import, requiring that all imported resources have matching config. See #24412. However, the option was not removed from the import command, and it is widely used. This commit reintroduces support for importing with a missing configuration by falling back to implying the provider FQN based on the resource type.
1 parent 3178d7d commit be8bc25

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

command/import_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ func TestImport_providerConfigWithVarFile(t *testing.T) {
601601
testStateOutput(t, statePath, testImportStr)
602602
}
603603

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

607607
statePath := testTempFile(t)
@@ -643,15 +643,15 @@ func TestImport_disallowMissingResourceConfig(t *testing.T) {
643643
"bar",
644644
}
645645

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

650-
msg := ui.ErrorWriter.String()
651-
652-
if want := `Error: Resource test_instance.foo not found in the configuration.`; !strings.Contains(msg, want) {
653-
t.Errorf("incorrect message\nwant substring: %s\ngot:\n%s", want, msg)
650+
if !p.ImportResourceStateCalled {
651+
t.Fatal("ImportResourceState should be called")
654652
}
653+
654+
testStateOutput(t, statePath, testImportStr)
655655
}
656656

657657
func TestImport_emptyConfig(t *testing.T) {

terraform/transform_import_state.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,23 @@ func (t *ImportStateTransformer) Transform(g *Graph) error {
3030
return fmt.Errorf("Module %s not found.", target.Addr.Module.Module())
3131
}
3232

33-
// Get the resource config
34-
rsCfg := modCfg.Module.ResourceByAddr(target.Addr.Resource.Resource)
35-
if rsCfg == nil {
36-
return fmt.Errorf("Resource %s not found in the configuration.", target.Addr)
37-
}
38-
39-
// Get the provider FQN for the resource from the resource configuration
40-
providerFqn := rsCfg.Provider
41-
42-
// This is only likely to happen in misconfigured tests.
43-
if rsCfg == nil {
44-
return fmt.Errorf("provider for resource %s not found in the configuration.", target.Addr)
33+
providerAddr := addrs.AbsProviderConfig{
34+
Module: target.Addr.Module.Module(),
4535
}
4636

47-
// Get the provider local config for the resource
48-
localpCfg := rsCfg.ProviderConfigAddr()
49-
50-
providerAddr := addrs.AbsProviderConfig{
51-
Provider: providerFqn,
52-
Alias: localpCfg.Alias,
53-
Module: target.Addr.Module.Module(),
37+
// Try to find the resource config
38+
rsCfg := modCfg.Module.ResourceByAddr(target.Addr.Resource.Resource)
39+
if rsCfg != nil {
40+
// Get the provider FQN for the resource from the resource configuration
41+
providerAddr.Provider = rsCfg.Provider
42+
43+
// Get the alias from the resource's provider local config
44+
providerAddr.Alias = rsCfg.ProviderConfigAddr().Alias
45+
} else {
46+
// Resource has no matching config, so use an implied provider
47+
// based on the resource type
48+
rsProviderType := target.Addr.Resource.Resource.ImpliedProvider()
49+
providerAddr.Provider = modCfg.Module.ImpliedProviderForUnqualifiedType(rsProviderType)
5450
}
5551

5652
node := &graphNodeImportState{

0 commit comments

Comments
 (0)