terraform: destroy resource should depend on destroy-time prov deps
This commit is contained in:
parent
7352be49de
commit
757217b91f
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
variable "key" {}
|
||||
|
||||
resource "aws_instance" "foo" {
|
||||
foo = "bar"
|
||||
|
||||
provisioner "shell" {
|
||||
foo = "${var.key}"
|
||||
when = "destroy"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
module "child" {
|
||||
source = "./child"
|
||||
key = "value"
|
||||
}
|
Loading…
Reference in New Issue