Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow evaluation of 0 instances during apply #26264

Merged
merged 1 commit into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions terraform/context_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11912,3 +11912,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