terraform: module orphan dependencies

This commit is contained in:
Mitchell Hashimoto 2015-01-27 11:10:51 -08:00
parent 922bdc92db
commit 7025c48165
2 changed files with 74 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/dag"
) )
// OrphanTransformer is a GraphTransformer that adds orphans to the // OrphanTransformer is a GraphTransformer that adds orphans to the
@ -31,9 +32,19 @@ func (t *OrphanTransformer) Transform(g *Graph) error {
state.Resources[k].Dependencies) state.Resources[k].Dependencies)
} }
// Go over each module orphan and add it to the graph // Go over each module orphan and add it to the graph. We store the
for _, path := range t.State.ModuleOrphans(g.Path, t.Config) { // vertexes and states outside so that we can connect dependencies later.
g.Add(&graphNodeOrphanModule{Path: path}) moduleOrphans := t.State.ModuleOrphans(g.Path, t.Config)
moduleVertexes := make([]dag.Vertex, len(moduleOrphans))
moduleStates := make([]*ModuleState, len(moduleVertexes))
for i, path := range moduleOrphans {
moduleVertexes[i] = g.Add(&graphNodeOrphanModule{Path: path})
moduleStates[i] = t.State.ModuleByPath(path)
}
// Module dependencies
for i, v := range moduleVertexes {
g.ConnectTo(v, moduleStates[i].Dependencies)
} }
return nil return nil

View File

@ -102,6 +102,60 @@ func TestOrphanTransformer_modules(t *testing.T) {
} }
} }
func TestOrphanTransformer_modulesDeps(t *testing.T) {
mod := testModule(t, "transform-orphan-modules")
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.foo": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
},
},
},
// Orphan module
&ModuleState{
Path: []string{RootModuleName, "foo"},
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
},
},
Dependencies: []string{
"aws_instance.foo",
},
},
},
}
g := Graph{Path: RootModulePath}
{
tf := &ConfigTransformer{Module: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
transform := &OrphanTransformer{State: state, Config: mod.Config()}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformOrphanModulesDepsStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
func TestOrphanTransformer_resourceDepends(t *testing.T) { func TestOrphanTransformer_resourceDepends(t *testing.T) {
mod := testModule(t, "transform-orphan-basic") mod := testModule(t, "transform-orphan-basic")
state := &State{ state := &State{
@ -161,6 +215,12 @@ aws_instance.foo
module.foo (orphan) module.foo (orphan)
` `
const testTransformOrphanModulesDepsStr = `
aws_instance.foo
module.foo (orphan)
aws_instance.foo
`
const testTransformOrphanResourceDependsStr = ` const testTransformOrphanResourceDependsStr = `
aws_instance.db (orphan) aws_instance.db (orphan)
aws_instance.web aws_instance.web