mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-25 08:21:07 -06:00
terraform: add TransitiveReductionTransformer
This commit is contained in:
parent
ed2075e384
commit
903e49162d
10
terraform/test-fixtures/transform-trans-reduce-basic/main.tf
Normal file
10
terraform/test-fixtures/transform-trans-reduce-basic/main.tf
Normal file
@ -0,0 +1,10 @@
|
||||
resource "aws_instance" "A" {}
|
||||
|
||||
resource "aws_instance" "B" {
|
||||
A = "${aws_instance.A.id}"
|
||||
}
|
||||
|
||||
resource "aws_instance" "C" {
|
||||
A = "${aws_instance.A.id}"
|
||||
B = "${aws_instance.B.id}"
|
||||
}
|
20
terraform/transform_transitive_reduction.go
Normal file
20
terraform/transform_transitive_reduction.go
Normal file
@ -0,0 +1,20 @@
|
||||
package terraform
|
||||
|
||||
// TransitiveReductionTransformer is a GraphTransformer that performs
|
||||
// finds the transitive reduction of the graph. For a definition of
|
||||
// transitive reduction, see Wikipedia.
|
||||
type TransitiveReductionTransformer struct{}
|
||||
|
||||
func (t *TransitiveReductionTransformer) Transform(g *Graph) error {
|
||||
// If the graph isn't valid, skip the transitive reduction.
|
||||
// We don't error here because Terraform itself handles graph
|
||||
// validation in a better way, or we assume it does.
|
||||
if err := g.Validate(); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Do it
|
||||
g.TransitiveReduction()
|
||||
|
||||
return nil
|
||||
}
|
39
terraform/transform_transitive_reduction_test.go
Normal file
39
terraform/transform_transitive_reduction_test.go
Normal file
@ -0,0 +1,39 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTransitiveReductionTransformer(t *testing.T) {
|
||||
mod := testModule(t, "transform-trans-reduce-basic")
|
||||
|
||||
g := Graph{Path: RootModulePath}
|
||||
{
|
||||
tf := &ConfigTransformer{Module: mod}
|
||||
if err := tf.Transform(&g); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
transform := &TransitiveReductionTransformer{}
|
||||
if err := transform.Transform(&g); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(g.String())
|
||||
expected := strings.TrimSpace(testTransformTransReduceBasicStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad:\n\n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
const testTransformTransReduceBasicStr = `
|
||||
aws_instance.A
|
||||
aws_instance.B
|
||||
aws_instance.A
|
||||
aws_instance.C
|
||||
aws_instance.B
|
||||
`
|
Loading…
Reference in New Issue
Block a user