Merge pull request #12063 from hashicorp/b-provisioner-destroy-module

core: the great destroy provisioner 0.9 beta 1 bug fix
This commit is contained in:
Mitchell Hashimoto 2017-02-17 15:24:36 -08:00 committed by GitHub
commit 23a1dcdec2
8 changed files with 242 additions and 6 deletions

View File

@ -4639,6 +4639,193 @@ 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_provisionerDestroyRef(t *testing.T) {
m := testModule(t, "apply-provisioner-destroy-ref")
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 != "hello" {
return fmt.Errorf("bad value for foo: %v %#v", val, c)
}
return nil
}
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.bar": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "bar",
Attributes: map[string]string{
"key": "hello",
},
},
},
"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, `<no state>`)
// Verify apply was invoked
if !pr.ApplyCalled {
t.Fatalf("provisioner not invoked")
}
}
// Test that a destroy provisioner referencing an invalid key errors.
func TestContext2Apply_provisionerDestroyRefInvalid(t *testing.T) {
m := testModule(t, "apply-provisioner-destroy-ref")
p := testProvider("aws")
pr := testProvisioner()
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
pr.ApplyFn = func(rs *InstanceState, c *ResourceConfig) error {
return nil
}
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.bar": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "bar",
},
},
"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)
}
if _, err := ctx.Apply(); err == nil {
t.Fatal("expected error")
}
}
func TestContext2Apply_provisionerResourceRef(t *testing.T) { func TestContext2Apply_provisionerResourceRef(t *testing.T) {
m := testModule(t, "apply-provisioner-resource-ref") m := testModule(t, "apply-provisioner-resource-ref")
p := testProvider("aws") p := testProvider("aws")

View File

@ -498,7 +498,7 @@ MISSING:
// //
// For an input walk, computed values are okay to return because we're only // For an input walk, computed values are okay to return because we're only
// looking for missing variables to prompt the user for. // looking for missing variables to prompt the user for.
if i.Operation == walkRefresh || i.Operation == walkPlanDestroy || i.Operation == walkDestroy || i.Operation == walkInput { if i.Operation == walkRefresh || i.Operation == walkPlanDestroy || i.Operation == walkInput {
return &unknownVariable, nil return &unknownVariable, nil
} }

View File

@ -96,9 +96,11 @@ func (n *NodeAbstractResource) References() []string {
result = append(result, ReferencesFromConfig(c.RawCount)...) result = append(result, ReferencesFromConfig(c.RawCount)...)
result = append(result, ReferencesFromConfig(c.RawConfig)...) result = append(result, ReferencesFromConfig(c.RawConfig)...)
for _, p := range c.Provisioners { for _, p := range c.Provisioners {
if p.When == config.ProvisionerWhenCreate {
result = append(result, ReferencesFromConfig(p.ConnInfo)...) result = append(result, ReferencesFromConfig(p.ConnInfo)...)
result = append(result, ReferencesFromConfig(p.RawConfig)...) result = append(result, ReferencesFromConfig(p.RawConfig)...)
} }
}
return result return result
} }

View File

@ -68,6 +68,21 @@ func (n *NodeDestroyResource) ReferenceableName() []string {
// GraphNodeReferencer, overriding NodeAbstractResource // GraphNodeReferencer, overriding NodeAbstractResource
func (n *NodeDestroyResource) References() []string { 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 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"
}

View File

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

View File

@ -159,9 +159,15 @@ func (t *DestroyEdgeTransformer) Transform(g *Graph) error {
// This part is a little bit weird but is the best way to // This part is a little bit weird but is the best way to
// find the dependencies we need to: build a graph and use the // find the dependencies we need to: build a graph and use the
// attach config and state transformers then ask for references. // attach config and state transformers then ask for references.
node := &NodeAbstractResource{Addr: addr} abstract := &NodeAbstractResource{Addr: addr}
tempG.Add(node) tempG.Add(abstract)
tempDestroyed = append(tempDestroyed, node) tempDestroyed = append(tempDestroyed, abstract)
// We also add the destroy version here since the destroy can
// depend on things that the creation doesn't (destroy provisioners).
destroy := &NodeDestroyResource{NodeAbstractResource: abstract}
tempG.Add(destroy)
tempDestroyed = append(tempDestroyed, destroy)
} }
// Run the graph transforms so we have the information we need to // Run the graph transforms so we have the information we need to