From 4a002bf10b19238006fccea646ef38957d1bcd57 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 10 May 2018 17:56:19 -0700 Subject: [PATCH] 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. --- terraform/eval_validate_selfref.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/terraform/eval_validate_selfref.go b/terraform/eval_validate_selfref.go index 4ad9eeae7..b4f060603 100644 --- a/terraform/eval_validate_selfref.go +++ b/terraform/eval_validate_selfref.go @@ -25,17 +25,25 @@ 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 { - if ref.Subject.String() == addrStr { - diags = diags.Append(&hcl.Diagnostic{ - Severity: hcl.DiagError, - Summary: "Self-referential block", - Detail: fmt.Sprintf("Configuration for %s may not refer to itself.", addrStr), - Subject: ref.SourceRange.ToHCL().Ptr(), - }) + for _, addrStr := range addrStrs { + if ref.Subject.String() == addrStr { + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Self-referential block", + Detail: fmt.Sprintf("Configuration for %s may not refer to itself.", addrStr), + Subject: ref.SourceRange.ToHCL().Ptr(), + }) + } } }