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:
Martin Atkins 2018-05-10 17:56:19 -07:00
parent fc5a3c5cec
commit 4a002bf10b

View File

@ -25,10 +25,17 @@ func (n *EvalValidateSelfRef) Eval(ctx EvalContext) (interface{}, error) {
var diags tfdiags.Diagnostics
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)
for _, ref := range refs {
for _, addrStr := range addrStrs {
if ref.Subject.String() == addrStr {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
@ -38,6 +45,7 @@ func (n *EvalValidateSelfRef) Eval(ctx EvalContext) (interface{}, error) {
})
}
}
}
return nil, diags.NonFatalErr()
}