do not evaluate module variables during import

We do not currently need to evaluate module variables in order to
import a resource.

This will likely change once we can select the import provider
automatically, and have a more dynamic method for dispatching providers
to module instances. In the meantime we can avoid the evaluation for now
and prevent a certain class of import errors.
This commit is contained in:
James Bardin 2020-10-06 12:51:28 -04:00
parent 6281d4d420
commit 8049d2e028
2 changed files with 90 additions and 1 deletions

View File

@ -732,6 +732,95 @@ func TestContextImport_multiStateSame(t *testing.T) {
}
}
func TestContextImport_noConfigModuleImport(t *testing.T) {
p := testProvider("aws")
m := testModuleInline(t, map[string]string{
"main.tf": `
locals {
xs = toset(["foo"])
}
module "a" {
for_each = local.xs
source = "./a"
}
module "b" {
for_each = local.xs
source = "./b"
y = module.a[each.key].y
}
`,
"a/main.tf": `
output "y" {
value = "bar"
}
`,
"b/main.tf": `
variable "y" {
type = string
}
resource "test_resource" "unused" {
value = var.y
}
`,
})
p.GetSchemaReturn = &ProviderSchema{
Provider: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {Type: cty.String, Optional: true},
},
},
ResourceTypes: map[string]*configschema.Block{
"test_resource": {
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
},
},
},
}
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_resource",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("test"),
}),
},
},
}
ctx := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})
state, diags := ctx.Import(&ImportOpts{
Targets: []*ImportTarget{
&ImportTarget{
Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "test_resource", "test", addrs.NoKey,
),
ID: "test",
},
},
})
if diags.HasErrors() {
t.Fatal(diags.ErrWithWarnings())
}
ri := state.ResourceInstance(mustResourceInstanceAddr("test_resource.test"))
expected := `{"id":"test"}`
if string(ri.Current.AttrsJSON) != expected {
t.Fatalf("expected %q, got %q\n", expected, ri.Current.AttrsJSON)
}
}
const testImportStr = `
aws_instance.foo:
ID = foo

View File

@ -153,7 +153,7 @@ func (n *nodeModuleVariable) Execute(ctx EvalContext, op walkOperation) error {
var err error
switch op {
case walkPlan, walkApply, walkDestroy, walkImport:
case walkPlan, walkApply, walkDestroy:
vals, err = n.EvalModuleCallArgument(ctx, false)
if err != nil {
return err