terraform: diff handler in expansion avoids duplicate destroy

This commit is contained in:
Armon Dadgar 2014-11-18 15:38:40 -08:00
parent 507b75449f
commit 01db1ff8bd
1 changed files with 30 additions and 12 deletions

View File

@ -1751,24 +1751,42 @@ func (n *GraphNodeResource) expand(g *depgraph.Graph, count int, diff *ModuleDif
} }
} }
// Initialize a default state if not available
if state == nil { if state == nil {
state = &ResourceState{ state = &ResourceState{
Type: r.Type, Type: r.Type,
} }
} }
if inDiff != nil && n.ExpandMode != ResourceExpandDestroy { // Prepare the diff if it exists
// Disable Destroy if we aren't doing a destroy expansion. if inDiff != nil {
// There is a seperate expansion for the destruction action. switch n.ExpandMode {
d := new(InstanceDiff) case ResourceExpandApply:
*d = *inDiff // Disable Destroy if we aren't doing a destroy expansion.
inDiff = d // There is a seperate expansion for the destruction action.
inDiff.Destroy = false d := new(InstanceDiff)
} else if inDiff != nil && inDiff.Destroy && n.ExpandMode == ResourceExpandDestroy { *d = *inDiff
// If we are doing a destroy, make sure it is exclusively inDiff = d
// a destroy, since there is a seperate expansion for the apply inDiff.Destroy = false
inDiff = new(InstanceDiff)
inDiff.Destroy = true // If we require a new resource, there is a seperate delete
// phase, so the create phase must not have access to the ID.
if inDiff.RequiresNew() {
s := new(ResourceState)
*s = *state
state = s
state.Primary = nil
}
case ResourceExpandDestroy:
// If we are doing a destroy, make sure it is exclusively
// a destroy, since there is a seperate expansion for the apply
inDiff = new(InstanceDiff)
inDiff.Destroy = true
default:
panic(fmt.Sprintf("Unhandled expansion mode %d", n.ExpandMode))
}
} }
// Inherit the existing flags! // Inherit the existing flags!