terraform: handle module dependencies with a diff

This commit is contained in:
Mitchell Hashimoto 2014-09-26 09:20:01 -07:00
parent 77b1c7daa0
commit 1fa3840a00
4 changed files with 105 additions and 1 deletions

View File

@ -601,6 +601,23 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error {
num-- num--
i-- i--
case *GraphNodeModule:
// We invert any module dependencies so we're destroyed
// first, before any modules are applied.
newDep := &depgraph.Dependency{
Name: n.Name,
Source: dep.Target,
Target: n,
}
dep.Target.Deps = append(dep.Target.Deps, newDep)
// Drop the dependency. We may have created
// an inverse depedency if the dependent resource
// is also being deleted, but this dependence is
// no longer required.
deps[i], deps[num-1] = deps[num-1], nil
num--
i--
case *GraphNodeResourceProvider: case *GraphNodeResourceProvider:
// Keep these around, but fix up the source to be ourselves // Keep these around, but fix up the source to be ourselves
// rather than the old node. // rather than the old node.
@ -608,7 +625,7 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error {
newDep.Source = n newDep.Source = n
deps[i] = &newDep deps[i] = &newDep
default: default:
panic(fmt.Errorf("Unhandled depedency type: %#v", dep.Meta)) panic(fmt.Errorf("Unhandled depedency type: %#v", dep.Target.Meta))
} }
} }
n.Deps = deps[:num] n.Deps = deps[:num]

View File

@ -624,6 +624,68 @@ func TestGraphAddDiff_destroy_counts(t *testing.T) {
} }
} }
func TestGraphAddDiff_module(t *testing.T) {
m := testModule(t, "graph-diff-module")
diff := &Diff{
Modules: []*ModuleDiff{
&ModuleDiff{
Path: rootModulePath,
Resources: map[string]*InstanceDiff{
"aws_instance.foo": &InstanceDiff{
Destroy: true,
},
},
},
},
}
g, err := Graph(&GraphOpts{Module: m, Diff: diff})
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTerraformGraphDiffModuleStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
func TestGraphAddDiff_moduleDestroy(t *testing.T) {
m := testModule(t, "graph-diff-module")
diff := &Diff{
Modules: []*ModuleDiff{
&ModuleDiff{
Path: rootModulePath,
Resources: map[string]*InstanceDiff{
"aws_instance.foo": &InstanceDiff{
Destroy: true,
},
},
},
&ModuleDiff{
Path: []string{"root", "child"},
Resources: map[string]*InstanceDiff{
"aws_instance.foo": &InstanceDiff{
Destroy: true,
},
},
},
},
}
g, err := Graph(&GraphOpts{Module: m, Diff: diff})
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTerraformGraphDiffModuleStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
func TestGraphEncodeDependencies(t *testing.T) { func TestGraphEncodeDependencies(t *testing.T) {
m := testModule(t, "graph-basic") m := testModule(t, "graph-basic")
state := &State{ state := &State{
@ -873,6 +935,19 @@ root
root -> aws_load_balancer.weblb root -> aws_load_balancer.weblb
` `
const testTerraformGraphDiffModuleStr = `
root: root
aws_instance.foo
aws_instance.foo -> aws_instance.foo (destroy)
aws_instance.foo -> module.child
aws_instance.foo (destroy)
module.child
module.child -> aws_instance.foo (destroy)
root
root -> aws_instance.foo
root -> module.child
`
const testTerraformGraphModulesStr = ` const testTerraformGraphModulesStr = `
root: root root: root
aws_instance.web aws_instance.web

View File

@ -0,0 +1,5 @@
resource "aws_instance" "foo" {}
output "bar" {
value = "baz"
}

View File

@ -0,0 +1,7 @@
module "child" {
source = "./child"
}
resource "aws_instance" "foo" {
value = "${module.child.bar}"
}