core: Fix destroy on nested module vars for count

Building on b10564a, adding tweaks that allow the module var count
search to act recursively, ensuring that a sitaution where something
like var.top gets passed to module middle, as var.middle, and then to
module bottom, as var.bottom, which is then used in a resource count.
This commit is contained in:
Chris Marchesi 2016-05-17 07:56:24 -07:00 committed by James Nugent
parent f33ef43195
commit 2a679edd25
5 changed files with 140 additions and 8 deletions

View File

@ -2706,6 +2706,92 @@ module.child:
}
}
func TestContext2Apply_destroyWithModuleVariableAndCountNested(t *testing.T) {
m := testModule(t, "apply-destroy-mod-var-and-count-nested")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
var state *State
var err error
{
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
// First plan and apply a create operation
if _, err := ctx.Plan(); err != nil {
t.Fatalf("plan err: %s", err)
}
state, err = ctx.Apply()
if err != nil {
t.Fatalf("apply err: %s", err)
}
}
h := new(HookRecordApplyOrder)
h.Active = true
{
ctx := testContext2(t, &ContextOpts{
Destroy: true,
Module: m,
State: state,
Hooks: []Hook{h},
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
// First plan and apply a create operation
plan, err := ctx.Plan()
if err != nil {
t.Fatalf("destroy plan err: %s", err)
}
var buf bytes.Buffer
if err := WritePlan(plan, &buf); err != nil {
t.Fatalf("plan write err: %s", err)
}
planFromFile, err := ReadPlan(&buf)
if err != nil {
t.Fatalf("plan read err: %s", err)
}
ctx, err = planFromFile.Context(&ContextOpts{
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
if err != nil {
t.Fatalf("err: %s", err)
}
state, err = ctx.Apply()
if err != nil {
t.Fatalf("destroy apply err: %s", err)
}
}
//Test that things were destroyed
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(`
<no state>
module.child:
<no state>
module.child.child2:
<no state>
`)
if actual != expected {
t.Fatalf("expected: \n%s\n\nbad: \n%s", expected, actual)
}
}
func TestContext2Apply_destroyOutputs(t *testing.T) {
m := testModule(t, "apply-destroy-outputs")
h := new(HookRecordApplyOrder)

View File

@ -62,13 +62,16 @@ func (n *GraphNodeConfigVariable) VariableName() string {
func (n *GraphNodeConfigVariable) DestroyEdgeInclude(v dag.Vertex) bool {
// Only include this variable in a destroy edge if the source vertex
// "v" has a count dependency on this variable.
log.Printf("[DEBUG] DestroyEdgeInclude: Checking: %s", dag.VertexName(v))
cv, ok := v.(GraphNodeCountDependent)
if !ok {
log.Printf("[DEBUG] DestroyEdgeInclude: Not GraphNodeCountDependent: %s", dag.VertexName(v))
return false
}
for _, d := range cv.CountDependentOn() {
for _, d2 := range n.DependableName() {
log.Printf("[DEBUG] DestroyEdgeInclude: d = %s : d2 = %s", d, d2)
if d == d2 {
return true
}
@ -97,14 +100,10 @@ func (n *GraphNodeConfigVariable) Noop(opts *NoopOpts) bool {
// If we're destroying, we have no need of variables unless they are depended
// on by the count of a resource.
if modDiff != nil && modDiff.Destroy {
for _, v := range opts.Graph.UpEdges(opts.Vertex).List() {
// Here we borrow the implementation of DestroyEdgeInclude, whose logic
// and semantics are exactly what we want here.
if n.DestroyEdgeInclude(v) {
log.Printf("[DEBUG] Variable has destroy edge from %s, not a noop",
dag.VertexName(v))
return false
}
if n.hasDestroyEdgeInPath(opts, nil) {
log.Printf("[DEBUG] Variable has destroy edge from %s, not a noop",
dag.VertexName(opts.Vertex))
return false
}
log.Printf("[DEBUG] Variable has no included destroy edges: noop!")
return true
@ -124,6 +123,31 @@ func (n *GraphNodeConfigVariable) Noop(opts *NoopOpts) bool {
return true
}
// hasDestroyEdgeInPath recursively walks for a destroy edge, ensuring that
// a variable both has no immediate destroy edges or any in its full module
// path, ensuring that links do not get severed in the middle.
func (n *GraphNodeConfigVariable) hasDestroyEdgeInPath(opts *NoopOpts, vertex dag.Vertex) bool {
if vertex == nil {
vertex = opts.Vertex
}
log.Printf("[DEBUG] hasDestroyEdgeInPath: Looking for destroy edge: %s - %T", dag.VertexName(vertex), vertex)
for _, v := range opts.Graph.UpEdges(vertex).List() {
if len(opts.Graph.UpEdges(v).List()) > 1 {
if n.hasDestroyEdgeInPath(opts, v) == true {
return true
}
}
// Here we borrow the implementation of DestroyEdgeInclude, whose logic
// and semantics are exactly what we want here.
if cv, ok := vertex.(*GraphNodeConfigVariableFlat); ok {
if cv.DestroyEdgeInclude(v) {
return true
}
}
}
return false
}
// GraphNodeProxy impl.
func (n *GraphNodeConfigVariable) Proxy() bool {
return true

View File

@ -0,0 +1,5 @@
variable "mod_count_child2" { }
resource "aws_instance" "foo" {
count = "${var.mod_count_child2}"
}

View File

@ -0,0 +1,8 @@
variable "mod_count_child" { }
module "child2" {
source = "./child2"
mod_count_child2 = "${var.mod_count_child}"
}
resource "aws_instance" "foo" { }

View File

@ -0,0 +1,9 @@
variable "mod_count_root" {
type = "string"
default = "3"
}
module "child" {
source = "./child"
mod_count_child = "${var.mod_count_root}"
}