8000 fix(cli): the LoadBalancerProvider doesn't match LBs when querying by… · aws/aws-cdk@f75dc72 · GitHub
[go: up one dir, main page]

Skip to content

Commit f75dc72

Browse files
fix(cli): the LoadBalancerProvider doesn't match LBs when querying by a subset of tags (#32164)
There was a regression in the load balancer lookup, in which we started requiring that the set of tags in the query is strictly the same as the set of tags in the load balancer (rather than merely a subset of it). Remove the length equality constraint and also simplify the code to make the intent clearer. Fixes #32161. ### Checklist - [ ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* Co-authored-by: Momo Kornher <kornherm@amazon.co.uk>
1 parent 089e9d8 commit f75dc72

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

packages/aws-cdk/lib/context-providers/load-balancers.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,12 @@ class LoadBalancerProvider {
160160
}
161161
return (await this.describeTags(loadBalancers.map((lb) => lb.LoadBalancerArn!)))
162162
.filter((tagDescription) => {
163-
return (
164-
tagDescription.Tags?.length === this.filter.loadBalancerTags?.length &&
165-
tagDescription.Tags?.filter(
166-
(tag) =>
167-
!this.filter.loadBalancerTags!.some((filter) => {
168-
return filter.key === tag.Key && filter.value === tag.Value;
169-
}),
170-
).length === 0
171-
);
163+
// For every tag in the filter, there is some tag in the LB that matches it.
164+
// In other words, the set of tags in the filter is a subset of the set of tags in the LB.
165+
return this.filter.loadBalancerTags!.every((filter) => {
166+
return tagDescription.Tags?.some((tag) =>
167+
filter.key === tag.Key && filter.value === tag.Value);
168+
});
172169
})
173170
.flatMap((tag) => loadBalancers.filter((loadBalancer) => tag.ResourceArn === loadBalancer.LoadBalancerArn));
174171
}

packages/aws-cdk/test/context-providers/load-balancers.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,52 @@ describe('load balancer context provider plugin', () => {
199199
});
200200
});
201201

202+
test('looks up by tags - query by subset', async () => {
203+
// GIVEN
204+
mockElasticLoadBalancingV2Client
205+
.on(DescribeLoadBalancersCommand)
206+
.resolves({
207+
LoadBalancers: [
208+
{
209+
IpAddressType: 'ipv4',
210+
LoadBalancerArn: 'arn:load-balancer2',
211+
DNSName: 'dns2.example.com',
212+
CanonicalHostedZoneId: 'Z1234',
213+
SecurityGroups: ['sg-1234'],
214+
VpcId: 'vpc-1234',
215+
Type: 'application',
216+
},
217+
],
218+
})
219+
.on(DescribeTagsCommand)
220+
.resolves({
221+
TagDescriptions: [
222+
{
223+
ResourceArn: 'arn:load-balancer2',
224+
Tags: [
225+
// Load balancer has two tags...
226+
{ Key: 'some', Value: 'tag' },
227+
{ Key: 'second', Value: 'tag2' },
228+
],
229+
},
230+
],
231+
});
232+
const provider = new LoadBalancerContextProviderPlugin(mockSDK);
233+
234+
// WHEN
235+
const result = await provider.getValue({
236+
account: '1234',
237+
region: 'us-east-1',
238+
loadBalancerType: LoadBalancerType.APPLICATION,
239+
loadBalancerTags: [
240+
// ...but we are querying for only one of them
241+
{ key: 'second', value: 'tag2' },
242+
],
243+
});
244+
245+
expect(result.loadBalancerArn).toEqual('arn:load-balancer2');
246+
});
247+
202248
test('filters by type', async () => {
203249
// GIVEN
204250
mockElasticLoadBalancingV2Client

0 commit comments

Comments
 (0)
0