Merge pull request #31290 from hashicorp/alisdair/check-block-missing-condition

configs: Fix check block configuration diagnostics
This commit is contained in:
Alisdair McDiarmid 2022-06-22 11:51:46 -04:00 committed by GitHub
commit 5df6bac2f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 18 deletions

View File

@ -40,7 +40,15 @@ 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())
exprs := []hcl.Expression{
cr.Condition,
cr.ErrorMessage,
}
for _, expr := range exprs {
if expr == nil {
continue
}
refs, _ := lang.References(expr.Variables())
for _, ref := range refs {
var refAddr addrs.Resource
@ -58,11 +66,12 @@ func (cr *CheckRule) validateSelfReferences(checkType string, addr addrs.Resourc
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(),
Subject: expr.Range().Ptr(),
})
break
}
}
}
return diags
}

View File

@ -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
}
}
}

View File

@ -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."
}
}
}