Skip to content

Commit bfce780

Browse files
committed
lang/eval: more evalContext fixups
self references do not need to be added to `managedResource`, and in fact that could cause issues later if self is allowed in contexts other than managed resources. Coalesce 2 cases in the Referenceable switch, be take the ContainingResource address of an instance beforehand.
1 parent fed4e82 commit bfce780

File tree

2 files changed

+9
-43
lines changed

2 files changed

+9
-43
lines changed

lang/eval.go

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,14 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
225225
continue
226226
}
227227

228-
// self can only be used within a resource instance
229-
subj := selfAddr.(addrs.ResourceInstance)
230-
231228
if selfAddr == addrs.Self {
232229
// Programming error: the self address cannot alias itself.
233230
panic("scope SelfAddr attempting to alias itself")
234231
}
235232

233+
// self can only be used within a resource instance
234+
subj := selfAddr.(addrs.ResourceInstance)
235+
236236
val, valDiags := normalizeRefValue(s.Data.GetResource(subj.ContainingResource(), rng))
237237

238238
diags = diags.Append(valDiags)
@@ -249,16 +249,16 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
249249
self = val
250250
}
251251

252-
r := subj.Resource
253-
if managedResources[r.Type] == nil {
254-
managedResources[r.Type] = make(map[string]cty.Value)
255-
}
256-
managedResources[r.Type][r.Name] = val
257252
continue
258253
}
259254

260255
// This type switch must cover all of the "Referenceable" implementations
261-
// in package addrs.
256+
// in package addrs, however we are removing the possibility of
257+
// ResourceInstance beforehand.
258+
if addr, ok := rawSubj.(addrs.ResourceInstance); ok {
259+
rawSubj = addr.ContainingResource()
260+
}
261+
262262
switch subj := rawSubj.(type) {
263263
case addrs.Resource:
264264
var into map[string]map[string]cty.Value
@@ -280,27 +280,6 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
280280
}
281281
into[r.Type][r.Name] = val
282282

283-
case addrs.ResourceInstance:
284-
var into map[string]map[string]cty.Value
285-
switch subj.Resource.Mode {
286-
case addrs.ManagedResourceMode:
287-
into = managedResources
288-
case addrs.DataResourceMode:
289-
into = dataResources
290-
default:
291-
panic(fmt.Errorf("unsupported ResourceMode %s", subj.Resource.Mode))
292-
}
293-
294-
val, valDiags := normalizeRefValue(s.Data.GetResource(subj.ContainingResource(), rng))
295-
296-
diags = diags.Append(valDiags)
297-
298-
r := subj.Resource
299-
if into[r.Type] == nil {
300-
into[r.Type] = make(map[string]cty.Value)
301-
}
302-
into[r.Type][r.Name] = val
303-
304283
case addrs.ModuleCallInstance:
305284
val, valDiags := normalizeRefValue(s.Data.GetModuleInstance(subj, rng))
306285
diags = diags.Append(valDiags)

lang/eval_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,6 @@ func TestScopeEvalContext(t *testing.T) {
237237
{
238238
`self.baz`,
239239
map[string]cty.Value{
240-
// In the test function below we set "SelfAddr" to be
241-
// one of the resources in our dataset, causing it to get
242-
// expanded here and then copied into "self".
243-
"null_resource": cty.ObjectVal(map[string]cty.Value{
244-
"multi": cty.TupleVal([]cty.Value{
245-
cty.ObjectVal(map[string]cty.Value{
246-
"attr": cty.StringVal("multi0"),
247-
}),
248-
cty.ObjectVal(map[string]cty.Value{
249-
"attr": cty.StringVal("multi1"),
250-
}),
251-
}),
252-
}),
253240
"self": cty.ObjectVal(map[string]cty.Value{
254241
"attr": cty.StringVal("multi1"),
255242
}),

0 commit comments

Comments
 (0)