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)
schema := ctx.ProvisionerSchema(prov.Type)
forEach, forEachDiags := evaluateResourceForEachExpression(n.ResourceConfig.ForEach, ctx)
diags = diags.Append(forEachDiags)
var forEach map[string]cty.Value
// 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)
// Evaluate the main provisioner configuration.

View File

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