diff --git a/terraform/context_import_test.go b/terraform/context_import_test.go index 1f9489c18..f344fac95 100644 --- a/terraform/context_import_test.go +++ b/terraform/context_import_test.go @@ -40,6 +40,40 @@ func TestContextImport_basic(t *testing.T) { } } +func TestContextImport_countIndex(t *testing.T) { + p := testProvider("aws") + ctx := testContext2(t, &ContextOpts{ + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + p.ImportStateReturn = []*InstanceState{ + &InstanceState{ + ID: "foo", + Ephemeral: EphemeralState{Type: "aws_instance"}, + }, + } + + state, err := ctx.Import(&ImportOpts{ + Targets: []*ImportTarget{ + &ImportTarget{ + Addr: "aws_instance.foo[0]", + ID: "bar", + }, + }, + }) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testImportCountIndexStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + func TestContextImport_collision(t *testing.T) { p := testProvider("aws") ctx := testContext2(t, &ContextOpts{ @@ -508,6 +542,12 @@ aws_instance.foo: provider = aws ` +const testImportCountIndexStr = ` +aws_instance.foo.0: + ID = foo + provider = aws +` + const testImportCollisionStr = ` aws_instance.foo: ID = bar diff --git a/terraform/transform_import_state.go b/terraform/transform_import_state.go index 2324aea0f..389c0464e 100644 --- a/terraform/transform_import_state.go +++ b/terraform/transform_import_state.go @@ -204,7 +204,7 @@ func (n *graphNodeImportStateSub) EvalTree() EvalNode { key := &ResourceStateKey{ Name: n.Target.Name, Type: info.Type, - Index: -1, + Index: n.Target.Index, } // The eval sequence