mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Merge pull request #31290 from hashicorp/alisdair/check-block-missing-condition
configs: Fix check block configuration diagnostics
This commit is contained in:
commit
5df6bac2f7
@ -40,27 +40,36 @@ type CheckRule struct {
|
|||||||
// is found.
|
// is found.
|
||||||
func (cr *CheckRule) validateSelfReferences(checkType string, addr addrs.Resource) hcl.Diagnostics {
|
func (cr *CheckRule) validateSelfReferences(checkType string, addr addrs.Resource) hcl.Diagnostics {
|
||||||
var diags hcl.Diagnostics
|
var diags hcl.Diagnostics
|
||||||
refs, _ := lang.References(cr.Condition.Variables())
|
exprs := []hcl.Expression{
|
||||||
for _, ref := range refs {
|
cr.Condition,
|
||||||
var refAddr addrs.Resource
|
cr.ErrorMessage,
|
||||||
|
}
|
||||||
switch rs := ref.Subject.(type) {
|
for _, expr := range exprs {
|
||||||
case addrs.Resource:
|
if expr == nil {
|
||||||
refAddr = rs
|
|
||||||
case addrs.ResourceInstance:
|
|
||||||
refAddr = rs.Resource
|
|
||||||
default:
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
refs, _ := lang.References(expr.Variables())
|
||||||
|
for _, ref := range refs {
|
||||||
|
var refAddr addrs.Resource
|
||||||
|
|
||||||
if refAddr.Equal(addr) {
|
switch rs := ref.Subject.(type) {
|
||||||
diags = diags.Append(&hcl.Diagnostic{
|
case addrs.Resource:
|
||||||
Severity: hcl.DiagError,
|
refAddr = rs
|
||||||
Summary: fmt.Sprintf("Invalid reference in %s", checkType),
|
case addrs.ResourceInstance:
|
||||||
Detail: fmt.Sprintf("Configuration for %s may not refer to itself.", addr.String()),
|
refAddr = rs.Resource
|
||||||
Subject: cr.Condition.Range().Ptr(),
|
default:
|
||||||
})
|
continue
|
||||||
break
|
}
|
||||||
|
|
||||||
|
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
|
return diags
|
||||||
|
29
internal/configs/testdata/invalid-files/precondition-postcondition-badref.tf
vendored
Normal file
29
internal/configs/testdata/invalid-files/precondition-postcondition-badref.tf
vendored
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
internal/configs/testdata/invalid-files/precondition-postcondition-missing-condition.tf
vendored
Normal file
12
internal/configs/testdata/invalid-files/precondition-postcondition-missing-condition.tf
vendored
Normal 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."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user