Skip to content

Commit e72d293

Browse files
radeksimkodbanck
andauthored
reference: Fix matching for targets which involve ranges & both addresses (#157)
Co-authored-by: Daniel Banck <[email protected]>
1 parent fe484f8 commit e72d293

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

reference/target.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,12 @@ func (target Target) Matches(origin MatchableOrigin) bool {
193193
}
194194
}
195195

196-
// Reject origin if it's outside the targetable range
196+
// If the target is only targetable from a particular range
197+
// we confirm that the origin is within that range.
198+
targetRangeMatches := true
197199
if target.TargetableFromRangePtr != nil && !rangeOverlaps(*target.TargetableFromRangePtr, origin.OriginRange()) {
198-
return false
200+
targetRangeMatches = false
199201
}
200202

201-
return (target.LocalAddr.Equals(localOriginAddr) || target.Addr.Equals(originAddr)) && matchesCons
203+
return ((target.LocalAddr.Equals(localOriginAddr) && targetRangeMatches) || target.Addr.Equals(originAddr)) && matchesCons
202204
}

reference/targets_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,79 @@ func TestTargets_Match(t *testing.T) {
193193
},
194194
true,
195195
},
196+
{
197+
"match of global nested target with local addrs set",
198+
Targets{
199+
{
200+
Addr: lang.Address{
201+
lang.RootStep{Name: "aws_acmpca_certificate"},
202+
lang.AttrStep{Name: "foo"},
203+
},
204+
LocalAddr: lang.Address{
205+
lang.RootStep{Name: "self"},
206+
},
207+
Type: cty.Object(map[string]cty.Type{
208+
"signing_algorithm": cty.String,
209+
}),
210+
ScopeId: "resource",
211+
NestedTargets: Targets{
212+
{
213+
Addr: lang.Address{
214+
lang.RootStep{Name: "aws_acmpca_certificate"},
215+
lang.AttrStep{Name: "foo"},
216+
lang.AttrStep{Name: "signing_algorithm"},
217+
},
218+
LocalAddr: lang.Address{
219+
lang.RootStep{Name: "self"},
220+
lang.AttrStep{Name: "signing_algorithm"},
221+
},
222+
Type: cty.String,
223+
TargetableFromRangePtr: &hcl.Range{
224+
Filename: "main.tf",
225+
Start: hcl.Pos{Line: 26, Column: 41, Byte: 360},
226+
End: hcl.Pos{Line: 41, Column: 2, Byte: 657},
227+
},
228+
},
229+
},
230+
},
231+
},
232+
LocalOrigin{
233+
Addr: lang.Address{
234+
lang.RootStep{Name: "aws_acmpca_certificate"},
235+
lang.AttrStep{Name: "foo"},
236+
lang.AttrStep{Name: "signing_algorithm"},
237+
},
238+
239+
Constraints: OriginConstraints{
240+
{OfType: cty.DynamicPseudoType},
241+
},
242+
Range: hcl.Range{
243+
Filename: "main.tf",
244+
Start: hcl.Pos{Line: 44, Column: 11, Byte: 684},
245+
End: hcl.Pos{Line: 44, Column: 55, Byte: 728},
246+
},
247+
},
248+
Targets{
249+
Target{
250+
Addr: lang.Address{
251+
lang.RootStep{Name: "aws_acmpca_certificate"},
252+
lang.AttrStep{Name: "foo"},
253+
lang.AttrStep{Name: "signing_algorithm"},
254+
},
255+
LocalAddr: lang.Address{
256+
lang.RootStep{Name: "self"},
257+
lang.AttrStep{Name: "signing_algorithm"},
258+
},
259+
Type: cty.String,
260+
TargetableFromRangePtr: &hcl.Range{
261+
Filename: "main.tf",
262+
Start: hcl.Pos{Line: 26, Column: 41, Byte: 360},
263+
End: hcl.Pos{Line: 41, Column: 2, Byte: 657},
264+
},
265+
},
266+
},
267+
true,
268+
},
196269
}
197270
for i, tc := range testCases {
198271
t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {

0 commit comments

Comments
 (0)