mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-25 16:31:10 -06:00
c937c06a03
Due to how deeply the configuration types go into Terraform Core, there isn't a great way to switch out to HCL2 gradually. As a consequence, this huge commit gets us from the old state to a _compilable_ new state, but does not yet attempt to fix any tests and has a number of known missing parts and bugs. We will continue to iterate on this in forthcoming commits, heading back towards passing tests and making Terraform fully-functional again. The three main goals here are: - Use the configuration models from the "configs" package instead of the older models in the "config" package, which is now deprecated and preserved only to help us write our migration tool. - Do expression inspection and evaluation using the functionality of the new "lang" package, instead of the Interpolator type and related functionality in the main "terraform" package. - Represent addresses of various objects using types in the addrs package, rather than hand-constructed strings. This is not critical to support the above, but was a big help during the implementation of these other points since it made it much more explicit what kind of address is expected in each context. Since our new packages are built to accommodate some future planned features that are not yet implemented (e.g. the "for_each" argument on resources, "count"/"for_each" on modules), and since there's still a fair amount of functionality still using old-style APIs, there is a moderate amount of shimming here to connect new assumptions with old, hopefully in a way that makes it easier to find and eliminate these shims later. I apologize in advance to the person who inevitably just found this huge commit while spelunking through the commit history.
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package terraform
|
|
|
|
import (
|
|
"github.com/hashicorp/terraform/configs"
|
|
"github.com/hashicorp/terraform/dag"
|
|
)
|
|
|
|
// 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 *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
|
|
}
|
|
|
|
// 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 *ModuleState) error {
|
|
if ms == nil {
|
|
return nil
|
|
}
|
|
|
|
path := normalizeModulePath(ms.Path)
|
|
|
|
// Get the configuration for this path. 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(path); c != nil {
|
|
m = c.Module
|
|
}
|
|
|
|
// Go through the orphans and add them all to the state
|
|
for _, relAddr := range ms.Orphans(m) {
|
|
addr := relAddr.Absolute(path)
|
|
abstract := NewNodeAbstractResourceInstance(addr)
|
|
var node dag.Vertex = abstract
|
|
if f := t.Concrete; f != nil {
|
|
node = f(abstract)
|
|
}
|
|
g.Add(node)
|
|
}
|
|
|
|
return nil
|
|
}
|