terraform: the orphan transform uses the graph path

This commit is contained in:
Mitchell Hashimoto 2015-01-26 21:32:32 -08:00
parent 3820aea513
commit 4f8152c28a
3 changed files with 69 additions and 44 deletions

View File

@ -9,6 +9,9 @@ import (
// RootModuleName is the name given to the root module implicitly.
const RootModuleName = "root"
// RootModulePath is the path for the root module.
var RootModulePath = []string{RootModuleName}
// Graph represents the graph that Terraform uses to represent resources
// and their dependencies. Each graph represents only one module, but it
// can contain further modules, which themselves have their own graph.

View File

@ -9,13 +9,23 @@ import (
// OrphanTransformer is a GraphTransformer that adds orphans to the
// graph. This transformer adds both resource and module orphans.
type OrphanTransformer struct {
State *ModuleState
// State is the global state. We require the global state to
// properly find module orphans at our path.
State *State
// Config is just the configuration of our current module.
Config *config.Config
}
func (t *OrphanTransformer) Transform(g *Graph) error {
state := t.State.ModuleByPath(g.Path)
if state == nil {
// If there is no state for our module, there can't be any orphans
return nil
}
// Get the orphans from our configuration. This will only get resources.
orphans := t.State.Orphans(t.Config)
orphans := state.Orphans(t.Config)
if len(orphans) == 0 {
return nil
}
@ -24,7 +34,7 @@ func (t *OrphanTransformer) Transform(g *Graph) error {
for _, k := range orphans {
g.ConnectTo(
g.Add(&graphNodeOrphanResource{ResourceName: k}),
t.State.Resources[k].Dependencies)
state.Resources[k].Dependencies)
}
// TODO: modules

View File

@ -1,6 +1,5 @@
package terraform
/*
import (
"strings"
"testing"
@ -8,33 +7,40 @@ import (
func TestOrphanTransformer(t *testing.T) {
mod := testModule(t, "transform-orphan-basic")
state := &ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
},
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
},
// The orphan
"aws_instance.db": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
// The orphan
"aws_instance.db": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
},
},
},
},
}
g, err := Graph2(mod)
if err != nil {
t.Fatalf("err: %s", err)
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 {
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -47,36 +53,43 @@ func TestOrphanTransformer(t *testing.T) {
func TestOrphanTransformer_resourceDepends(t *testing.T) {
mod := testModule(t, "transform-orphan-basic")
state := &ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
},
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
},
// The orphan
"aws_instance.db": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
Dependencies: []string{
"aws_instance.web",
// The orphan
"aws_instance.db": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
Dependencies: []string{
"aws_instance.web",
},
},
},
},
},
}
g, err := Graph2(mod)
if err != nil {
t.Fatalf("err: %s", err)
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 {
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
@ -97,4 +110,3 @@ aws_instance.db (orphan)
aws_instance.web
aws_instance.web
`
*/