Merge pull request #7131 from hashicorp/b-destroy-module-provider-config
core: Fix destroy when module vars used in provider config
This commit is contained in:
commit
445cd84f62
|
@ -443,6 +443,45 @@ func TestContext2Apply_destroySkipsCBD(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContext2Apply_destroyModuleVarProviderConfig(t *testing.T) {
|
||||
m := testModule(t, "apply-destroy-mod-var-provider-config")
|
||||
p := testProvider("aws")
|
||||
p.ApplyFn = testApplyFn
|
||||
p.DiffFn = testDiffFn
|
||||
state := &State{
|
||||
Modules: []*ModuleState{
|
||||
&ModuleState{
|
||||
Path: []string{"root", "child"},
|
||||
Resources: map[string]*ResourceState{
|
||||
"aws_instance.foo": &ResourceState{
|
||||
Type: "aws_instance",
|
||||
Primary: &InstanceState{
|
||||
ID: "foo",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Module: m,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
State: state,
|
||||
Destroy: true,
|
||||
})
|
||||
|
||||
if _, err := ctx.Plan(); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
_, err := ctx.Apply()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// https://github.com/hashicorp/terraform/issues/2892
|
||||
func TestContext2Apply_destroyCrossProviders(t *testing.T) {
|
||||
m := testModule(t, "apply-destroy-cross-providers")
|
||||
|
|
|
@ -234,3 +234,24 @@ func (n *GraphNodeConfigVariableFlat) Path() []string {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *GraphNodeConfigVariableFlat) Noop(opts *NoopOpts) bool {
|
||||
// First look for provider nodes that depend on this variable downstream
|
||||
modDiff := opts.Diff.ModuleByPath(n.ModulePath)
|
||||
if modDiff != nil && modDiff.Destroy {
|
||||
ds, err := opts.Graph.Descendents(n)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Error looking up descendents of %s: %s", n.Name(), err)
|
||||
} else {
|
||||
for _, d := range ds.List() {
|
||||
if _, ok := d.(GraphNodeProvider); ok {
|
||||
log.Printf("[DEBUG] This variable is depended on by a provider, can't be a noop.")
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Then fall back to existing impl
|
||||
return n.GraphNodeConfigVariable.Noop(opts)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
variable "input" {}
|
||||
|
||||
provider "aws" {
|
||||
region = "us-east-${var.input}"
|
||||
}
|
||||
|
||||
resource "aws_instance" "foo" { }
|
|
@ -0,0 +1,4 @@
|
|||
module "child" {
|
||||
source = "./child"
|
||||
input = "1"
|
||||
}
|
Loading…
Reference in New Issue