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

configs: Fix check block configuration diagnostics #31290

Merged
merged 1 commit into from Jun 22, 2022
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
47 changes: 28 additions & 19 deletions internal/configs/checks.go
Expand Up @@ -40,27 +40,36 @@ type CheckRule struct {
// is found.
func (cr *CheckRule) validateSelfReferences(checkType string, addr addrs.Resource) hcl.Diagnostics {
var diags hcl.Diagnostics
refs, _ := lang.References(cr.Condition.Variables())
for _, ref := range refs {
var refAddr addrs.Resource

switch rs := ref.Subject.(type) {
case addrs.Resource:
refAddr = rs
case addrs.ResourceInstance:
refAddr = rs.Resource
default:
exprs := []hcl.Expression{
cr.Condition,
cr.ErrorMessage,
}
for _, expr := range exprs {
if expr == nil {
continue
}

if refAddr.Equal(addr) {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Invalid reference in %s", checkType),
Detail: fmt.Sprintf("Configuration for %s may not refer to itself.", addr.String()),
Subject: cr.Condition.Range().Ptr(),
})
break
refs, _ := lang.References(expr.Variables())
for _, ref := range refs {
var refAddr addrs.Resource

switch rs := ref.Subject.(type) {
case addrs.Resource:
refAddr = rs
case addrs.ResourceInstance:
refAddr = rs.Resource
default:
continue
}

if refAddr.Equal(addr) {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Invalid reference in %s", checkType),
Detail: fmt.Sprintf("Configuration for %s may not refer to itself.", addr.String()),
Subject: expr.Range().Ptr(),
})
break
}
}
}
return diags
Expand Down
@@ -0,0 +1,29 @@
data "example" "example" {
foo = 5

lifecycle {
precondition {
condition = data.example.example.foo == 5 # ERROR: Invalid reference in precondition
error_message = "Must be five."
}
postcondition {
condition = self.foo == 5
error_message = "Must be five, but is ${data.example.example.foo}." # ERROR: Invalid reference in postcondition
}
}
}

resource "example" "example" {
foo = 5

lifecycle {
precondition {
condition = example.example.foo == 5 # ERROR: Invalid reference in precondition
error_message = "Must be five."
}
postcondition {
condition = self.foo == 5
error_message = "Must be five, but is ${example.example.foo}." # ERROR: Invalid reference in postcondition
}
}
}
@@ -0,0 +1,12 @@
resource "example" "example" {
foo = 5

lifecycle {
precondition { # ERROR: Missing required argument
error_message = "Can a check block fail without a condition?"
}
postcondition { # ERROR: Missing required argument
error_message = "Do not try to pass the check; only realize that there is no check."
}
}
}