diff --git a/lang/eval.go b/lang/eval.go index a2408f150..d3cb5dbb1 100644 --- a/lang/eval.go +++ b/lang/eval.go @@ -225,14 +225,14 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl continue } - // self can only be used within a resource instance - subj := selfAddr.(addrs.ResourceInstance) - if selfAddr == addrs.Self { // Programming error: the self address cannot alias itself. panic("scope SelfAddr attempting to alias itself") } + // self can only be used within a resource instance + subj := selfAddr.(addrs.ResourceInstance) + val, valDiags := normalizeRefValue(s.Data.GetResource(subj.ContainingResource(), rng)) diags = diags.Append(valDiags) @@ -249,16 +249,16 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl self = val } - r := subj.Resource - if managedResources[r.Type] == nil { - managedResources[r.Type] = make(map[string]cty.Value) - } - managedResources[r.Type][r.Name] = val continue } // This type switch must cover all of the "Referenceable" implementations - // in package addrs. + // in package addrs, however we are removing the possibility of + // ResourceInstance beforehand. + if addr, ok := rawSubj.(addrs.ResourceInstance); ok { + rawSubj = addr.ContainingResource() + } + switch subj := rawSubj.(type) { case addrs.Resource: var into map[string]map[string]cty.Value @@ -280,27 +280,6 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl } into[r.Type][r.Name] = val - case addrs.ResourceInstance: - var into map[string]map[string]cty.Value - switch subj.Resource.Mode { - case addrs.ManagedResourceMode: - into = managedResources - case addrs.DataResourceMode: - into = dataResources - default: - panic(fmt.Errorf("unsupported ResourceMode %s", subj.Resource.Mode)) - } - - val, valDiags := normalizeRefValue(s.Data.GetResource(subj.ContainingResource(), rng)) - - diags = diags.Append(valDiags) - - r := subj.Resource - if into[r.Type] == nil { - into[r.Type] = make(map[string]cty.Value) - } - into[r.Type][r.Name] = val - case addrs.ModuleCallInstance: val, valDiags := normalizeRefValue(s.Data.GetModuleInstance(subj, rng)) diags = diags.Append(valDiags) diff --git a/lang/eval_test.go b/lang/eval_test.go index 57763770e..64c1e4542 100644 --- a/lang/eval_test.go +++ b/lang/eval_test.go @@ -237,19 +237,6 @@ func TestScopeEvalContext(t *testing.T) { { `self.baz`, map[string]cty.Value{ - // In the test function below we set "SelfAddr" to be - // one of the resources in our dataset, causing it to get - // expanded here and then copied into "self". - "null_resource": cty.ObjectVal(map[string]cty.Value{ - "multi": cty.TupleVal([]cty.Value{ - cty.ObjectVal(map[string]cty.Value{ - "attr": cty.StringVal("multi0"), - }), - cty.ObjectVal(map[string]cty.Value{ - "attr": cty.StringVal("multi1"), - }), - }), - }), "self": cty.ObjectVal(map[string]cty.Value{ "attr": cty.StringVal("multi1"), }),