From 4f8152c28abaeb61493490c1a3d61faaa8dadfea Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 26 Jan 2015 21:32:32 -0800 Subject: [PATCH] terraform: the orphan transform uses the graph path --- terraform/graph.go | 3 + terraform/transform_orphan.go | 16 ++++- terraform/transform_orphan_test.go | 94 +++++++++++++++++------------- 3 files changed, 69 insertions(+), 44 deletions(-) diff --git a/terraform/graph.go b/terraform/graph.go index 7b4a793c4d..962e1042b6 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -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. diff --git a/terraform/transform_orphan.go b/terraform/transform_orphan.go index 296dab61bf..66e4fa8acf 100644 --- a/terraform/transform_orphan.go +++ b/terraform/transform_orphan.go @@ -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 diff --git a/terraform/transform_orphan_test.go b/terraform/transform_orphan_test.go index a8f894e05b..299455e0fd 100644 --- a/terraform/transform_orphan_test.go +++ b/terraform/transform_orphan_test.go @@ -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 ` -*/