Merge pull request #21764 from hashicorp/jbardin/normalize-import-blocks

normalize empty blocks during import
This commit is contained in:
James Bardin 2019-06-18 07:14:37 -04:00 committed by GitHub
commit c7058eaa52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)