mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 17:01:04 -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.
134 lines
3.4 KiB
Go
134 lines
3.4 KiB
Go
package terraform
|
|
|
|
import (
|
|
"log"
|
|
"sync"
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/hashicorp/terraform/configs"
|
|
"github.com/hashicorp/terraform/dag"
|
|
)
|
|
|
|
// ConfigTransformer is a GraphTransformer that adds all the resources
|
|
// from the configuration to the graph.
|
|
//
|
|
// The module used to configure this transformer must be the root module.
|
|
//
|
|
// Only resources are added to the graph. Variables, outputs, and
|
|
// providers must be added via other transforms.
|
|
//
|
|
// Unlike ConfigTransformerOld, this transformer creates a graph with
|
|
// all resources including module resources, rather than creating module
|
|
// nodes that are then "flattened".
|
|
type ConfigTransformer struct {
|
|
Concrete ConcreteResourceNodeFunc
|
|
|
|
// Module is the module to add resources from.
|
|
Config *configs.Config
|
|
|
|
// Unique will only add resources that aren't already present in the graph.
|
|
Unique bool
|
|
|
|
// Mode will only add resources that match the given mode
|
|
ModeFilter bool
|
|
Mode addrs.ResourceMode
|
|
|
|
l sync.Mutex
|
|
uniqueMap map[string]struct{}
|
|
}
|
|
|
|
func (t *ConfigTransformer) Transform(g *Graph) error {
|
|
// Lock since we use some internal state
|
|
t.l.Lock()
|
|
defer t.l.Unlock()
|
|
|
|
// If no configuration is available, we don't do anything
|
|
if t.Config == nil {
|
|
return nil
|
|
}
|
|
|
|
// Reset the uniqueness map. If we're tracking uniques, then populate
|
|
// it with addresses.
|
|
t.uniqueMap = make(map[string]struct{})
|
|
defer func() { t.uniqueMap = nil }()
|
|
if t.Unique {
|
|
for _, v := range g.Vertices() {
|
|
if rn, ok := v.(GraphNodeResource); ok {
|
|
t.uniqueMap[rn.ResourceAddr().String()] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Start the transformation process
|
|
return t.transform(g, t.Config)
|
|
}
|
|
|
|
func (t *ConfigTransformer) transform(g *Graph, config *configs.Config) error {
|
|
// If no config, do nothing
|
|
if config == nil {
|
|
return nil
|
|
}
|
|
|
|
// Add our resources
|
|
if err := t.transformSingle(g, config); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Transform all the children.
|
|
for _, c := range config.Children {
|
|
if err := t.transform(g, c); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config) error {
|
|
path := config.Path
|
|
module := config.Module
|
|
log.Printf("[TRACE] ConfigTransformer: Starting for path: %v", path)
|
|
|
|
// For now we assume that each module call produces only one module
|
|
// instance with no key, since we don't yet support "count" and "for_each"
|
|
// on modules.
|
|
// FIXME: As part of supporting "count" and "for_each" on modules, rework
|
|
// this so that we'll "expand" the module call first and then create graph
|
|
// nodes for each module instance separately.
|
|
instPath := path.UnkeyedInstanceShim()
|
|
|
|
allResources := make([]*configs.Resource, 0, len(module.ManagedResources)+len(module.DataResources))
|
|
for _, r := range module.ManagedResources {
|
|
allResources = append(allResources, r)
|
|
}
|
|
for _, r := range module.DataResources {
|
|
allResources = append(allResources, r)
|
|
}
|
|
|
|
for _, r := range allResources {
|
|
relAddr := r.Addr()
|
|
|
|
if t.ModeFilter && relAddr.Mode != t.Mode {
|
|
// Skip non-matching modes
|
|
continue
|
|
}
|
|
|
|
addr := relAddr.Absolute(instPath)
|
|
if _, ok := t.uniqueMap[addr.String()]; ok {
|
|
// We've already seen a resource with this address. This should
|
|
// never happen, because we enforce uniqueness in the config loader.
|
|
continue
|
|
}
|
|
|
|
abstract := &NodeAbstractResource{Addr: addr}
|
|
var node dag.Vertex = abstract
|
|
if f := t.Concrete; f != nil {
|
|
node = f(abstract)
|
|
}
|
|
|
|
g.Add(node)
|
|
}
|
|
|
|
return nil
|
|
}
|