diff --git a/helper/plugin/grpc_provider.go b/helper/plugin/grpc_provider.go index 83ab81f84..d99e8bb29 100644 --- a/helper/plugin/grpc_provider.go +++ b/helper/plugin/grpc_provider.go @@ -496,10 +496,23 @@ func (s *GRPCProviderServer) ApplyResourceChange(_ context.Context, req *proto.A } } - diff, err := schema.DiffFromValues(priorStateVal, plannedStateVal, res) - if err != nil { - resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err) - return resp, nil + var diff *terraform.InstanceDiff + destroy := false + + // a null state means we are destroying the instance + if plannedStateVal.IsNull() { + destroy = true + diff = &terraform.InstanceDiff{ + Attributes: make(map[string]*terraform.ResourceAttrDiff), + Meta: make(map[string]interface{}), + Destroy: true, + } + } else { + diff, err = schema.DiffFromValues(priorStateVal, plannedStateVal, res) + if err != nil { + resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err) + return resp, nil + } } if diff == nil { @@ -516,10 +529,16 @@ func (s *GRPCProviderServer) ApplyResourceChange(_ context.Context, req *proto.A return resp, nil } - newStateVal, err := schema.StateValueFromInstanceState(newInstanceState, block.ImpliedType()) - if err != nil { - resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err) - return resp, nil + newStateVal := cty.NullVal(block.ImpliedType()) + + // We keep the null val if we destroyed the resource, otherwise build the + // entire object, even if the new state was nil. + if !destroy { + newStateVal, err = schema.StateValueFromInstanceState(newInstanceState, block.ImpliedType()) + if err != nil { + resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err) + return resp, nil + } } newStateMP, err := msgpack.Marshal(newStateVal, block.ImpliedType()) @@ -531,12 +550,14 @@ func (s *GRPCProviderServer) ApplyResourceChange(_ context.Context, req *proto.A Msgpack: newStateMP, } - meta, err := json.Marshal(newInstanceState.Meta) - if err != nil { - resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err) - return resp, nil + if newInstanceState != nil { + meta, err := json.Marshal(newInstanceState.Meta) + if err != nil { + resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err) + return resp, nil + } + resp.Private = meta } - resp.Private = meta return resp, nil }