mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-02 12:17:39 -06:00
a3403f2766
Due to how often the state and plan types are referenced throughout Terraform, there isn't a great way to switch them out gradually. As a consequence, this huge commit gets us from the old world to a _compilable_ new world, but still has a large number of known test failures due to key functionality being stubbed out. The stubs here are for anything that interacts with providers, since we now need to do the follow-up work to similarly replace the old terraform.ResourceProvider interface with its replacement in the new "providers" package. That work, along with work to fix the remaining failing tests, will follow in subsequent commits. The aim here was to replace all references to terraform.State and its downstream types with states.State, terraform.Plan with plans.Plan, state.State with statemgr.State, and switch to the new implementations of the state and plan file formats. However, due to the number of times those types are used, this also ended up affecting numerous other parts of core such as terraform.Hook, the backend.Backend interface, and most of the CLI commands. Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize in advance to the person who inevitably just found this huge commit while spelunking through the commit history.
91 lines
2.6 KiB
Go
91 lines
2.6 KiB
Go
package terraform
|
|
|
|
import (
|
|
"github.com/hashicorp/terraform/configs"
|
|
"github.com/hashicorp/terraform/dag"
|
|
"github.com/hashicorp/terraform/states"
|
|
)
|
|
|
|
// OrphanResourceTransformer is a GraphTransformer that adds resource
|
|
// orphans to the graph. A resource orphan is a resource that is
|
|
// represented in the state but not in the configuration.
|
|
//
|
|
// This only adds orphans that have no representation at all in the
|
|
// configuration.
|
|
type OrphanResourceTransformer struct {
|
|
Concrete ConcreteResourceInstanceNodeFunc
|
|
|
|
// State is the global state. We require the global state to
|
|
// properly find module orphans at our path.
|
|
State *states.State
|
|
|
|
// Config is the root node in the configuration tree. We'll look up
|
|
// the appropriate note in this tree using the path in each node.
|
|
Config *configs.Config
|
|
}
|
|
|
|
func (t *OrphanResourceTransformer) Transform(g *Graph) error {
|
|
if t.State == nil {
|
|
// If the entire state is nil, there can't be any orphans
|
|
return nil
|
|
}
|
|
if t.Config == nil {
|
|
// Should never happen: we can't be doing any Terraform operations
|
|
// without at least an empty configuration.
|
|
panic("OrpahResourceTransformer used without setting Config")
|
|
}
|
|
|
|
// Go through the modules and for each module transform in order
|
|
// to add the orphan.
|
|
for _, ms := range t.State.Modules {
|
|
if err := t.transform(g, ms); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *OrphanResourceTransformer) transform(g *Graph, ms *states.Module) error {
|
|
if ms == nil {
|
|
return nil
|
|
}
|
|
|
|
moduleAddr := ms.Addr
|
|
|
|
// Get the configuration for this module. The configuration might be
|
|
// nil if the module was removed from the configuration. This is okay,
|
|
// this just means that every resource is an orphan.
|
|
var m *configs.Module
|
|
if c := t.Config.DescendentForInstance(moduleAddr); c != nil {
|
|
m = c.Module
|
|
}
|
|
|
|
// An "orphan" is a resource that is in the state but not the configuration,
|
|
// so we'll walk the state resources and try to correlate each of them
|
|
// with a configuration block. Each orphan gets a node in the graph whose
|
|
// type is decided by t.Concrete.
|
|
//
|
|
// We don't handle orphans related to changes in the "count" and "for_each"
|
|
// pseudo-arguments here. They are handled by OrphanResourceCountTransformer.
|
|
for _, rs := range ms.Resources {
|
|
if m != nil {
|
|
if r := m.ResourceByAddr(rs.Addr); r != nil {
|
|
continue
|
|
}
|
|
}
|
|
|
|
for key := range rs.Instances {
|
|
addr := rs.Addr.Instance(key).Absolute(moduleAddr)
|
|
abstract := NewNodeAbstractResourceInstance(addr)
|
|
var node dag.Vertex = abstract
|
|
if f := t.Concrete; f != nil {
|
|
node = f(abstract)
|
|
}
|
|
g.Add(node)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|