walkDestroy is a form of "apply"

When computing the count value, make sure to include walkDestroy with
walkApply, as the former is only a special case of the latter.

When applying a saved plan, the computed count values are lost and we
can no longer query the state for those values. The apply walk was
already considered in the `resourceCountMax` function, but the destroy
walk was not.  This worked when destroying in a single operation
("terraform destroy"), since the state would still be updated with the
latest counts from the plan.
This commit is contained in:
James Bardin 2018-04-10 11:32:52 -04:00
parent be6fffb171
commit cfefeec926
3 changed files with 96 additions and 1 deletions

View File

@ -9578,6 +9578,89 @@ func TestContext2Apply_plannedInterpolatedCount(t *testing.T) {
}
}
func TestContext2Apply_plannedDestroyInterpolatedCount(t *testing.T) {
m := testModule(t, "plan-destroy-interpolated-count")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
providerResolver := ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
)
s := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.a.0": {
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
Provider: "provider.aws",
},
"aws_instance.a.1": {
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
Provider: "provider.aws",
},
},
Outputs: map[string]*OutputState{
"out": {
Type: "list",
Value: []string{"foo", "foo"},
},
},
},
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
ProviderResolver: providerResolver,
State: s,
Destroy: true,
})
plan, err := ctx.Plan()
if err != nil {
t.Fatalf("plan failed: %s", err)
}
// We'll marshal and unmarshal the plan here, to ensure that we have
// a clean new context as would be created if we separately ran
// terraform plan -out=tfplan && terraform apply tfplan
var planBuf bytes.Buffer
err = WritePlan(plan, &planBuf)
if err != nil {
t.Fatalf("failed to write plan: %s", err)
}
plan, err = ReadPlan(&planBuf)
if err != nil {
t.Fatalf("failed to read plan: %s", err)
}
ctx, err = plan.Context(&ContextOpts{
ProviderResolver: providerResolver,
Destroy: true,
})
if err != nil {
t.Fatalf("failed to create context for plan: %s", err)
}
// Applying the plan should now succeed
_, err = ctx.Apply()
if err != nil {
t.Fatalf("apply failed: %s", err)
}
}
func TestContext2Apply_scaleInMultivarRef(t *testing.T) {
m := testModule(t, "apply-resource-scale-in")

View File

@ -789,7 +789,8 @@ func (i *Interpolater) resourceCountMax(
// If we're NOT applying, then we assume we can read the count
// from the state. Plan and so on may not have any state yet so
// we do a full interpolation.
if i.Operation != walkApply {
// Don't forget walkDestroy, which is a special case of walkApply
if !(i.Operation == walkApply || i.Operation == walkDestroy) {
if cr == nil {
return 0, nil
}

View File

@ -0,0 +1,11 @@
variable "list" {
default = ["1", "2"]
}
resource "aws_instance" "a" {
count = "${length(var.list)}"
}
output "out" {
value = "${aws_instance.a.*.id}"
}