Skip to content

Commit 696290e

Browse files
authored
Merge pull request #26264 from hashicorp/jbardin/evaluate-destroy
allow evaluation of 0 instances during apply
2 parents 55d58cb + 7156649 commit 696290e

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

terraform/context_apply_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11944,3 +11944,66 @@ resource "test_instance" "b" {
1194411944
t.Fatalf("apply errors: %s", diags.Err())
1194511945
}
1194611946
}
11947+
11948+
func TestContext2Apply_removeReferencedResource(t *testing.T) {
11949+
m := testModuleInline(t, map[string]string{
11950+
"main.tf": `
11951+
variable "ct" {
11952+
}
11953+
11954+
resource "test_resource" "to_remove" {
11955+
count = var.ct
11956+
}
11957+
11958+
resource "test_resource" "c" {
11959+
value = join("", test_resource.to_remove[*].id)
11960+
}
11961+
`})
11962+
11963+
p := testProvider("test")
11964+
p.ApplyFn = testApplyFn
11965+
p.DiffFn = testDiffFn
11966+
11967+
ctx := testContext2(t, &ContextOpts{
11968+
Config: m,
11969+
Providers: map[addrs.Provider]providers.Factory{
11970+
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
11971+
},
11972+
Variables: InputValues{
11973+
"ct": &InputValue{
11974+
Value: cty.NumberIntVal(1),
11975+
},
11976+
},
11977+
})
11978+
11979+
if _, diags := ctx.Plan(); diags.HasErrors() {
11980+
t.Fatalf("plan errors: %s", diags.Err())
11981+
}
11982+
11983+
state, diags := ctx.Apply()
11984+
if diags.HasErrors() {
11985+
t.Fatalf("apply errors: %s", diags.Err())
11986+
}
11987+
11988+
ctx = testContext2(t, &ContextOpts{
11989+
Config: m,
11990+
Providers: map[addrs.Provider]providers.Factory{
11991+
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
11992+
},
11993+
Variables: InputValues{
11994+
"ct": &InputValue{
11995+
Value: cty.NumberIntVal(0),
11996+
},
11997+
},
11998+
State: state,
11999+
})
12000+
12001+
if _, diags := ctx.Plan(); diags.HasErrors() {
12002+
t.Fatalf("plan errors: %s", diags.Err())
12003+
}
12004+
12005+
_, diags = ctx.Apply()
12006+
if diags.HasErrors() {
12007+
t.Fatalf("apply errors: %s", diags.Err())
12008+
}
12009+
}

terraform/evaluate.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,10 @@ func (d *evaluationStateData) GetResource(addr addrs.Resource, rng tfdiags.Sourc
648648

649649
if rs == nil {
650650
switch d.Operation {
651-
case walkPlan:
652-
// During plan as we evaluate each removed instance they are removed
653-
// from the temporary working state. Since we know there there are
654-
// no instances, and resources might be referenced in a context
655-
// that needs to be known during plan, return an empty container of
656-
// the expected type.
651+
case walkPlan, walkApply:
652+
// During plan and apply as we evaluate each removed instance they
653+
// are removed from the working state. Since we know there are no
654+
// instances, return an empty container of the expected type.
657655
switch {
658656
case config.Count != nil:
659657
return cty.EmptyTupleVal, diags

0 commit comments

Comments
 (0)