Merge pull request #32464 from hashicorp/jbardin/output-check-refs

Ensure we have all references for output preconditions
This commit is contained in:
James Bardin 2023-01-05 14:29:43 -05:00 committed by GitHub
commit e200f53ec0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 7 deletions

View File

@ -1784,3 +1784,51 @@ resource "test_object" "y" {
_, diags = ctx.Apply(plan, m)
assertNoErrors(t, diags)
}
// ensure all references from preconditions are tracked through plan and apply
func TestContext2Apply_preconditionErrorMessageRef(t *testing.T) {
p := testProvider("test")
ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})
m := testModuleInline(t, map[string]string{
"main.tf": `
module "nested" {
source = "./mod"
}
output "nested_a" {
value = module.nested.a
}
`,
"mod/main.tf": `
variable "boop" {
default = "boop"
}
variable "msg" {
default = "Incorrect boop."
}
output "a" {
value = "x"
precondition {
condition = var.boop == "boop"
error_message = var.msg
}
}
`,
})
plan, diags := ctx.Plan(m, states.NewState(), &PlanOpts{
Mode: plans.NormalMode,
})
assertNoErrors(t, diags)
_, diags = ctx.Apply(plan, m)
assertNoErrors(t, diags)
}

View File

@ -278,19 +278,21 @@ func (n *NodeApplyableOutput) ReferenceableAddrs() []addrs.Referenceable {
}
func referencesForOutput(c *configs.Output) []*addrs.Reference {
var refs []*addrs.Reference
impRefs, _ := lang.ReferencesInExpr(c.Expr)
expRefs, _ := lang.References(c.DependsOn)
l := len(impRefs) + len(expRefs)
if l == 0 {
return nil
}
refs := make([]*addrs.Reference, 0, l)
refs = append(refs, impRefs...)
refs = append(refs, expRefs...)
for _, check := range c.Preconditions {
checkRefs, _ := lang.ReferencesInExpr(check.Condition)
refs = append(refs, checkRefs...)
condRefs, _ := lang.ReferencesInExpr(check.Condition)
refs = append(refs, condRefs...)
errRefs, _ := lang.ReferencesInExpr(check.ErrorMessage)
refs = append(refs, errRefs...)
}
return refs
}