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.
This commit is contained in:
James Bardin 2019-06-17 20:27:12 -04:00
parent e605b838ac
commit 8ae31aa2db
2 changed files with 43 additions and 0 deletions

View File

@ -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 {

View File

@ -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)