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) var diff *terraform.InstanceDiff
if err != nil { destroy := false
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
return resp, nil // 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 { if diff == nil {
@ -516,10 +529,16 @@ func (s *GRPCProviderServer) ApplyResourceChange(_ context.Context, req *proto.A
return resp, nil return resp, nil
} }
newStateVal, err := schema.StateValueFromInstanceState(newInstanceState, block.ImpliedType()) newStateVal := cty.NullVal(block.ImpliedType())
if err != nil {
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err) // We keep the null val if we destroyed the resource, otherwise build the
return resp, nil // 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()) newStateMP, err := msgpack.Marshal(newStateVal, block.ImpliedType())
@ -531,12 +550,14 @@ func (s *GRPCProviderServer) ApplyResourceChange(_ context.Context, req *proto.A
Msgpack: newStateMP, Msgpack: newStateMP,
} }
meta, err := json.Marshal(newInstanceState.Meta) if newInstanceState != nil {
if err != nil { meta, err := json.Marshal(newInstanceState.Meta)
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err) if err != nil {
return resp, nil resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
return resp, nil
}
resp.Private = meta
} }
resp.Private = meta
return resp, nil return resp, nil
} }