terraform: test that variable deps reach out to parent graph
This commit is contained in:
parent
e542d6efdf
commit
dd14ce9a0b
|
@ -35,7 +35,21 @@ func (n *GraphNodeConfigVariable) DependableName() []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *GraphNodeConfigVariable) DependentOn() []string {
|
func (n *GraphNodeConfigVariable) DependentOn() []string {
|
||||||
return nil
|
// If we don't have any value set, we don't depend on anything
|
||||||
|
if n.Value == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get what we depend on based on our value
|
||||||
|
vars := n.Value.Variables
|
||||||
|
result := make([]string, 0, len(vars))
|
||||||
|
for _, v := range vars {
|
||||||
|
if vn := varNameForVar(v); vn != "" {
|
||||||
|
result = append(result, vn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// GraphNodeVariable impl.
|
// GraphNodeVariable impl.
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
variable "var" {}
|
||||||
|
|
||||||
|
resource "aws_instance" "child" {
|
||||||
|
value = "${var.var}"
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
module "child" {
|
||||||
|
source = "./child"
|
||||||
|
var = "${aws_instance.parent.value}"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_instance" "parent" {
|
||||||
|
value = "foo"
|
||||||
|
}
|
|
@ -6,12 +6,12 @@ type GraphNodeFlattenable interface {
|
||||||
FlattenGraph() *Graph
|
FlattenGraph() *Graph
|
||||||
}
|
}
|
||||||
|
|
||||||
// FlattenTransform is a transformer that goes through the graph, finds
|
// FlattenTransformer is a transformer that goes through the graph, finds
|
||||||
// subgraphs that can be flattened, and flattens them into this graph,
|
// subgraphs that can be flattened, and flattens them into this graph,
|
||||||
// removing the prior subgraph node.
|
// removing the prior subgraph node.
|
||||||
type FlattenTransform struct{}
|
type FlattenTransformer struct{}
|
||||||
|
|
||||||
func (t *FlattenTransform) Transform(g *Graph) error {
|
func (t *FlattenTransformer) Transform(g *Graph) error {
|
||||||
for _, v := range g.Vertices() {
|
for _, v := range g.Vertices() {
|
||||||
fn, ok := v.(GraphNodeFlattenable)
|
fn, ok := v.(GraphNodeFlattenable)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -35,6 +35,12 @@ func (t *FlattenTransform) Transform(g *Graph) error {
|
||||||
|
|
||||||
// Remove the old node
|
// Remove the old node
|
||||||
g.Remove(v)
|
g.Remove(v)
|
||||||
|
|
||||||
|
// Connect the dependencies for all the new nodes that we added.
|
||||||
|
// This will properly connect variables to their sources, for example.
|
||||||
|
for _, sv := range subgraph.Vertices() {
|
||||||
|
g.ConnectDependent(sv)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package terraform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFlattenTransformer(t *testing.T) {
|
||||||
|
mod := testModule(t, "transform-flatten")
|
||||||
|
|
||||||
|
var b BasicGraphBuilder
|
||||||
|
b = BasicGraphBuilder{
|
||||||
|
Steps: []GraphTransformer{
|
||||||
|
&ConfigTransformer{Module: mod},
|
||||||
|
&VertexTransformer{
|
||||||
|
Transforms: []GraphVertexTransformer{
|
||||||
|
&ExpandTransform{
|
||||||
|
Builder: &b,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&FlattenTransformer{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
g, err := b.Build(rootModulePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := strings.TrimSpace(g.String())
|
||||||
|
expected := strings.TrimSpace(testTransformFlattenStr)
|
||||||
|
if actual != expected {
|
||||||
|
t.Fatalf("bad:\n\n%s", actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const testTransformFlattenStr = `
|
||||||
|
aws_instance.parent
|
||||||
|
module.child.aws_instance.child
|
||||||
|
module.child.var.var
|
||||||
|
module.child.var.var
|
||||||
|
aws_instance.parent
|
||||||
|
`
|
Loading…
Reference in New Issue