Skip to content

Commit

Permalink
Merge pull request #26264 from hashicorp/jbardin/evaluate-destroy
Browse files Browse the repository at this point in the history
allow evaluation of 0 instances during apply
  • Loading branch information
jbardin authored Sep 16, 2020
2 parents 55d58cb + 7156649 commit 696290e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
63 changes: 63 additions & 0 deletions terraform/context_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11944,3 +11944,66 @@ resource "test_instance" "b" {
t.Fatalf("apply errors: %s", diags.Err())
}
}

func TestContext2Apply_removeReferencedResource(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
variable "ct" {
}
resource "test_resource" "to_remove" {
count = var.ct
}
resource "test_resource" "c" {
value = join("", test_resource.to_remove[*].id)
}
`})

p := testProvider("test")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn

ctx := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
Variables: InputValues{
"ct": &InputValue{
Value: cty.NumberIntVal(1),
},
},
})

if _, diags := ctx.Plan(); diags.HasErrors() {
t.Fatalf("plan errors: %s", diags.Err())
}

state, diags := ctx.Apply()
if diags.HasErrors() {
t.Fatalf("apply errors: %s", diags.Err())
}

ctx = testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
Variables: InputValues{
"ct": &InputValue{
Value: cty.NumberIntVal(0),
},
},
State: state,
})

if _, diags := ctx.Plan(); diags.HasErrors() {
t.Fatalf("plan errors: %s", diags.Err())
}

_, diags = ctx.Apply()
if diags.HasErrors() {
t.Fatalf("apply errors: %s", diags.Err())
}
}
10 changes: 4 additions & 6 deletions terraform/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,10 @@ func (d *evaluationStateData) GetResource(addr addrs.Resource, rng tfdiags.Sourc

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

0 comments on commit 696290e

Please sign in to comment.