Skip to content

Commit

Permalink
Merge pull request #32464 from hashicorp/jbardin/output-check-refs
Browse files Browse the repository at this point in the history
Ensure we have all references for output preconditions
  • Loading branch information
jbardin committed Jan 5, 2023
2 parents 6e2a749 + 45cb048 commit e200f53
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
48 changes: 48 additions & 0 deletions internal/terraform/context_apply2_test.go
Expand Up @@ -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)
}
16 changes: 9 additions & 7 deletions internal/terraform/node_output.go
Expand Up @@ -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
}

Expand Down

0 comments on commit e200f53

Please sign in to comment.