mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
terraform: ConfigTransformer has Unique and mode filters
This commit is contained in:
parent
d0b7a4a072
commit
9c16489887
@ -0,0 +1,3 @@
|
|||||||
|
data "aws_ami" "foo" {}
|
||||||
|
|
||||||
|
resource "aws_instance" "web" {}
|
@ -4,7 +4,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/config"
|
||||||
"github.com/hashicorp/terraform/config/module"
|
"github.com/hashicorp/terraform/config/module"
|
||||||
"github.com/hashicorp/terraform/dag"
|
"github.com/hashicorp/terraform/dag"
|
||||||
)
|
)
|
||||||
@ -23,10 +25,25 @@ import (
|
|||||||
type ConfigTransformer struct {
|
type ConfigTransformer struct {
|
||||||
Concrete ConcreteResourceNodeFunc
|
Concrete ConcreteResourceNodeFunc
|
||||||
|
|
||||||
|
// Module is the module to add resources from.
|
||||||
Module *module.Tree
|
Module *module.Tree
|
||||||
|
|
||||||
|
// 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 config.ResourceMode
|
||||||
|
|
||||||
|
l sync.Mutex
|
||||||
|
uniqueMap map[string]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ConfigTransformer) Transform(g *Graph) error {
|
func (t *ConfigTransformer) Transform(g *Graph) error {
|
||||||
|
// Lock since we use some internal state
|
||||||
|
t.l.Lock()
|
||||||
|
defer t.l.Unlock()
|
||||||
|
|
||||||
// If no module is given, we don't do anything
|
// If no module is given, we don't do anything
|
||||||
if t.Module == nil {
|
if t.Module == nil {
|
||||||
return nil
|
return nil
|
||||||
@ -37,6 +54,18 @@ func (t *ConfigTransformer) Transform(g *Graph) error {
|
|||||||
return errors.New("module must be loaded for ConfigTransformer")
|
return errors.New("module must be loaded for ConfigTransformer")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
// Start the transformation process
|
||||||
return t.transform(g, t.Module)
|
return t.transform(g, t.Module)
|
||||||
}
|
}
|
||||||
@ -66,13 +95,13 @@ func (t *ConfigTransformer) transformSingle(g *Graph, m *module.Tree) error {
|
|||||||
log.Printf("[TRACE] ConfigTransformer: Starting for path: %v", m.Path())
|
log.Printf("[TRACE] ConfigTransformer: Starting for path: %v", m.Path())
|
||||||
|
|
||||||
// Get the configuration for this module
|
// Get the configuration for this module
|
||||||
config := m.Config()
|
conf := m.Config()
|
||||||
|
|
||||||
// Build the path we're at
|
// Build the path we're at
|
||||||
path := m.Path()
|
path := m.Path()
|
||||||
|
|
||||||
// Write all the resources out
|
// Write all the resources out
|
||||||
for _, r := range config.Resources {
|
for _, r := range conf.Resources {
|
||||||
// Build the resource address
|
// Build the resource address
|
||||||
addr, err := parseResourceAddressConfig(r)
|
addr, err := parseResourceAddressConfig(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -81,6 +110,16 @@ func (t *ConfigTransformer) transformSingle(g *Graph, m *module.Tree) error {
|
|||||||
}
|
}
|
||||||
addr.Path = path
|
addr.Path = path
|
||||||
|
|
||||||
|
// If this is already in our uniqueness map, don't add it again
|
||||||
|
if _, ok := t.uniqueMap[addr.String()]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove non-matching modes
|
||||||
|
if t.ModeFilter && addr.Mode != t.Mode {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Build the abstract node and the concrete one
|
// Build the abstract node and the concrete one
|
||||||
abstract := &NodeAbstractResource{Addr: addr}
|
abstract := &NodeAbstractResource{Addr: addr}
|
||||||
var node dag.Vertex = abstract
|
var node dag.Vertex = abstract
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/config"
|
||||||
"github.com/hashicorp/terraform/config/module"
|
"github.com/hashicorp/terraform/config/module"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -48,6 +49,80 @@ func TestConfigTransformer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigTransformer_mode(t *testing.T) {
|
||||||
|
g := Graph{Path: RootModulePath}
|
||||||
|
tf := &ConfigTransformer{
|
||||||
|
Module: testModule(t, "transform-config-mode-data"),
|
||||||
|
ModeFilter: true,
|
||||||
|
Mode: config.DataResourceMode,
|
||||||
|
}
|
||||||
|
if err := tf.Transform(&g); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := strings.TrimSpace(g.String())
|
||||||
|
expected := strings.TrimSpace(`
|
||||||
|
data.aws_ami.foo
|
||||||
|
`)
|
||||||
|
if actual != expected {
|
||||||
|
t.Fatalf("bad:\n\n%s", actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConfigTransformer_nonUnique(t *testing.T) {
|
||||||
|
addr, err := ParseResourceAddress("aws_instance.web")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("bad: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
g := Graph{Path: RootModulePath}
|
||||||
|
g.Add(&NodeAbstractResource{Addr: addr})
|
||||||
|
tf := &ConfigTransformer{Module: testModule(t, "graph-basic")}
|
||||||
|
if err := tf.Transform(&g); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := strings.TrimSpace(g.String())
|
||||||
|
expected := strings.TrimSpace(`
|
||||||
|
aws_instance.web
|
||||||
|
aws_instance.web
|
||||||
|
aws_load_balancer.weblb
|
||||||
|
aws_security_group.firewall
|
||||||
|
openstack_floating_ip.random
|
||||||
|
`)
|
||||||
|
if actual != expected {
|
||||||
|
t.Fatalf("bad:\n\n%s", actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConfigTransformer_unique(t *testing.T) {
|
||||||
|
addr, err := ParseResourceAddress("aws_instance.web")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("bad: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
g := Graph{Path: RootModulePath}
|
||||||
|
g.Add(&NodeAbstractResource{Addr: addr})
|
||||||
|
tf := &ConfigTransformer{
|
||||||
|
Module: testModule(t, "graph-basic"),
|
||||||
|
Unique: true,
|
||||||
|
}
|
||||||
|
if err := tf.Transform(&g); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := strings.TrimSpace(g.String())
|
||||||
|
expected := strings.TrimSpace(`
|
||||||
|
aws_instance.web
|
||||||
|
aws_load_balancer.weblb
|
||||||
|
aws_security_group.firewall
|
||||||
|
openstack_floating_ip.random
|
||||||
|
`)
|
||||||
|
if actual != expected {
|
||||||
|
t.Fatalf("bad:\n\n%s", actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const testConfigTransformerGraphBasicStr = `
|
const testConfigTransformerGraphBasicStr = `
|
||||||
aws_instance.web
|
aws_instance.web
|
||||||
aws_load_balancer.weblb
|
aws_load_balancer.weblb
|
||||||
|
Loading…
Reference in New Issue
Block a user