mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 17:01:04 -06:00
334c6f1c2c
Previously our handling of create_before_destroy -- and of deposed objects in particular -- was rather "implicit" and spread over various different subsystems. We'd quietly just destroy every deposed object during a destroy operation, without any user-visible plan to do so. Here we make things more explicit by tracking each deposed object individually by its pseudorandomly-allocated key. There are two different mechanisms at play here, building on the same concepts: - During a replace operation with create_before_destroy, we *pre-allocate* a DeposedKey to use for the prior object in the "apply" node and then pass that exact id to the destroy node, ensuring that we only destroy the single object we planned to destroy. In the happy path here the user never actually sees the allocated deposed key because we use it and then immediately destroy it within the same operation. However, that destroy may fail, which brings us to the second mechanism: - If any deposed objects are already present in state during _plan_, we insert a destroy change for them into the plan so that it's explicit to the user that we are going to destroy these additional objects, and then create an individual graph node for each one in DiffTransformer. The main motivation here is to be more careful in how we handle these destroys so that from a user's standpoint we never destroy something without the user knowing about it ahead of time. However, this new organization also hopefully makes the code itself a little easier to follow because the connection between the create and destroy steps of a Replace is reprseented in a single place (in DiffTransformer) and deposed instances each have their own explicit graph node rather than being secretly handled as part of the main instance-level graph node.
75 lines
2.4 KiB
Go
75 lines
2.4 KiB
Go
package terraform
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/hashicorp/terraform/states"
|
|
)
|
|
|
|
// StateTransformer is a GraphTransformer that adds the elements of
|
|
// the state to the graph.
|
|
//
|
|
// This transform is used for example by the DestroyPlanGraphBuilder to ensure
|
|
// that only resources that are in the state are represented in the graph.
|
|
type StateTransformer struct {
|
|
// ConcreteCurrent and ConcreteDeposed are used to specialize the abstract
|
|
// resource instance nodes that this transformer will create.
|
|
//
|
|
// If either of these is nil, the objects of that type will be skipped and
|
|
// not added to the graph at all. It doesn't make sense to use this
|
|
// transformer without setting at least one of these, since that would
|
|
// skip everything and thus be a no-op.
|
|
ConcreteCurrent ConcreteResourceInstanceNodeFunc
|
|
ConcreteDeposed ConcreteResourceInstanceDeposedNodeFunc
|
|
|
|
State *states.State
|
|
}
|
|
|
|
func (t *StateTransformer) Transform(g *Graph) error {
|
|
if !t.State.HasResources() {
|
|
log.Printf("[TRACE] StateTransformer: state is empty, so nothing to do")
|
|
return nil
|
|
}
|
|
|
|
switch {
|
|
case t.ConcreteCurrent != nil && t.ConcreteDeposed != nil:
|
|
log.Printf("[TRACE] StateTransformer: creating nodes for both current and deposed instance objects")
|
|
case t.ConcreteCurrent != nil:
|
|
log.Printf("[TRACE] StateTransformer: creating nodes for current instance objects only")
|
|
case t.ConcreteDeposed != nil:
|
|
log.Printf("[TRACE] StateTransformer: creating nodes for deposed instance objects only")
|
|
default:
|
|
log.Printf("[TRACE] StateTransformer: pointless no-op call, creating no nodes at all")
|
|
}
|
|
|
|
for _, ms := range t.State.Modules {
|
|
moduleAddr := ms.Addr
|
|
|
|
for _, rs := range ms.Resources {
|
|
resourceAddr := rs.Addr.Absolute(moduleAddr)
|
|
|
|
for key, is := range rs.Instances {
|
|
addr := resourceAddr.Instance(key)
|
|
|
|
if obj := is.Current; obj != nil && t.ConcreteCurrent != nil {
|
|
abstract := NewNodeAbstractResourceInstance(addr)
|
|
node := t.ConcreteCurrent(abstract)
|
|
g.Add(node)
|
|
log.Printf("[TRACE] StateTransformer: added %T for %s current object", node, addr)
|
|
}
|
|
|
|
if t.ConcreteDeposed != nil {
|
|
for dk := range is.Deposed {
|
|
abstract := NewNodeAbstractResourceInstance(addr)
|
|
node := t.ConcreteDeposed(abstract, dk)
|
|
g.Add(node)
|
|
log.Printf("[TRACE] StateTransformer: added %T for %s deposed object %s", node, addr, dk)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|