From a0d9bc0f19c4bb5bd8d16ba4f5abac7ff556d19c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 1 May 2015 11:26:58 -0700 Subject: [PATCH] terraform: outputs connect properly --- terraform/graph_config_node_module.go | 12 ++++++++- .../transform-flatten/child/main.tf | 4 +++ .../test-fixtures/transform-flatten/main.tf | 4 +++ terraform/transform_config.go | 2 +- terraform/transform_flatten.go | 25 ++++++++++++++++--- terraform/transform_flatten_test.go | 4 +++ 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/terraform/graph_config_node_module.go b/terraform/graph_config_node_module.go index 76fe99a6b9..48c1b6a4fb 100644 --- a/terraform/graph_config_node_module.go +++ b/terraform/graph_config_node_module.go @@ -27,7 +27,7 @@ func (n *GraphNodeConfigModule) DependableName() []string { result := make([]string, 1, len(config.Outputs)+1) result[0] = n.Name() for _, o := range config.Outputs { - result = append(result, fmt.Sprintf("%s.%s", n.Name(), o.Name)) + result = append(result, fmt.Sprintf("%s.output.%s", n.Name(), o.Name)) } return result @@ -116,6 +116,16 @@ func (n *graphNodeModuleExpanded) ConfigType() GraphNodeConfigType { return GraphNodeConfigTypeModule } +// GraphNodeDependable +func (n *graphNodeModuleExpanded) DependableName() []string { + return n.Original.DependableName() +} + +// GraphNodeDependent +func (n *graphNodeModuleExpanded) DependentOn() []string { + return n.Original.DependentOn() +} + // GraphNodeDotter impl. func (n *graphNodeModuleExpanded) DotNode(name string, opts *GraphDotOpts) *dot.Node { return dot.NewNode(name, map[string]string{ diff --git a/terraform/test-fixtures/transform-flatten/child/main.tf b/terraform/test-fixtures/transform-flatten/child/main.tf index e0c8ce4e41..7371f826d2 100644 --- a/terraform/test-fixtures/transform-flatten/child/main.tf +++ b/terraform/test-fixtures/transform-flatten/child/main.tf @@ -3,3 +3,7 @@ variable "var" {} resource "aws_instance" "child" { value = "${var.var}" } + +output "output" { + value = "${aws_instance.child.value}" +} diff --git a/terraform/test-fixtures/transform-flatten/main.tf b/terraform/test-fixtures/transform-flatten/main.tf index 648d15786d..179e151a3c 100644 --- a/terraform/test-fixtures/transform-flatten/main.tf +++ b/terraform/test-fixtures/transform-flatten/main.tf @@ -6,3 +6,7 @@ module "child" { resource "aws_instance" "parent" { value = "foo" } + +resource "aws_instance" "parent-output" { + value = "${module.child.output}" +} diff --git a/terraform/transform_config.go b/terraform/transform_config.go index 8783242941..3c061eeb1e 100644 --- a/terraform/transform_config.go +++ b/terraform/transform_config.go @@ -105,7 +105,7 @@ func (t *ConfigTransformer) Transform(g *Graph) error { func varNameForVar(raw config.InterpolatedVariable) string { switch v := raw.(type) { case *config.ModuleVariable: - return fmt.Sprintf("module.%s.%s", v.Name, v.Field) + return fmt.Sprintf("module.%s.output.%s", v.Name, v.Field) case *config.ResourceVariable: return v.ResourceId() case *config.UserVariable: diff --git a/terraform/transform_flatten.go b/terraform/transform_flatten.go index 30f0edaf79..995524dff7 100644 --- a/terraform/transform_flatten.go +++ b/terraform/transform_flatten.go @@ -1,5 +1,9 @@ package terraform +import ( + "github.com/hashicorp/terraform/dag" +) + // GraphNodeFlattenable must be implemented by nodes that can be flattened // into the graph. type GraphNodeFlattenable interface { @@ -24,6 +28,17 @@ func (t *FlattenTransformer) Transform(g *Graph) error { continue } + // Get all the things that depend on this node. We'll re-connect + // dependents later. We have to copy these here since the UpEdges + // value will be deleted after the Remove below. + dependents := make([]dag.Vertex, 0, 5) + for _, v := range g.UpEdges(v).List() { + dependents = append(dependents, v) + } + + // Remove the old node + g.Remove(v) + // Flatten the subgraph into this one. Keep any existing // connections that existed. for _, sv := range subgraph.Vertices() { @@ -33,14 +48,18 @@ func (t *FlattenTransformer) Transform(g *Graph) error { g.Connect(se) } - // 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) } + + // Re-connect all the things that dependend on the graph + // we just flattened. This should connect them back into the + // correct nodes if their DependentOn() is setup correctly. + for _, v := range dependents { + g.ConnectDependent(v) + } } return nil diff --git a/terraform/transform_flatten_test.go b/terraform/transform_flatten_test.go index cc564d495c..eddad8897b 100644 --- a/terraform/transform_flatten_test.go +++ b/terraform/transform_flatten_test.go @@ -37,8 +37,12 @@ func TestFlattenTransformer(t *testing.T) { const testTransformFlattenStr = ` aws_instance.parent +aws_instance.parent-output + module.child.output.output module.child.aws_instance.child module.child.var.var +module.child.output.output + module.child.aws_instance.child module.child.var.var aws_instance.parent `