Fix nested module "unknown variable" during dest (#7496)
* Fix nested module "unknown variable" during dstry During a destroy with nested modules, accessing a variable between them causes an "unknown variable accessed" during destroy.
This commit is contained in:
parent
86ff2ca7a9
commit
21e2173e0a
|
@ -4704,3 +4704,83 @@ aws_instance.foo:
|
|||
t.Fatalf("bad: \n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
// https://github.com/hashicorp/terraform/issues/7378
|
||||
func TestContext2Apply_destroyNestedModuleWithAttrsReferencingResource(t *testing.T) {
|
||||
m := testModule(t, "apply-destroy-nested-module-with-attrs")
|
||||
p := testProvider("null")
|
||||
p.ApplyFn = testApplyFn
|
||||
p.DiffFn = testDiffFn
|
||||
|
||||
var state *State
|
||||
var err error
|
||||
{
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Module: m,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"null": 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)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Destroy: true,
|
||||
Module: m,
|
||||
State: state,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"null": testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
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{
|
||||
"null": 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.middle:
|
||||
<no state>
|
||||
`)
|
||||
if actual != expected {
|
||||
t.Fatalf("expected: \n%s\n\nbad: \n%s", expected, actual)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -130,6 +130,7 @@ func (n *GraphNodeConfigVariable) hasDestroyEdgeInPath(opts *NoopOpts, vertex da
|
|||
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 {
|
||||
|
@ -137,10 +138,12 @@ func (n *GraphNodeConfigVariable) hasDestroyEdgeInPath(opts *NoopOpts, vertex da
|
|||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Here we borrow the implementation of DestroyEdgeInclude, whose logic
|
||||
// and semantics are exactly what we want here.
|
||||
// and semantics are exactly what we want here. We add a check for the
|
||||
// the root node, since we have to always depend on its existance.
|
||||
if cv, ok := vertex.(*GraphNodeConfigVariableFlat); ok {
|
||||
if cv.DestroyEdgeInclude(v) {
|
||||
if dag.VertexName(v) == rootNodeName || cv.DestroyEdgeInclude(v) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
variable "bottom_param" {}
|
|
@ -0,0 +1,8 @@
|
|||
variable "param" {}
|
||||
|
||||
resource "null_resource" "n" {}
|
||||
|
||||
module "bottom" {
|
||||
source = "./bottom"
|
||||
bottom_param = "${var.param}"
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
module "middle" {
|
||||
source = "./middle"
|
||||
param = "foo"
|
||||
}
|
Loading…
Reference in New Issue