allow evaluation of 0 instances during apply

While this was easier to spot during plan, it is also possible to
evaluate resources with 0 instances during apply as well.

This doesn't effect the failure when scaling CBD instances, it only
changes the fact that the inconsistent value is no longer unknown.
This commit is contained in:
James Bardin 2020-09-16 10:45:33 -04:00
parent 7b2f66c403
commit 7156649336
2 changed files with 67 additions and 6 deletions

View File

@ -11912,3 +11912,66 @@ resource "test_instance" "b" {
t.Fatalf("apply errors: %s", diags.Err()) 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())
}
}

View File

@ -648,12 +648,10 @@ func (d *evaluationStateData) GetResource(addr addrs.Resource, rng tfdiags.Sourc
if rs == nil { if rs == nil {
switch d.Operation { switch d.Operation {
case walkPlan: case walkPlan, walkApply:
// During plan as we evaluate each removed instance they are removed // During plan and apply as we evaluate each removed instance they
// from the temporary working state. Since we know there there are // are removed from the working state. Since we know there are no
// no instances, and resources might be referenced in a context // instances, return an empty container of the expected type.
// that needs to be known during plan, return an empty container of
// the expected type.
switch { switch {
case config.Count != nil: case config.Count != nil:
return cty.EmptyTupleVal, diags return cty.EmptyTupleVal, diags