make sure apply can properly destroy

We need to ensure that a destroyed value is returned as such from apply
This commit is contained in:
James Bardin 2018-10-15 21:13:05 -04:00 committed by Martin Atkins
parent 3b6deef296
commit f8b1a3c7a4
1 changed files with 34 additions and 13 deletions

View File

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