terraform: outputs connect properly

This commit is contained in:
Mitchell Hashimoto 2015-05-01 11:26:58 -07:00
parent 7e838dfae2
commit a0d9bc0f19
6 changed files with 46 additions and 5 deletions

View File

@ -27,7 +27,7 @@ func (n *GraphNodeConfigModule) DependableName() []string {
result := make([]string, 1, len(config.Outputs)+1) result := make([]string, 1, len(config.Outputs)+1)
result[0] = n.Name() result[0] = n.Name()
for _, o := range config.Outputs { 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 return result
@ -116,6 +116,16 @@ func (n *graphNodeModuleExpanded) ConfigType() GraphNodeConfigType {
return GraphNodeConfigTypeModule return GraphNodeConfigTypeModule
} }
// GraphNodeDependable
func (n *graphNodeModuleExpanded) DependableName() []string {
return n.Original.DependableName()
}
// GraphNodeDependent
func (n *graphNodeModuleExpanded) DependentOn() []string {
return n.Original.DependentOn()
}
// GraphNodeDotter impl. // GraphNodeDotter impl.
func (n *graphNodeModuleExpanded) DotNode(name string, opts *GraphDotOpts) *dot.Node { func (n *graphNodeModuleExpanded) DotNode(name string, opts *GraphDotOpts) *dot.Node {
return dot.NewNode(name, map[string]string{ return dot.NewNode(name, map[string]string{

View File

@ -3,3 +3,7 @@ variable "var" {}
resource "aws_instance" "child" { resource "aws_instance" "child" {
value = "${var.var}" value = "${var.var}"
} }
output "output" {
value = "${aws_instance.child.value}"
}

View File

@ -6,3 +6,7 @@ module "child" {
resource "aws_instance" "parent" { resource "aws_instance" "parent" {
value = "foo" value = "foo"
} }
resource "aws_instance" "parent-output" {
value = "${module.child.output}"
}

View File

@ -105,7 +105,7 @@ func (t *ConfigTransformer) Transform(g *Graph) error {
func varNameForVar(raw config.InterpolatedVariable) string { func varNameForVar(raw config.InterpolatedVariable) string {
switch v := raw.(type) { switch v := raw.(type) {
case *config.ModuleVariable: 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: case *config.ResourceVariable:
return v.ResourceId() return v.ResourceId()
case *config.UserVariable: case *config.UserVariable:

View File

@ -1,5 +1,9 @@
package terraform package terraform
import (
"github.com/hashicorp/terraform/dag"
)
// GraphNodeFlattenable must be implemented by nodes that can be flattened // GraphNodeFlattenable must be implemented by nodes that can be flattened
// into the graph. // into the graph.
type GraphNodeFlattenable interface { type GraphNodeFlattenable interface {
@ -24,6 +28,17 @@ func (t *FlattenTransformer) Transform(g *Graph) error {
continue 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 // Flatten the subgraph into this one. Keep any existing
// connections that existed. // connections that existed.
for _, sv := range subgraph.Vertices() { for _, sv := range subgraph.Vertices() {
@ -33,14 +48,18 @@ func (t *FlattenTransformer) Transform(g *Graph) error {
g.Connect(se) g.Connect(se)
} }
// Remove the old node
g.Remove(v)
// Connect the dependencies for all the new nodes that we added. // Connect the dependencies for all the new nodes that we added.
// This will properly connect variables to their sources, for example. // This will properly connect variables to their sources, for example.
for _, sv := range subgraph.Vertices() { for _, sv := range subgraph.Vertices() {
g.ConnectDependent(sv) 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 return nil

View File

@ -37,8 +37,12 @@ func TestFlattenTransformer(t *testing.T) {
const testTransformFlattenStr = ` const testTransformFlattenStr = `
aws_instance.parent aws_instance.parent
aws_instance.parent-output
module.child.output.output
module.child.aws_instance.child module.child.aws_instance.child
module.child.var.var module.child.var.var
module.child.output.output
module.child.aws_instance.child
module.child.var.var module.child.var.var
aws_instance.parent aws_instance.parent
` `