destroy provisioner cannot re-evaluate for_each

During destroy, the for expression may be unknown and evaluation will
fail. Destroy provisioners however can only reference the key value,
which is known in the address.
This commit is contained in:
James Bardin 2020-02-17 16:07:38 -05:00
parent 8421abaca0
commit 953ada1cf8
2 changed files with 20 additions and 17 deletions

View File

@ -561,8 +561,15 @@ func (n *EvalApplyProvisioners) apply(ctx EvalContext, provs []*configs.Provisio
provisioner := ctx.Provisioner(prov.Type) provisioner := ctx.Provisioner(prov.Type)
schema := ctx.ProvisionerSchema(prov.Type) schema := ctx.ProvisionerSchema(prov.Type)
forEach, forEachDiags := evaluateResourceForEachExpression(n.ResourceConfig.ForEach, ctx) var forEach map[string]cty.Value
diags = diags.Append(forEachDiags)
// We can't evaluate the for_each expression during a destroy
if n.When != configs.ProvisionerWhenDestroy {
m, forEachDiags := evaluateResourceForEachExpression(n.ResourceConfig.ForEach, ctx)
diags = diags.Append(forEachDiags)
forEach = m
}
keyData := EvalDataForInstanceKey(instanceAddr.Key, forEach) keyData := EvalDataForInstanceKey(instanceAddr.Key, forEach)
// Evaluate the main provisioner configuration. // Evaluate the main provisioner configuration.

View File

@ -105,24 +105,20 @@ type InstanceKeyEvalData = instances.RepetitionData
// EvalDataForInstanceKey constructs a suitable InstanceKeyEvalData for // EvalDataForInstanceKey constructs a suitable InstanceKeyEvalData for
// evaluating in a context that has the given instance key. // evaluating in a context that has the given instance key.
func EvalDataForInstanceKey(key addrs.InstanceKey, forEachMap map[string]cty.Value) InstanceKeyEvalData { func EvalDataForInstanceKey(key addrs.InstanceKey, forEachMap map[string]cty.Value) InstanceKeyEvalData {
var countIdx cty.Value var evalData InstanceKeyEvalData
var eachKey cty.Value if key == nil {
var eachVal cty.Value return evalData
if intKey, ok := key.(addrs.IntKey); ok {
countIdx = cty.NumberIntVal(int64(intKey))
} }
if stringKey, ok := key.(addrs.StringKey); ok { keyValue := key.Value()
eachKey = cty.StringVal(string(stringKey)) switch keyValue.Type() {
eachVal = forEachMap[string(stringKey)] case cty.String:
} evalData.EachKey = keyValue
evalData.EachValue = forEachMap[keyValue.AsString()]
return InstanceKeyEvalData{ case cty.Number:
CountIndex: countIdx, evalData.CountIndex = keyValue
EachKey: eachKey,
EachValue: eachVal,
} }
return evalData
} }
// EvalDataForNoInstanceKey is a value of InstanceKeyData that sets no instance // EvalDataForNoInstanceKey is a value of InstanceKeyData that sets no instance