diff --git a/terraform/graph.go b/terraform/graph.go index 99d6561f6..7342c88fb 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -299,6 +299,9 @@ func graphEncodeDependencies(g *depgraph.Graph) { var inject []string for _, dep := range n.Deps { switch target := dep.Target.Meta.(type) { + case *GraphNodeModule: + inject = append(inject, dep.Target.Name) + case *GraphNodeResource: if target.Resource.Id == r.Id { continue @@ -311,6 +314,12 @@ func graphEncodeDependencies(g *depgraph.Graph) { id := fmt.Sprintf("%s.%d", target.ID, i) inject = append(inject, id) } + + case *GraphNodeResourceProvider: + // Do nothing + + default: + panic(fmt.Sprintf("Unknown graph node: %#v", dep.Target)) } } diff --git a/terraform/graph_test.go b/terraform/graph_test.go index fd2e0894b..bfb1167f6 100644 --- a/terraform/graph_test.go +++ b/terraform/graph_test.go @@ -2,6 +2,7 @@ package terraform import ( "reflect" + "sort" "strings" "testing" ) @@ -688,29 +689,8 @@ func TestGraphAddDiff_moduleDestroy(t *testing.T) { func TestGraphEncodeDependencies(t *testing.T) { m := testModule(t, "graph-basic") - state := &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.web": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "foo", - }, - }, - "aws_load_balancer.weblb": &ResourceState{ - Type: "aws_load_balancer", - Primary: &InstanceState{ - ID: "foo", - }, - }, - }, - }, - }, - } - g, err := Graph(&GraphOpts{Module: m, State: state}) + g, err := Graph(&GraphOpts{Module: m}) if err != nil { t.Fatalf("err: %s", err) } @@ -772,6 +752,30 @@ func TestGraphEncodeDependencies_count(t *testing.T) { } } +func TestGraphEncodeDependencies_module(t *testing.T) { + m := testModule(t, "graph-modules") + + g, err := Graph(&GraphOpts{Module: m}) + if err != nil { + t.Fatalf("err: %s", err) + } + + // This should encode the dependency information into the state + graphEncodeDependencies(g) + + web := g.Noun("aws_instance.web").Meta.(*GraphNodeResource).Resource + sort.Strings(web.Dependencies) + if len(web.Dependencies) != 2 { + t.Fatalf("bad: %#v", web) + } + if web.Dependencies[0] != "aws_security_group.firewall" { + t.Fatalf("bad: %#v", web) + } + if web.Dependencies[1] != "module.consul" { + t.Fatalf("bad: %#v", web) + } +} + func TestGraph_orphan_dependencies(t *testing.T) { m := testModule(t, "graph-count") state := &State{