fix destroy so incoming module vars get set

This commit is contained in:
Luke Amdor 2014-12-05 17:18:45 -06:00
parent 55acb9809a
commit 6dcb7166d1
5 changed files with 96 additions and 0 deletions

View File

@ -939,6 +939,22 @@ func (c *walkContext) planDestroyWalkFn() depgraph.WalkFunc {
// Build another walkContext for this module and walk it.
wc := c.Context.walkContext(c.Operation, m.Path)
// compute incoming vars
if m.Config != nil {
wc.Variables = make(map[string]string)
rc := NewResourceConfig(m.Config.RawConfig)
rc.interpolate(c, nil)
for k, v := range rc.Config {
wc.Variables[k] = v.(string)
}
for k, _ := range rc.Raw {
if _, ok := wc.Variables[k]; !ok {
wc.Variables[k] = config.UnknownVariableValue
}
}
}
// Set the graph to specifically walk this subgraph
wc.graph = m.Graph

View File

@ -3716,6 +3716,55 @@ func TestContextPlan_moduleDestroy(t *testing.T) {
}
}
func TestContextPlan_moduleDestroyMultivar(t *testing.T) {
m := testModule(t, "plan-module-destroy-multivar")
p := testProvider("aws")
p.DiffFn = testDiffFn
s := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{},
},
&ModuleState{
Path: []string{"root", "child"},
Resources: map[string]*ResourceState{
"aws_instance.foo.0": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "bar0",
},
},
"aws_instance.foo.1": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "bar1",
},
},
},
},
},
}
ctx := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
State: s,
})
plan, err := ctx.Plan(&PlanOpts{Destroy: true})
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanModuleDestroyMultivarStr)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
}
func TestContextPlan_pathVar(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {

View File

@ -790,6 +790,24 @@ module.child:
ID = bar
`
const testTerraformPlanModuleDestroyMultivarStr = `
DIFF:
module.child:
DESTROY MODULE
DESTROY: aws_instance.foo.0
DESTROY: aws_instance.foo.1
STATE:
<no state>
module.child:
aws_instance.foo.0:
ID = bar0
aws_instance.foo.1:
ID = bar1
`
const testTerraformPlanModuleInputStr = `
DIFF:

View File

@ -0,0 +1,8 @@
variable "instance_count" {
default = "1"
}
resource "aws_instance" "foo" {
count = "${var.instance_count}"
bar = "bar"
}

View File

@ -0,0 +1,5 @@
module "child" {
source = "./child"
instance_count = "2"
}