From 02bedd6850daeded63ae0fb4770baefd5b9c6ef6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 30 Jan 2015 13:31:39 -0800 Subject: [PATCH] terraform: transformers operate on root structures --- terraform/transform_config.go | 17 ++++++++++++----- terraform/transform_config_test.go | 12 ++++++------ terraform/transform_orphan.go | 19 ++++++++++++++----- terraform/transform_orphan_test.go | 10 +++++----- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/terraform/transform_config.go b/terraform/transform_config.go index 0358f7b83..f92c97dca 100644 --- a/terraform/transform_config.go +++ b/terraform/transform_config.go @@ -10,9 +10,9 @@ import ( ) // ConfigTransformer is a GraphTransformer that adds the configuration -// to the graph. It is assumed that the module tree given in Module matches -// the Path attribute of the Graph being transformed. If this is not the case, -// the behavior is unspecified, but unlikely to be what you want. +// to the graph. The module used to configure this transformer must be +// the root module. We'll look up the child module by the Path in the +// Graph. type ConfigTransformer struct { Module *module.Tree } @@ -26,8 +26,15 @@ func (t *ConfigTransformer) Transform(g *Graph) error { return errors.New("module must be loaded") } + // Get the module we care about + module := t.Module.Child(g.Path[1:]) + if module == nil { + return fmt.Errorf( + "module not found for path: %#v", g.Path[1:]) + } + // Get the configuration for this module - config := t.Module.Config() + config := module.Config() // Create the node list we'll use for the graph nodes := make([]graphNodeConfig, 0, @@ -44,7 +51,7 @@ func (t *ConfigTransformer) Transform(g *Graph) error { } // Write all the modules out - children := t.Module.Children() + children := module.Children() for _, m := range config.Modules { nodes = append(nodes, &GraphNodeConfigModule{ Module: m, diff --git a/terraform/transform_config_test.go b/terraform/transform_config_test.go index fb4bf1c8d..b361e91be 100644 --- a/terraform/transform_config_test.go +++ b/terraform/transform_config_test.go @@ -9,7 +9,7 @@ import ( ) func TestConfigTransformer_nilModule(t *testing.T) { - var g Graph + g := Graph{Path: RootModulePath} tf := &ConfigTransformer{} if err := tf.Transform(&g); err == nil { t.Fatal("should error") @@ -23,7 +23,7 @@ func TestConfigTransformer_unloadedModule(t *testing.T) { t.Fatalf("err: %s", err) } - var g Graph + g := Graph{Path: RootModulePath} tf := &ConfigTransformer{Module: mod} if err := tf.Transform(&g); err == nil { t.Fatal("should error") @@ -31,7 +31,7 @@ func TestConfigTransformer_unloadedModule(t *testing.T) { } func TestConfigTransformer(t *testing.T) { - var g Graph + g := Graph{Path: RootModulePath} tf := &ConfigTransformer{Module: testModule(t, "graph-basic")} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -45,7 +45,7 @@ func TestConfigTransformer(t *testing.T) { } func TestConfigTransformer_dependsOn(t *testing.T) { - var g Graph + g := Graph{Path: RootModulePath} tf := &ConfigTransformer{Module: testModule(t, "graph-depends-on")} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -59,7 +59,7 @@ func TestConfigTransformer_dependsOn(t *testing.T) { } func TestConfigTransformer_modules(t *testing.T) { - var g Graph + g := Graph{Path: RootModulePath} tf := &ConfigTransformer{Module: testModule(t, "graph-modules")} if err := tf.Transform(&g); err != nil { t.Fatalf("err: %s", err) @@ -73,7 +73,7 @@ func TestConfigTransformer_modules(t *testing.T) { } func TestConfigTransformer_errMissingDeps(t *testing.T) { - var g Graph + g := Graph{Path: RootModulePath} tf := &ConfigTransformer{Module: testModule(t, "graph-missing-deps")} if err := tf.Transform(&g); err == nil { t.Fatalf("err: %s", err) diff --git a/terraform/transform_orphan.go b/terraform/transform_orphan.go index 970063d29..205f00204 100644 --- a/terraform/transform_orphan.go +++ b/terraform/transform_orphan.go @@ -3,7 +3,7 @@ package terraform import ( "fmt" - "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/dag" ) @@ -14,8 +14,9 @@ type OrphanTransformer struct { // properly find module orphans at our path. State *State - // Config is just the configuration of our current module. - Config *config.Config + // Module is the root module. We'll look up the proper configuration + // using the graph path. + Module *module.Tree } func (t *OrphanTransformer) Transform(g *Graph) error { @@ -25,8 +26,16 @@ func (t *OrphanTransformer) Transform(g *Graph) error { return nil } + module := t.Module.Child(g.Path[1:]) + if module == nil { + panic(fmt.Sprintf( + "module not found for path: %#v", + g.Path[1:])) + } + config := module.Config() + // Go over each resource orphan and add it to the graph. - resourceOrphans := state.Orphans(t.Config) + resourceOrphans := state.Orphans(config) resourceVertexes := make([]dag.Vertex, len(resourceOrphans)) for i, k := range resourceOrphans { resourceVertexes[i] = g.Add(&graphNodeOrphanResource{ @@ -37,7 +46,7 @@ func (t *OrphanTransformer) Transform(g *Graph) error { // Go over each module orphan and add it to the graph. We store the // vertexes and states outside so that we can connect dependencies later. - moduleOrphans := t.State.ModuleOrphans(g.Path, t.Config) + moduleOrphans := t.State.ModuleOrphans(g.Path, config) moduleVertexes := make([]dag.Vertex, len(moduleOrphans)) for i, path := range moduleOrphans { moduleVertexes[i] = g.Add(&graphNodeOrphanModule{ diff --git a/terraform/transform_orphan_test.go b/terraform/transform_orphan_test.go index 76809a4d7..9ca451b3b 100644 --- a/terraform/transform_orphan_test.go +++ b/terraform/transform_orphan_test.go @@ -41,7 +41,7 @@ func TestOrphanTransformer(t *testing.T) { } } - transform := &OrphanTransformer{State: state, Config: mod.Config()} + transform := &OrphanTransformer{State: state, Module: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -92,7 +92,7 @@ func TestOrphanTransformer_modules(t *testing.T) { } } - transform := &OrphanTransformer{State: state, Config: mod.Config()} + transform := &OrphanTransformer{State: state, Module: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -146,7 +146,7 @@ func TestOrphanTransformer_modulesDeps(t *testing.T) { } } - transform := &OrphanTransformer{State: state, Config: mod.Config()} + transform := &OrphanTransformer{State: state, Module: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -200,7 +200,7 @@ func TestOrphanTransformer_modulesDepsOrphan(t *testing.T) { } } - transform := &OrphanTransformer{State: state, Config: mod.Config()} + transform := &OrphanTransformer{State: state, Module: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) } @@ -249,7 +249,7 @@ func TestOrphanTransformer_resourceDepends(t *testing.T) { } } - transform := &OrphanTransformer{State: state, Config: mod.Config()} + transform := &OrphanTransformer{State: state, Module: mod} if err := transform.Transform(&g); err != nil { t.Fatalf("err: %s", err) }