opentofu/terraform/transform_config.go

97 lines
2.2 KiB
Go
Raw Normal View History

package terraform
import (
"errors"
"fmt"
2016-11-05 19:53:12 -05:00
"log"
"github.com/hashicorp/terraform/config/module"
2016-11-05 19:53:12 -05:00
"github.com/hashicorp/terraform/dag"
)
2016-11-05 18:26:12 -05:00
// 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 {
2016-11-05 19:53:12 -05:00
Concrete ConcreteResourceNodeFunc
Module *module.Tree
}
func (t *ConfigTransformer) Transform(g *Graph) error {
2016-11-05 18:26:12 -05:00
// If no module is given, we don't do anything
if t.Module == nil {
2016-11-05 18:26:12 -05:00
return nil
}
2016-11-05 18:26:12 -05:00
// If the module isn't loaded, that is simply an error
if !t.Module.Loaded() {
2016-11-05 18:26:12 -05:00
return errors.New("module must be loaded for ConfigTransformer")
}
2016-11-05 19:53:12 -05:00
// Start the transformation process
return t.transform(g, t.Module)
}
func (t *ConfigTransformer) transform(g *Graph, m *module.Tree) error {
// If no config, do nothing
if m == nil {
return nil
}
2016-11-05 19:53:12 -05:00
// Add our resources
if err := t.transformSingle(g, m); err != nil {
return err
}
2016-11-05 19:53:12 -05:00
// Transform all the children.
for _, c := range m.Children() {
if err := t.transform(g, c); err != nil {
return err
}
2015-01-21 16:39:16 -06:00
}
2016-11-05 19:53:12 -05:00
return nil
}
2016-11-05 19:53:12 -05:00
func (t *ConfigTransformer) transformSingle(g *Graph, m *module.Tree) error {
log.Printf("[TRACE] ConfigTransformer: Starting for path: %v", m.Path())
2016-11-05 19:53:12 -05:00
// Get the configuration for this module
config := m.Config()
2016-11-05 19:53:12 -05:00
// Build the path we're at
path := m.Path()
2016-11-05 19:53:12 -05:00
// Write all the resources out
for _, r := range config.Resources {
// Build the resource address
addr, err := parseResourceAddressConfig(r)
if err != nil {
panic(fmt.Sprintf(
"Error parsing config address, this is a bug: %#v", r))
}
addr.Path = path
2015-01-23 17:11:11 -06:00
2016-11-05 19:53:12 -05:00
// Build the abstract node and the concrete one
abstract := &NodeAbstractResource{Addr: addr}
var node dag.Vertex = abstract
if f := t.Concrete; f != nil {
node = f(abstract)
}
2016-11-05 19:53:12 -05:00
// Add it to the graph
g.Add(node)
}
2016-11-05 19:53:12 -05:00
return nil
}