Skip to content
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
48 changes: 48 additions & 0 deletions internal/terraform/context_apply2_test.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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