From 8ae31aa2db661f92215eef28273a98f2550fd827 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 17 Jun 2019 20:27:12 -0400 Subject: [PATCH] normalize empty blocks during import Like Upgrade, Import is another case where we don't have the context of the configuration and need to ensure missing blocks are normalized. --- .../test/resource_nested_set_test.go | 40 +++++++++++++++++++ helper/plugin/grpc_provider.go | 3 ++ 2 files changed, 43 insertions(+) diff --git a/builtin/providers/test/resource_nested_set_test.go b/builtin/providers/test/resource_nested_set_test.go index e782bb7fb..fac38ef6d 100644 --- a/builtin/providers/test/resource_nested_set_test.go +++ b/builtin/providers/test/resource_nested_set_test.go @@ -30,6 +30,46 @@ resource "test_resource_nested_set" "foo" { }) } +func TestResourceNestedSet_basicImport(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_nested_set" "foo" { + single { + value = "bar" + } +} + `), + }, + resource.TestStep{ + ImportState: true, + ResourceName: "test_resource_nested_set.foo", + Config: strings.TrimSpace(` +resource "test_resource_nested_set" "foo" { + single { + value = "bar" + } +} + `), + ImportStateCheck: func(ss []*terraform.InstanceState) error { + for _, s := range ss { + if s.Attributes["multi.#"] != "0" || + s.Attributes["single.#"] != "0" || + s.Attributes["type_list.#"] != "0" || + s.Attributes["with_list.#"] != "0" { + return fmt.Errorf("missing blocks in imported state:\n%s", s) + } + } + return nil + }, + }, + }, + }) +} + // The set should not be generated because of it's computed value func TestResourceNestedSet_noSet(t *testing.T) { checkFunc := func(s *terraform.State) error { diff --git a/helper/plugin/grpc_provider.go b/helper/plugin/grpc_provider.go index bc4883cb9..db9d12cfb 100644 --- a/helper/plugin/grpc_provider.go +++ b/helper/plugin/grpc_provider.go @@ -967,6 +967,9 @@ func (s *GRPCProviderServer) ImportResourceState(_ context.Context, req *proto.I return resp, nil } + // Normalize the value and fill in any missing blocks. + newStateVal = objchange.NormalizeObjectFromLegacySDK(newStateVal, schemaBlock) + newStateMP, err := msgpack.Marshal(newStateVal, schemaBlock.ImpliedType()) if err != nil { resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)