Skip to content

Commit

Permalink
fix normalizeNullValues when apply resource to accept an empty list
Browse files Browse the repository at this point in the history
and doesn't use a null src otherwise a drift is generate when refresh resource
Fixes #766
  • Loading branch information
jeremmfr committed Jun 18, 2021
1 parent 7a6c389 commit a2dafcf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions helper/schema/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,13 +1261,17 @@ func normalizeNullValues(dst, src cty.Value, apply bool) cty.Value {
}

// Handle null/empty changes for collections during apply.
// A change between null and empty values prefers src to make sure the state
// A change from emtpy to null values prefers src to make sure the state
// is consistent between plan and apply.
// A change from null to empty values prefers dst to not generate drifts of
// values when refresh.
if ty.IsCollectionType() && apply {
dstEmpty := !dst.IsNull() && dst.IsKnown() && dst.LengthInt() == 0
srcEmpty := !src.IsNull() && src.IsKnown() && src.LengthInt() == 0

if (src.IsNull() && dstEmpty) || (srcEmpty && dst.IsNull()) {
if src.IsNull() && dstEmpty {
return dst
} else if srcEmpty && dst.IsNull() {
return src
}
}
Expand Down
6 changes: 3 additions & 3 deletions helper/schema/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,7 @@ func TestNormalizeNullValues(t *testing.T) {
"network_interface": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"network_ip": cty.StringVal("10.128.0.64"),
"access_config": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{"public_ptr_domain_name": cty.String, "nat_ip": cty.String}))),
"access_config": cty.ListValEmpty(cty.Object(map[string]cty.Type{"public_ptr_domain_name": cty.String, "nat_ip": cty.String})),
"address": cty.StringVal("address"),
"name": cty.StringVal("nic0"),
}),
Expand Down Expand Up @@ -1529,9 +1529,9 @@ func TestNormalizeNullValues(t *testing.T) {
})),
}),
Expect: cty.ObjectVal(map[string]cty.Value{
"set": cty.NullVal(cty.Set(cty.Object(map[string]cty.Type{
"set": cty.SetValEmpty(cty.Object(map[string]cty.Type{
"list": cty.List(cty.String),
}))),
})),
}),
Apply: true,
},
Expand Down

0 comments on commit a2dafcf

Please sign in to comment.