terraform: properly encode module dependencies in state

This commit is contained in:
Mitchell Hashimoto 2014-09-26 09:38:53 -07:00
parent 1fa3840a00
commit 4782e31e9d
2 changed files with 35 additions and 22 deletions

View File

@ -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))
}
}

View File

@ -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{