Skip to content

Commit 04061f2

Browse files
authored
chore(ec2): update PrefixList.fromLookup() to expect the result from CcApi context provider has exactly one resource (#34199)
### Issue # (if applicable) Follow-up to #33619. ### Reason for this change CcApi context provider now can expect the matched count of resources: aws/aws-cdk-cli#251. `PrefixList.fromLookup()` is needed to be updated using this feature not to persist invalid results in `cdk.context.json`. See also aws/aws-cdk-cli#257. ### Description of changes - Bumped `@aws-cdk/cloud-assembly-schema` to latest ^43.6.0. - Specify `expectedMatchCount: 'exactly-one'` to expect exactly one prefix list id is returned. - Updated validation to check unexpected result. Actual error will be returned from the context provider. ### Describe any new or updated permissions being added N/A ### Description of how you validated changes Updated integ tests. ### Checklist - [x] 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*
1 parent ca065bb commit 04061f2

File tree

5 files changed

+17
-30
lines changed

5 files changed

+17
-30
lines changed

packages/@aws-cdk/cx-api/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@
8282
"semver": "^7.7.2"
8383
},
8484
"peerDependencies": {
85-
"@aws-cdk/cloud-assembly-schema": ">=41.0.0"
85+
"@aws-cdk/cloud-assembly-schema": ">=43.6.0"
8686
},
8787
"license": "Apache-2.0",
8888
"devDependencies": {
8989
"@aws-cdk/cdk-build-tools": "0.0.0",
90-
"@aws-cdk/cloud-assembly-schema": "^41.2.0",
90+
"@aws-cdk/cloud-assembly-schema": "^43.6.0",
9191
"@aws-cdk/pkglint": "0.0.0",
9292
"@types/jest": "^29.5.14",
9393
"@types/mock-fs": "^4.13.4",

packages/aws-cdk-lib/aws-ec2/lib/prefix-list.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,14 @@ export class PrefixList extends PrefixListBase {
148148
...(options.addressFamily ? { AddressFamily: options.addressFamily } : undefined),
149149
},
150150
propertiesToReturn: ['PrefixListId'],
151+
expectedMatchCount: 'exactly-one',
151152
} satisfies Omit<cxschema.CcApiContextQuery, 'account'|'region'>,
152153
dummyValue: [dummyResponse] satisfies PrefixListContextResponse[],
153154
}).value;
154155

155156
// getValue returns a list of result objects. We are expecting 1 result or Error.
156-
if (response.length === 0) {
157-
throw new ValidationError(`Could not find any managed prefix lists matching ${JSON.stringify(options)}`, scope);
158-
} else if (response.length > 1) {
159-
throw new ValidationError(`Found ${response.length} managed prefix lists matching ${JSON.stringify(options)}; please narrow the search criteria`, scope);
157+
if (response.length !== 1) {
158+
throw new ValidationError('Unexpected response received from the context provider. Please clear out the context key using `cdk context --remove` and try again.', scope);
160159
}
161160

162161
const prefixList = response[0];

packages/aws-cdk-lib/aws-ec2/test/prefix-list.test.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ describe('prefix list', () => {
130130
PrefixListName: 'com.amazonaws.us-east-1.testprefixlist',
131131
},
132132
propertiesToReturn: ['PrefixListId'],
133+
expectedMatchCount: 'exactly-one',
133134
},
134135
dummyValue: [
135136
{ PrefixListId: 'pl-xxxxxxxx' },
@@ -164,6 +165,7 @@ describe('prefix list', () => {
164165
AddressFamily: 'IPv6',
165166
},
166167
propertiesToReturn: ['PrefixListId'],
168+
expectedMatchCount: 'exactly-one',
167169
},
168170
dummyValue: [
169171
{ PrefixListId: 'pl-xxxxxxxx' },
@@ -182,9 +184,11 @@ describe('prefix list', () => {
182184
}).toThrow('All arguments to look up a managed prefix list must be concrete (no Tokens)');
183185
});
184186

185-
test('fromLookup throws if not found', () => {
187+
test.each([
188+
[[]],
189+
[[{ PrefixListId: 'pl-xxxxxxxx' }, { PrefixListId: 'pl-yyyyyyyy' }]],
190+
])('fromLookup throws for unexpected result', (resultObjs) => {
186191
// GIVEN
187-
const resultObjs = [];
188192
jest.spyOn(ContextProvider, 'getValue').mockReturnValue({ value: resultObjs });
189193

190194
// WHEN
@@ -195,22 +199,6 @@ describe('prefix list', () => {
195199
PrefixList.fromLookup(stack, 'PrefixList', {
196200
prefixListName: 'com.amazonaws.us-east-1.missingprefixlist',
197201
});
198-
}).toThrow('Could not find any managed prefix lists matching');
199-
});
200-
201-
test('fromLookup throws if multiple resources found', () => {
202-
// GIVEN
203-
const resultObjs = [{ PrefixListId: 'pl-xxxxxxxx' }, { PrefixListId: 'pl-yyyyyyyy' }];
204-
jest.spyOn(ContextProvider, 'getValue').mockReturnValue({ value: resultObjs });
205-
206-
// WHEN
207-
const stack = new Stack(undefined, undefined, { env: { region: 'us-east-1', account: '123456789012' } });
208-
209-
// THEN
210-
expect(() => {
211-
PrefixList.fromLookup(stack, 'PrefixList', {
212-
prefixListName: 'com.amazonaws.us-east-1.missingprefixlist',
213-
});
214-
}).toThrow('Found 2 managed prefix lists matching');
202+
}).toThrow('Unexpected response received from the context provider.');
215203
});
216204
});

packages/aws-cdk-lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"dependencies": {
122122
"@aws-cdk/asset-awscli-v1": "2.2.237",
123123
"@aws-cdk/asset-node-proxy-agent-v6": "^2.1.0",
124-
"@aws-cdk/cloud-assembly-schema": "^41.2.0",
124+
"@aws-cdk/cloud-assembly-schema": "^43.6.0",
125125
"@balena/dockerignore": "^1.0.2",
126126
"case": "1.6.3",
127127
"fs-extra": "^11.3.0",

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@
7474
"@aws-cdk/service-spec-types" "^0.0.141"
7575
"@cdklabs/tskb" "^0.0.3"
7676

77-
"@aws-cdk/cloud-assembly-schema@^41.2.0":
78-
version "41.2.0"
79-
resolved "https://registry.npmjs.org/@aws-cdk/cloud-assembly-schema/-/cloud-assembly-schema-41.2.0.tgz#c1ef513e1cc0528dbc05948ae39d5631306af423"
80-
integrity sha512-JaulVS6z9y5+u4jNmoWbHZRs9uGOnmn/ktXygNWKNu1k6lF3ad4so3s18eRu15XCbUIomxN9WPYT6Ehh7hzONw==
77+
"@aws-cdk/cloud-assembly-schema@^43.6.0":
78+
version "43.9.0"
79+
resolved "https://registry.npmjs.org/@aws-cdk/cloud-assembly-schema/-/cloud-assembly-schema-43.9.0.tgz#795c491f703c0faa71b4c97a888086a54614b2d4"
80+
integrity sha512-S6fiiNGSRoTqCnpAH3mXV52Ssj6i+eUB6LSKYqGdKqzrgGkyvR/acurMGYtCJt5gsf3uZ+5J6utYs2X6dcg8Vw==
8181
dependencies:
8282
jsonschema "~1.4.1"
8383
semver "^7.7.1"

0 commit comments

Comments
 (0)