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

@ -38,7 +38,6 @@ func TestContextImport_basic(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -78,7 +77,6 @@ func TestContextImport_countIndex(t *testing.T) {
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"),
}, },
}, },
}) })
@ -136,7 +134,6 @@ func TestContextImport_collision(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -180,7 +177,6 @@ func TestContextImport_missingType(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -234,7 +230,6 @@ func TestContextImport_moduleProvider(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -292,7 +287,6 @@ func TestContextImport_providerModule(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -350,7 +344,6 @@ func TestContextImport_providerVarConfig(t *testing.T) {
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)
} }