Merge pull request #25352 from hashicorp/alisdair/fix-import-allow-missing-config

command/import: Fix allow-missing-config option
This commit is contained in:
Alisdair McDiarmid 2020-06-24 13:37:13 -04:00 committed by GitHub
commit f714a9da79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 61 deletions

View File

@ -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)
@ -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) {

View File

@ -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")

View File

@ -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)
}
// Get the provider local config for the resource
localpCfg := rsCfg.ProviderConfigAddr()
providerAddr := addrs.AbsProviderConfig{
Provider: providerFqn,
Alias: localpCfg.Alias,
Module: target.Addr.Module.Module(),
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{