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

Ensure we have all references for output preconditions #32464

Merged
merged 2 commits into from Jan 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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