diff --git a/terraform/graph.go b/terraform/graph.go index 962e1042b..d9414db63 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -56,7 +56,6 @@ func (g *Graph) Add(v dag.Vertex) dag.Vertex { func (g *Graph) ConnectTo(source dag.Vertex, target []string) []string { g.once.Do(g.init) - // TODO: test var missing []string for _, t := range target { if dest := g.dependableMap[t]; dest != nil { @@ -78,3 +77,12 @@ func (g *Graph) init() { g.dependableMap = make(map[string]dag.Vertex) } } + +// GraphNodeDependable is an interface which says that a node can be +// depended on (an edge can be placed between this node and another) according +// to the well-known name returned by DependableName. +// +// DependableName can return multiple names it is known by. +type GraphNodeDependable interface { + DependableName() []string +} diff --git a/terraform/graph_deps.go b/terraform/graph_deps.go deleted file mode 100644 index 6dc1ac005..000000000 --- a/terraform/graph_deps.go +++ /dev/null @@ -1,43 +0,0 @@ -package terraform - -import ( - "github.com/hashicorp/terraform/dag" -) - -// GraphNodeDependable is an interface which says that a node can be -// depended on (an edge can be placed between this node and another) according -// to the well-known name returned by DependableName. -// -// DependableName can return multiple names it is known by. -type GraphNodeDependable interface { - DependableName() []string -} - -// GraphConnectDeps is a helper to connect a Vertex to the proper dependencies -// in the graph based only on the names expected by DependableName. -// -// This function will return the number of dependencies found and connected. -func GraphConnectDeps(g *dag.Graph, source dag.Vertex, targets []string) int { - count := 0 - - // This is reasonably horrible. In the future, we should optimize this - // through some kind of metadata on the graph that can store all of - // this information in a look-aside table. - for _, v := range g.Vertices() { - if dv, ok := v.(GraphNodeDependable); ok { - for _, n := range dv.DependableName() { - for _, n2 := range targets { - if n == n2 { - count++ - g.Connect(dag.BasicEdge(source, v)) - goto NEXT - } - } - } - } - - NEXT: - } - - return count -} diff --git a/terraform/graph_deps_test.go b/terraform/graph_deps_test.go deleted file mode 100644 index 984090b59..000000000 --- a/terraform/graph_deps_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package terraform - -import ( - "strings" - "testing" - - "github.com/hashicorp/terraform/dag" -) - -func TestGraphConnectDeps(t *testing.T) { - var g dag.Graph - g.Add(&testGraphDependable{VertexName: "a", Mock: []string{"a"}}) - b := g.Add(&testGraphDependable{VertexName: "b"}) - - if n := GraphConnectDeps(&g, b, []string{"a"}); n != 1 { - t.Fatalf("bad: %d", n) - } - - actual := strings.TrimSpace(g.String()) - expected := strings.TrimSpace(testGraphConnectDepsStr) - if actual != expected { - t.Fatalf("bad: %s", actual) - } -} - -type testGraphDependable struct { - VertexName string - Mock []string -} - -func (v *testGraphDependable) Name() string { - return v.VertexName -} - -func (v *testGraphDependable) DependableName() []string { - return v.Mock -} - -const testGraphConnectDepsStr = ` -a -b - a -` diff --git a/terraform/graph_test.go b/terraform/graph_test.go index 0c96d2214..7a70d231c 100644 --- a/terraform/graph_test.go +++ b/terraform/graph_test.go @@ -18,7 +18,42 @@ func TestGraphAdd(t *testing.T) { } } +func TestGraphConnectTo(t *testing.T) { + var g Graph + g.Add(&testGraphDependable{VertexName: "a", Mock: []string{"a"}}) + b := g.Add(&testGraphDependable{VertexName: "b"}) + + if missing := g.ConnectTo(b, []string{"a"}); len(missing) > 0 { + t.Fatalf("bad: %#v", missing) + } + + actual := strings.TrimSpace(g.String()) + expected := strings.TrimSpace(testGraphConnectDepsStr) + if actual != expected { + t.Fatalf("bad: %s", actual) + } +} + +type testGraphDependable struct { + VertexName string + Mock []string +} + +func (v *testGraphDependable) Name() string { + return v.VertexName +} + +func (v *testGraphDependable) DependableName() []string { + return v.Mock +} + const testGraphAddStr = ` 42 84 ` + +const testGraphConnectDepsStr = ` +a +b + a +`