@@ -194,8 +194,8 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
194194 // it, since that allows us to gather a full set of any errors and
195195 // warnings, but once we've gathered all the data we'll then skip anything
196196 // that's redundant in the process of populating our values map.
197- dataResources := map [string ]map [string ]map [addrs. InstanceKey ] cty.Value {}
198- managedResources := map [string ]map [string ]map [addrs. InstanceKey ] cty.Value {}
197+ dataResources := map [string ]map [string ]cty.Value {}
198+ managedResources := map [string ]map [string ]cty.Value {}
199199 wholeModules := map [string ]map [addrs.InstanceKey ]cty.Value {}
200200 moduleOutputs := map [string ]map [addrs.InstanceKey ]map [string ]cty.Value {}
201201 inputVariables := map [string ]cty.Value {}
@@ -208,7 +208,6 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
208208
209209 for _ , ref := range refs {
210210 rng := ref .SourceRange
211- isSelf := false
212211
213212 rawSubj := ref .Subject
214213 if rawSubj == addrs .Self {
@@ -226,45 +225,60 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
226225 continue
227226 }
228227
229- // Treat "self" as an alias for the configured self address.
230- rawSubj = selfAddr
231- isSelf = true
232-
233- if rawSubj == addrs .Self {
228+ if selfAddr == addrs .Self {
234229 // Programming error: the self address cannot alias itself.
235230 panic ("scope SelfAddr attempting to alias itself" )
236231 }
232+
233+ // self can only be used within a resource instance
234+ subj := selfAddr .(addrs.ResourceInstance )
235+
236+ val , valDiags := normalizeRefValue (s .Data .GetResource (subj .ContainingResource (), rng ))
237+
238+ diags = diags .Append (valDiags )
239+
240+ // Self is an exception in that it must always resolve to a
241+ // particular instance. We will still insert the full resource into
242+ // the context below.
243+ switch k := subj .Key .(type ) {
244+ case addrs.IntKey :
245+ self = val .Index (cty .NumberIntVal (int64 (k )))
246+ case addrs.StringKey :
247+ self = val .Index (cty .StringVal (string (k )))
248+ default :
249+ self = val
250+ }
251+
252+ continue
237253 }
238254
239255 // This type switch must cover all of the "Referenceable" implementations
240- // in package addrs.
241- switch subj := rawSubj .(type ) {
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+ }
242261
243- case addrs.ResourceInstance :
244- var into map [string ]map [string ]map [addrs.InstanceKey ]cty.Value
245- switch subj .Resource .Mode {
262+ switch subj := rawSubj .(type ) {
263+ case addrs.Resource :
264+ var into map [string ]map [string ]cty.Value
265+ switch subj .Mode {
246266 case addrs .ManagedResourceMode :
247267 into = managedResources
248268 case addrs .DataResourceMode :
249269 into = dataResources
250270 default :
251- panic (fmt .Errorf ("unsupported ResourceMode %s" , subj .Resource . Mode ))
271+ panic (fmt .Errorf ("unsupported ResourceMode %s" , subj .Mode ))
252272 }
253273
254- val , valDiags := normalizeRefValue (s .Data .GetResourceInstance (subj , rng ))
274+ val , valDiags := normalizeRefValue (s .Data .GetResource (subj , rng ))
255275 diags = diags .Append (valDiags )
256276
257- r := subj . Resource
277+ r := subj
258278 if into [r .Type ] == nil {
259- into [r .Type ] = make (map [string ]map [addrs.InstanceKey ]cty.Value )
260- }
261- if into [r.Type ][r.Name ] == nil {
262- into [r.Type ][r.Name ] = make (map [addrs.InstanceKey ]cty.Value )
263- }
264- into [r.Type ][r.Name ][subj .Key ] = val
265- if isSelf {
266- self = val
279+ into [r .Type ] = make (map [string ]cty.Value )
267280 }
281+ into [r.Type ][r.Name ] = val
268282
269283 case addrs.ModuleCallInstance :
270284 val , valDiags := normalizeRefValue (s .Data .GetModuleInstance (subj , rng ))
@@ -274,9 +288,6 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
274288 wholeModules [subj .Call .Name ] = make (map [addrs.InstanceKey ]cty.Value )
275289 }
276290 wholeModules [subj .Call .Name ][subj .Key ] = val
277- if isSelf {
278- self = val
279- }
280291
281292 case addrs.ModuleCallOutput :
282293 val , valDiags := normalizeRefValue (s .Data .GetModuleInstanceOutput (subj , rng ))
@@ -291,57 +302,36 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
291302 moduleOutputs [callName ][callKey ] = make (map [string ]cty.Value )
292303 }
293304 moduleOutputs [callName ][callKey ][subj .Name ] = val
294- if isSelf {
295- self = val
296- }
297305
298306 case addrs.InputVariable :
299307 val , valDiags := normalizeRefValue (s .Data .GetInputVariable (subj , rng ))
300308 diags = diags .Append (valDiags )
301309 inputVariables [subj .Name ] = val
302- if isSelf {
303- self = val
304- }
305310
306311 case addrs.LocalValue :
307312 val , valDiags := normalizeRefValue (s .Data .GetLocalValue (subj , rng ))
308313 diags = diags .Append (valDiags )
309314 localValues [subj .Name ] = val
310- if isSelf {
311- self = val
312- }
313315
314316 case addrs.PathAttr :
315317 val , valDiags := normalizeRefValue (s .Data .GetPathAttr (subj , rng ))
316318 diags = diags .Append (valDiags )
317319 pathAttrs [subj .Name ] = val
318- if isSelf {
319- self = val
320- }
321320
322321 case addrs.TerraformAttr :
323322 val , valDiags := normalizeRefValue (s .Data .GetTerraformAttr (subj , rng ))
324323 diags = diags .Append (valDiags )
325324 terraformAttrs [subj .Name ] = val
326- if isSelf {
327- self = val
328- }
329325
330326 case addrs.CountAttr :
331327 val , valDiags := normalizeRefValue (s .Data .GetCountAttr (subj , rng ))
332328 diags = diags .Append (valDiags )
333329 countAttrs [subj .Name ] = val
334- if isSelf {
335- self = val
336- }
337330
338331 case addrs.ForEachAttr :
339332 val , valDiags := normalizeRefValue (s .Data .GetForEachAttr (subj , rng ))
340333 diags = diags .Append (valDiags )
341334 forEachAttrs [subj .Name ] = val
342- if isSelf {
343- self = val
344- }
345335
346336 default :
347337 // Should never happen
@@ -367,13 +357,9 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
367357 return ctx , diags
368358}
369359
370- func buildResourceObjects (resources map [string ]map [string ]map [addrs. InstanceKey ] cty.Value ) map [string ]cty.Value {
360+ func buildResourceObjects (resources map [string ]map [string ]cty.Value ) map [string ]cty.Value {
371361 vals := make (map [string ]cty.Value )
372- for typeName , names := range resources {
373- nameVals := make (map [string ]cty.Value )
374- for name , keys := range names {
375- nameVals [name ] = buildInstanceObjects (keys )
376- }
362+ for typeName , nameVals := range resources {
377363 vals [typeName ] = cty .ObjectVal (nameVals )
378364 }
379365 return vals
0 commit comments