terraform: destroy module nodes show up in plan destroy

This commit is contained in:
Mitchell Hashimoto 2016-10-19 10:02:07 -07:00
parent e68327e765
commit 480a414c40
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
6 changed files with 126 additions and 57 deletions

View File

@ -1582,7 +1582,7 @@ func TestContext2Plan_moduleDestroy(t *testing.T) {
actual := strings.TrimSpace(plan.String()) actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanModuleDestroyStr) expected := strings.TrimSpace(testTerraformPlanModuleDestroyStr)
if actual != expected { if actual != expected {
t.Fatalf("bad:\n%s", actual) t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected)
} }
} }
@ -1634,7 +1634,7 @@ func TestContext2Plan_moduleDestroyCycle(t *testing.T) {
actual := strings.TrimSpace(plan.String()) actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanModuleDestroyCycleStr) expected := strings.TrimSpace(testTerraformPlanModuleDestroyCycleStr)
if actual != expected { if actual != expected {
t.Fatalf("bad:\n%s", actual) t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected)
} }
} }
@ -1684,7 +1684,7 @@ func TestContext2Plan_moduleDestroyMultivar(t *testing.T) {
actual := strings.TrimSpace(plan.String()) actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanModuleDestroyMultivarStr) expected := strings.TrimSpace(testTerraformPlanModuleDestroyMultivarStr)
if actual != expected { if actual != expected {
t.Fatalf("bad:\n%s", actual) t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected)
} }
} }

View File

@ -44,6 +44,9 @@ func (b *DestroyPlanGraphBuilder) Steps() []GraphTransformer {
// Attach the configuration to any resources // Attach the configuration to any resources
&AttachResourceConfigTransformer{Module: b.Module}, &AttachResourceConfigTransformer{Module: b.Module},
// Module destroy nodes
&ModuleDestroyTransformer{State: b.State},
// Single root // Single root
&RootTransformer{}, &RootTransformer{},
} }

View File

@ -59,7 +59,7 @@ func (n *GraphNodeConfigModule) Expand(b GraphBuilder) (GraphNodeSubgraph, error
{ {
// Add the destroy marker to the graph // Add the destroy marker to the graph
t := &ModuleDestroyTransformer{} t := &ModuleDestroyTransformerOld{}
if err := t.Transform(graph); err != nil { if err := t.Transform(graph); err != nil {
return nil, err return nil, err
} }

View File

@ -0,0 +1,29 @@
package terraform
import (
"fmt"
)
// NodeDestroyableModule represents a module destruction.
type NodeDestroyableModuleVariable struct {
PathValue []string
}
func (n *NodeDestroyableModuleVariable) Name() string {
result := "plan-destroy"
if len(n.PathValue) > 1 {
result = fmt.Sprintf("%s.%s", modulePrefixStr(n.PathValue), result)
}
return result
}
// GraphNodeSubPath
func (n *NodeDestroyableModuleVariable) Path() []string {
return n.PathValue
}
// GraphNodeEvalable
func (n *NodeDestroyableModuleVariable) EvalTree() EvalNode {
return &EvalDiffDestroyModule{Path: n.PathValue}
}

View File

@ -1,62 +1,37 @@
package terraform package terraform
import (
"fmt"
"github.com/hashicorp/terraform/dag"
)
// ModuleDestroyTransformer is a GraphTransformer that adds a node // ModuleDestroyTransformer is a GraphTransformer that adds a node
// to the graph that will just mark the full module for destroy in // to the graph that will add a module destroy node for all modules in
// the destroy scenario. // the state.
type ModuleDestroyTransformer struct{} //
// NOTE: This is _completely unnecessary_ in the new graph worlds. This is
// only done to make old tests pass. However, this node does nothing in
// the new apply graph.
type ModuleDestroyTransformer struct {
State *State
}
func (t *ModuleDestroyTransformer) Transform(g *Graph) error { func (t *ModuleDestroyTransformer) Transform(g *Graph) error {
// Create the node // If empty do nothing
n := &graphNodeModuleDestroy{Path: g.Path} if t.State.Empty() {
return nil
}
// Add it to the graph. We don't need any edges because for _, ms := range t.State.Modules {
// it can happen whenever. // Just a silly edge case that is required to get old tests to pass.
g.Add(n) // It is probably a bug with the old graph but we mimic it here
// so that old tests pass.
if len(ms.Path) <= 1 {
continue
}
// Create the node
n := &NodeDestroyableModuleVariable{PathValue: ms.Path}
// Add it to the graph. We don't need any edges because
// it can happen whenever.
g.Add(n)
}
return nil return nil
} }
type graphNodeModuleDestroy struct {
Path []string
}
func (n *graphNodeModuleDestroy) Name() string {
return "plan-destroy"
}
// GraphNodeEvalable impl.
func (n *graphNodeModuleDestroy) EvalTree() EvalNode {
return &EvalOpFilter{
Ops: []walkOperation{walkPlanDestroy},
Node: &EvalDiffDestroyModule{Path: n.Path},
}
}
// GraphNodeFlattenable impl.
func (n *graphNodeModuleDestroy) Flatten(p []string) (dag.Vertex, error) {
return &graphNodeModuleDestroyFlat{
graphNodeModuleDestroy: n,
PathValue: p,
}, nil
}
type graphNodeModuleDestroyFlat struct {
*graphNodeModuleDestroy
PathValue []string
}
func (n *graphNodeModuleDestroyFlat) Name() string {
return fmt.Sprintf(
"%s.%s", modulePrefixStr(n.PathValue), n.graphNodeModuleDestroy.Name())
}
func (n *graphNodeModuleDestroyFlat) Path() []string {
return n.PathValue
}

View File

@ -0,0 +1,62 @@
package terraform
import (
"fmt"
"github.com/hashicorp/terraform/dag"
)
// ModuleDestroyTransformer is a GraphTransformer that adds a node
// to the graph that will just mark the full module for destroy in
// the destroy scenario.
type ModuleDestroyTransformerOld struct{}
func (t *ModuleDestroyTransformerOld) Transform(g *Graph) error {
// Create the node
n := &graphNodeModuleDestroy{Path: g.Path}
// Add it to the graph. We don't need any edges because
// it can happen whenever.
g.Add(n)
return nil
}
type graphNodeModuleDestroy struct {
Path []string
}
func (n *graphNodeModuleDestroy) Name() string {
return "plan-destroy"
}
// GraphNodeEvalable impl.
func (n *graphNodeModuleDestroy) EvalTree() EvalNode {
return &EvalOpFilter{
Ops: []walkOperation{walkPlanDestroy},
Node: &EvalDiffDestroyModule{Path: n.Path},
}
}
// GraphNodeFlattenable impl.
func (n *graphNodeModuleDestroy) Flatten(p []string) (dag.Vertex, error) {
return &graphNodeModuleDestroyFlat{
graphNodeModuleDestroy: n,
PathValue: p,
}, nil
}
type graphNodeModuleDestroyFlat struct {
*graphNodeModuleDestroy
PathValue []string
}
func (n *graphNodeModuleDestroyFlat) Name() string {
return fmt.Sprintf(
"%s.%s", modulePrefixStr(n.PathValue), n.graphNodeModuleDestroy.Name())
}
func (n *graphNodeModuleDestroyFlat) Path() []string {
return n.PathValue
}