mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-25 08:21:07 -06:00
terraform: FlatConfigTransformer
This commit is contained in:
parent
6622ca001d
commit
3d4937b784
@ -0,0 +1 @@
|
||||
resource "aws_instance" "baz" {}
|
@ -0,0 +1,6 @@
|
||||
resource "aws_instance" "foo" {}
|
||||
resource "aws_instance" "bar" { value = "${aws_instance.foo.value}" }
|
||||
|
||||
module "child" {
|
||||
source = "./child"
|
||||
}
|
72
terraform/transform_config_flat.go
Normal file
72
terraform/transform_config_flat.go
Normal file
@ -0,0 +1,72 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/hashicorp/terraform/config/module"
|
||||
"github.com/hashicorp/terraform/dag"
|
||||
)
|
||||
|
||||
// FlatConfigTransformer is a GraphTransformer that adds the configuration
|
||||
// to the graph. The module used to configure this transformer must be
|
||||
// the root module.
|
||||
//
|
||||
// In relation to ConfigTransformer: this is a newer generation config
|
||||
// transformer. It puts the _entire_ config into the graph (there is no
|
||||
// "flattening" step as before).
|
||||
type FlatConfigTransformer struct {
|
||||
Concrete ConcreteResourceNodeFunc // What to turn resources into
|
||||
|
||||
Module *module.Tree
|
||||
}
|
||||
|
||||
func (t *FlatConfigTransformer) Transform(g *Graph) error {
|
||||
// If no module, we do nothing
|
||||
if t.Module == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// If the module is not loaded, that is an error
|
||||
if !t.Module.Loaded() {
|
||||
return errors.New("module must be loaded")
|
||||
}
|
||||
|
||||
return t.transform(g, t.Module)
|
||||
}
|
||||
|
||||
func (t *FlatConfigTransformer) transform(g *Graph, m *module.Tree) error {
|
||||
// If no module, no problem
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Transform all the children.
|
||||
for _, c := range m.Children() {
|
||||
if err := t.transform(g, c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Get the configuration for this module
|
||||
config := m.Config()
|
||||
|
||||
// Write all the resources out
|
||||
for _, r := range config.Resources {
|
||||
// Grab the address for this resource
|
||||
addr, err := parseResourceAddressConfig(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
addr.Path = m.Path()
|
||||
|
||||
abstract := &NodeAbstractResource{Addr: addr}
|
||||
var node dag.Vertex = abstract
|
||||
if f := t.Concrete; f != nil {
|
||||
node = f(abstract)
|
||||
}
|
||||
|
||||
g.Add(node)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
40
terraform/transform_config_flat_test.go
Normal file
40
terraform/transform_config_flat_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFlatConfigTransformer_nilModule(t *testing.T) {
|
||||
g := Graph{Path: RootModulePath}
|
||||
tf := &FlatConfigTransformer{}
|
||||
if err := tf.Transform(&g); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
if len(g.Vertices()) > 0 {
|
||||
t.Fatal("graph should be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlatConfigTransformer(t *testing.T) {
|
||||
g := Graph{Path: RootModulePath}
|
||||
tf := &FlatConfigTransformer{
|
||||
Module: testModule(t, "transform-flat-config-basic"),
|
||||
}
|
||||
if err := tf.Transform(&g); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(g.String())
|
||||
expected := strings.TrimSpace(testTransformFlatConfigBasicStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad:\n\n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
const testTransformFlatConfigBasicStr = `
|
||||
aws_instance.bar
|
||||
aws_instance.foo
|
||||
module.child.aws_instance.baz
|
||||
`
|
Loading…
Reference in New Issue
Block a user