core: Tolerate the prior state being nil in EvalDiff

This happens legitimately in situations where we'll be creating an object
for this resource instance for the first time.
This commit is contained in:
Martin Atkins 2018-08-27 12:09:46 -07:00
parent 7d760c09fb
commit ec11efc50a
1 changed files with 11 additions and 4 deletions

View File

@ -97,9 +97,6 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
absAddr := n.Addr.Absolute(ctx.Path())
priorVal := state.Value
// Evaluate the configuration // Evaluate the configuration
schema := providerSchema.ResourceTypes[n.Addr.Resource.Type] schema := providerSchema.ResourceTypes[n.Addr.Resource.Type]
if schema == nil { if schema == nil {
@ -113,6 +110,16 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
return nil, diags.Err() return nil, diags.Err()
} }
absAddr := n.Addr.Absolute(ctx.Path())
var priorVal cty.Value
var priorPrivate []byte
if state != nil {
priorVal = state.Value
priorPrivate = state.Private
} else {
priorVal = cty.NullVal(schema.ImpliedType())
}
proposedNewVal := objchange.ProposedNewObject(schema, priorVal, configVal) proposedNewVal := objchange.ProposedNewObject(schema, priorVal, configVal)
// Call pre-diff hook // Call pre-diff hook
@ -132,7 +139,7 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
Config: configVal, Config: configVal,
PriorState: priorVal, PriorState: priorVal,
ProposedNewState: proposedNewVal, ProposedNewState: proposedNewVal,
PriorPrivate: state.Private, PriorPrivate: priorPrivate,
}) })
diags = diags.Append(resp.Diagnostics.InConfigBody(config.Config)) diags = diags.Append(resp.Diagnostics.InConfigBody(config.Config))
if diags.HasErrors() { if diags.HasErrors() {