add implied providers during import

The CLI adds the provider references during import, but tests may not
have them specified.
This commit is contained in:
James Bardin 2018-12-04 15:39:46 -05:00
parent a65624e0c1
commit 7f9d76cbf5
3 changed files with 23 additions and 15 deletions

View File

@ -37,8 +37,7 @@ func TestContextImport_basic(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -77,8 +76,7 @@ func TestContextImport_countIndex(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.IntKey(0), addrs.ManagedResourceMode, "aws_instance", "foo", addrs.IntKey(0),
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -135,8 +133,7 @@ func TestContextImport_collision(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -179,8 +176,7 @@ func TestContextImport_missingType(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -233,8 +229,7 @@ func TestContextImport_moduleProvider(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -291,8 +286,7 @@ func TestContextImport_providerModule(t *testing.T) {
Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).ResourceInstance( Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -349,8 +343,7 @@ func TestContextImport_providerVarConfig(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })

View File

@ -0,0 +1,7 @@
provider "aws" {
foo = data.template_data_source.d.foo
}
data "template_data_source" "d" {
foo = "bar"
}

View File

@ -16,10 +16,18 @@ type ImportStateTransformer struct {
func (t *ImportStateTransformer) Transform(g *Graph) error { func (t *ImportStateTransformer) Transform(g *Graph) error {
for _, target := range t.Targets { for _, target := range t.Targets {
// The ProviderAddr may not be supplied for non-aliased providers.
// This will be populated if the targets come from the cli, but tests
// may not specify implied provider addresses.
providerAddr := target.ProviderAddr
if providerAddr.ProviderConfig.Type == "" {
providerAddr = target.Addr.Resource.Resource.DefaultProviderConfig().Absolute(target.Addr.Module)
}
node := &graphNodeImportState{ node := &graphNodeImportState{
Addr: target.Addr, Addr: target.Addr,
ID: target.ID, ID: target.ID,
ProviderAddr: target.ProviderAddr, ProviderAddr: providerAddr,
} }
g.Add(node) g.Add(node)
} }