From f8b1a3c7a4134bee662dd94ec78882c88651858a Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 15 Oct 2018 21:13:05 -0400 Subject: [PATCH] make sure apply can properly destroy We need to ensure that a destroyed value is returned as such from apply --- helper/plugin/grpc_provider.go | 47 ++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 13 deletions(-) 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 }