mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-29 10:21:01 -06:00
bee7403f3e
Previously we would reject attempts to delete a workspace if its state contained any resources at all, even if none of the resources had any resource instance objects associated with it. Nowadays there isn't any situation where the normal Terraform workflow will leave behind resource husks, and so this isn't as problematic as it might've been in the v0.12 era, but nonetheless what we actually care about for this check is whether there might be any remote objects that this state is tracking, and for that it's more precise to look for non-nil resource instance objects, rather than whole resources. This also includes some adjustments to our error messaging to give more information about the problem and to use terminology more consistent with how we currently talk about this situation in our documentation and elsewhere in the UI. We were also using the old State.HasResources method as part of some of our tests. I considered preserving it to avoid changing the behavior of those tests, but the new check seemed close enough to the intent of those tests that it wasn't worth maintaining this method that wouldn't be used in any main code anymore. I've therefore updated those tests to use the new HasResourceInstanceObjects method instead.
73 lines
2.3 KiB
Go
73 lines
2.3 KiB
Go
package terraform
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/hashicorp/terraform/internal/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 == nil {
|
|
log.Printf("[TRACE] StateTransformer: state is nil, 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 {
|
|
for _, rs := range ms.Resources {
|
|
resourceAddr := rs.Addr
|
|
|
|
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
|
|
}
|