mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 18:01:01 -06:00
core: EvalValidateSelfRef must catch instances referencing their resources
An instance like aws_instance.foo[0] is not permitted to refer to aws_instance.foo, since that result contains the individual instance along with all other instances.
This commit is contained in:
parent
fc5a3c5cec
commit
4a002bf10b
@ -25,17 +25,25 @@ func (n *EvalValidateSelfRef) Eval(ctx EvalContext) (interface{}, error) {
|
|||||||
var diags tfdiags.Diagnostics
|
var diags tfdiags.Diagnostics
|
||||||
addr := n.Addr
|
addr := n.Addr
|
||||||
|
|
||||||
addrStr := addr.String()
|
addrStrs := make([]string, 0, 1)
|
||||||
|
addrStrs = append(addrStrs, addr.String())
|
||||||
|
switch tAddr := addr.(type) {
|
||||||
|
case addrs.ResourceInstance:
|
||||||
|
// A resource instance may not refer to its containing resource either.
|
||||||
|
addrStrs = append(addrStrs, tAddr.ContainingResource().String())
|
||||||
|
}
|
||||||
|
|
||||||
refs, _ := lang.ReferencesInBlock(n.Config, n.Schema)
|
refs, _ := lang.ReferencesInBlock(n.Config, n.Schema)
|
||||||
for _, ref := range refs {
|
for _, ref := range refs {
|
||||||
if ref.Subject.String() == addrStr {
|
for _, addrStr := range addrStrs {
|
||||||
diags = diags.Append(&hcl.Diagnostic{
|
if ref.Subject.String() == addrStr {
|
||||||
Severity: hcl.DiagError,
|
diags = diags.Append(&hcl.Diagnostic{
|
||||||
Summary: "Self-referential block",
|
Severity: hcl.DiagError,
|
||||||
Detail: fmt.Sprintf("Configuration for %s may not refer to itself.", addrStr),
|
Summary: "Self-referential block",
|
||||||
Subject: ref.SourceRange.ToHCL().Ptr(),
|
Detail: fmt.Sprintf("Configuration for %s may not refer to itself.", addrStr),
|
||||||
})
|
Subject: ref.SourceRange.ToHCL().Ptr(),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user