diff --git a/terraform/graph_config_node_variable.go b/terraform/graph_config_node_variable.go index d0b1f0e46..b6e539006 100644 --- a/terraform/graph_config_node_variable.go +++ b/terraform/graph_config_node_variable.go @@ -35,7 +35,21 @@ func (n *GraphNodeConfigVariable) DependableName() []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. diff --git a/terraform/test-fixtures/transform-flatten/child/main.tf b/terraform/test-fixtures/transform-flatten/child/main.tf new file mode 100644 index 000000000..e0c8ce4e4 --- /dev/null +++ b/terraform/test-fixtures/transform-flatten/child/main.tf @@ -0,0 +1,5 @@ +variable "var" {} + +resource "aws_instance" "child" { + value = "${var.var}" +} diff --git a/terraform/test-fixtures/transform-flatten/main.tf b/terraform/test-fixtures/transform-flatten/main.tf new file mode 100644 index 000000000..648d15786 --- /dev/null +++ b/terraform/test-fixtures/transform-flatten/main.tf @@ -0,0 +1,8 @@ +module "child" { + source = "./child" + var = "${aws_instance.parent.value}" +} + +resource "aws_instance" "parent" { + value = "foo" +} diff --git a/terraform/transform_flatten.go b/terraform/transform_flatten.go index ecdef22fe..30f0edaf7 100644 --- a/terraform/transform_flatten.go +++ b/terraform/transform_flatten.go @@ -6,12 +6,12 @@ type GraphNodeFlattenable interface { 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, // 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() { fn, ok := v.(GraphNodeFlattenable) if !ok { @@ -35,6 +35,12 @@ func (t *FlattenTransform) Transform(g *Graph) error { // Remove the old node 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 diff --git a/terraform/transform_flatten_test.go b/terraform/transform_flatten_test.go new file mode 100644 index 000000000..cc564d495 --- /dev/null +++ b/terraform/transform_flatten_test.go @@ -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 +`