terraform: destroy resource should depend on destroy-time prov deps

This commit is contained in:
Mitchell Hashimoto 2017-02-17 13:13:44 -08:00
parent 7352be49de
commit 757217b91f
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 91 additions and 0 deletions

View File

@ -4639,6 +4639,68 @@ aws_instance.foo:
}
}
func TestContext2Apply_provisionerDestroyModule(t *testing.T) {
m := testModule(t, "apply-provisioner-destroy-module")
p := testProvider("aws")
pr := testProvisioner()
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
pr.ApplyFn = func(rs *InstanceState, c *ResourceConfig) error {
val, ok := c.Config["foo"]
if !ok || val != "value" {
t.Fatalf("bad value for foo: %v %#v", val, c)
}
return nil
}
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: []string{"root", "child"},
Resources: map[string]*ResourceState{
"aws_instance.foo": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "bar",
},
},
},
},
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
State: state,
Destroy: true,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
Provisioners: map[string]ResourceProvisionerFactory{
"shell": testProvisionerFuncFixed(pr),
},
})
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
checkStateString(t, state, `
module.child:
<no state>`)
// Verify apply was invoked
if !pr.ApplyCalled {
t.Fatalf("provisioner not invoked")
}
}
func TestContext2Apply_provisionerResourceRef(t *testing.T) {
m := testModule(t, "apply-provisioner-resource-ref")
p := testProvider("aws")

View File

@ -68,6 +68,21 @@ func (n *NodeDestroyResource) ReferenceableName() []string {
// GraphNodeReferencer, overriding NodeAbstractResource
func (n *NodeDestroyResource) References() []string {
// If we have a config, then we need to include destroy-time dependencies
if c := n.Config; c != nil {
var result []string
for _, p := range c.Provisioners {
// We include conn info and config for destroy time provisioners
// as dependencies that we have.
if p.When == config.ProvisionerWhenDestroy {
result = append(result, ReferencesFromConfig(p.ConnInfo)...)
result = append(result, ReferencesFromConfig(p.RawConfig)...)
}
}
return result
}
return nil
}

View File

@ -0,0 +1,10 @@
variable "key" {}
resource "aws_instance" "foo" {
foo = "bar"
provisioner "shell" {
foo = "${var.key}"
when = "destroy"
}
}

View File

@ -0,0 +1,4 @@
module "child" {
source = "./child"
key = "value"
}