terraform: transformers operate on root structures

This commit is contained in:
Mitchell Hashimoto 2015-01-30 13:31:39 -08:00
parent 96a2d3e116
commit 02bedd6850
4 changed files with 37 additions and 21 deletions

View File

@ -10,9 +10,9 @@ import (
) )
// ConfigTransformer is a GraphTransformer that adds the configuration // ConfigTransformer is a GraphTransformer that adds the configuration
// to the graph. It is assumed that the module tree given in Module matches // to the graph. The module used to configure this transformer must be
// the Path attribute of the Graph being transformed. If this is not the case, // the root module. We'll look up the child module by the Path in the
// the behavior is unspecified, but unlikely to be what you want. // Graph.
type ConfigTransformer struct { type ConfigTransformer struct {
Module *module.Tree Module *module.Tree
} }
@ -26,8 +26,15 @@ func (t *ConfigTransformer) Transform(g *Graph) error {
return errors.New("module must be loaded") 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 // Get the configuration for this module
config := t.Module.Config() config := module.Config()
// Create the node list we'll use for the graph // Create the node list we'll use for the graph
nodes := make([]graphNodeConfig, 0, nodes := make([]graphNodeConfig, 0,
@ -44,7 +51,7 @@ func (t *ConfigTransformer) Transform(g *Graph) error {
} }
// Write all the modules out // Write all the modules out
children := t.Module.Children() children := module.Children()
for _, m := range config.Modules { for _, m := range config.Modules {
nodes = append(nodes, &GraphNodeConfigModule{ nodes = append(nodes, &GraphNodeConfigModule{
Module: m, Module: m,

View File

@ -9,7 +9,7 @@ import (
) )
func TestConfigTransformer_nilModule(t *testing.T) { func TestConfigTransformer_nilModule(t *testing.T) {
var g Graph g := Graph{Path: RootModulePath}
tf := &ConfigTransformer{} tf := &ConfigTransformer{}
if err := tf.Transform(&g); err == nil { if err := tf.Transform(&g); err == nil {
t.Fatal("should error") t.Fatal("should error")
@ -23,7 +23,7 @@ func TestConfigTransformer_unloadedModule(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
var g Graph g := Graph{Path: RootModulePath}
tf := &ConfigTransformer{Module: mod} tf := &ConfigTransformer{Module: mod}
if err := tf.Transform(&g); err == nil { if err := tf.Transform(&g); err == nil {
t.Fatal("should error") t.Fatal("should error")
@ -31,7 +31,7 @@ func TestConfigTransformer_unloadedModule(t *testing.T) {
} }
func TestConfigTransformer(t *testing.T) { func TestConfigTransformer(t *testing.T) {
var g Graph g := Graph{Path: RootModulePath}
tf := &ConfigTransformer{Module: testModule(t, "graph-basic")} tf := &ConfigTransformer{Module: testModule(t, "graph-basic")}
if err := tf.Transform(&g); err != nil { if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -45,7 +45,7 @@ func TestConfigTransformer(t *testing.T) {
} }
func TestConfigTransformer_dependsOn(t *testing.T) { func TestConfigTransformer_dependsOn(t *testing.T) {
var g Graph g := Graph{Path: RootModulePath}
tf := &ConfigTransformer{Module: testModule(t, "graph-depends-on")} tf := &ConfigTransformer{Module: testModule(t, "graph-depends-on")}
if err := tf.Transform(&g); err != nil { if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -59,7 +59,7 @@ func TestConfigTransformer_dependsOn(t *testing.T) {
} }
func TestConfigTransformer_modules(t *testing.T) { func TestConfigTransformer_modules(t *testing.T) {
var g Graph g := Graph{Path: RootModulePath}
tf := &ConfigTransformer{Module: testModule(t, "graph-modules")} tf := &ConfigTransformer{Module: testModule(t, "graph-modules")}
if err := tf.Transform(&g); err != nil { if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -73,7 +73,7 @@ func TestConfigTransformer_modules(t *testing.T) {
} }
func TestConfigTransformer_errMissingDeps(t *testing.T) { func TestConfigTransformer_errMissingDeps(t *testing.T) {
var g Graph g := Graph{Path: RootModulePath}
tf := &ConfigTransformer{Module: testModule(t, "graph-missing-deps")} tf := &ConfigTransformer{Module: testModule(t, "graph-missing-deps")}
if err := tf.Transform(&g); err == nil { if err := tf.Transform(&g); err == nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)

View File

@ -3,7 +3,7 @@ package terraform
import ( import (
"fmt" "fmt"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/dag" "github.com/hashicorp/terraform/dag"
) )
@ -14,8 +14,9 @@ type OrphanTransformer struct {
// properly find module orphans at our path. // properly find module orphans at our path.
State *State State *State
// Config is just the configuration of our current module. // Module is the root module. We'll look up the proper configuration
Config *config.Config // using the graph path.
Module *module.Tree
} }
func (t *OrphanTransformer) Transform(g *Graph) error { func (t *OrphanTransformer) Transform(g *Graph) error {
@ -25,8 +26,16 @@ func (t *OrphanTransformer) Transform(g *Graph) error {
return nil 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. // 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)) resourceVertexes := make([]dag.Vertex, len(resourceOrphans))
for i, k := range resourceOrphans { for i, k := range resourceOrphans {
resourceVertexes[i] = g.Add(&graphNodeOrphanResource{ 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 // 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. // 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)) moduleVertexes := make([]dag.Vertex, len(moduleOrphans))
for i, path := range moduleOrphans { for i, path := range moduleOrphans {
moduleVertexes[i] = g.Add(&graphNodeOrphanModule{ moduleVertexes[i] = g.Add(&graphNodeOrphanModule{

View File

@ -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 { if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err) 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 { if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err) 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 { if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err) 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 { if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err) 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 { if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }