Skip to content

Commit 142bd0e

Browse files
nija-atmergify[bot]
authored andcommitted
fix(apigateway): allow multiple api keys to the same usage plan (#4903)
* fix(apigateway): allow multiple api keys to the same usage plan fixes #4860 * Updated to not change physical ids on existing UsagePlanKey * Changed to use uniqueId
1 parent a56f4cc commit 142bd0e

File tree

5 files changed

+108
-3
lines changed

5 files changed

+108
-3
lines changed

packages/@aws-cdk/aws-apigateway/lib/restapi.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export class RestApi extends Resource implements IRestApi {
301301
/**
302302
* Adds a usage plan.
303303
*/
304-
public addUsagePlan(id: string, props: UsagePlanProps): UsagePlan {
304+
public addUsagePlan(id: string, props: UsagePlanProps = {}): UsagePlan {
305305
return new UsagePlan(this, id, props);
306306
}
307307

packages/@aws-cdk/aws-apigateway/lib/usage-plan.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@ export class UsagePlan extends Resource {
178178
* @param apiKey
179179
*/
180180
public addApiKey(apiKey: IApiKey): void {
181-
new CfnUsagePlanKey(this, 'UsagePlanKeyResource', {
181+
const prefix = 'UsagePlanKeyResource';
182+
183+
// Postfixing apikey id only from the 2nd child, to keep physicalIds of UsagePlanKey for existing CDK apps unmodifed.
184+
const id = this.node.tryFindChild(prefix) ? `${prefix}:${apiKey.node.uniqueId}` : prefix;
185+
186+
new CfnUsagePlanKey(this, id, {
182187
keyId: apiKey.keyId,
183188
keyType: UsagePlanKeyType.API_KEY,
184189
usagePlanId: this.usagePlanId
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"Resources": {
3+
"myusageplan4B391740": {
4+
"Type": "AWS::ApiGateway::UsagePlan",
5+
"Properties": {}
6+
},
7+
"myusageplanUsagePlanKeyResource095B4EA9": {
8+
"Type": "AWS::ApiGateway::UsagePlanKey",
9+
"Properties": {
10+
"KeyId": {
11+
"Ref": "myapikey18B056ACE"
12+
},
13+
"KeyType": "API_KEY",
14+
"UsagePlanId": {
15+
"Ref": "myusageplan4B391740"
16+
}
17+
}
18+
},
19+
"myusageplanUsagePlanKeyResourcetestapigatewayusageplanmultikeymyapikey29D6460C6AE8DE59D": {
20+
"Type": "AWS::ApiGateway::UsagePlanKey",
21+
"Properties": {
22+
"KeyId": {
23+
"Ref": "myapikey250C8F11B"
24+
},
25+
"KeyType": "API_KEY",
26+
"UsagePlanId": {
27+
"Ref": "myusageplan4B391740"
28+
}
29+
}
30+
},
31+
"myapikey18B056ACE": {
32+
"Type": "AWS::ApiGateway::ApiKey",
33+
"Properties": {
34+
"Enabled": true
35+
}
36+
},
37+
"myapikey250C8F11B": {
38+
"Type": "AWS::ApiGateway::ApiKey",
39+
"Properties": {
40+
"Enabled": true
41+
}
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import cdk = require('@aws-cdk/core');
2+
import apigateway = require('../lib');
3+
4+
class Test extends cdk.Stack {
5+
constructor(scope: cdk.App, id: string) {
6+
super(scope, id);
7+
8+
const usageplan = new apigateway.UsagePlan(this, 'myusageplan');
9+
const apikey1 = new apigateway.ApiKey(this, 'myapikey1');
10+
const apikey2 = new apigateway.ApiKey(this, 'myapikey2');
11+
usageplan.addApiKey(apikey1);
12+
usageplan.addApiKey(apikey2);
13+
}
14+
}
15+
16+
const app = new cdk.App();
17+
18+
new Test(app, 'test-apigateway-usageplan-multikey');
19+
20+
app.synth();

packages/@aws-cdk/aws-apigateway/test/test.usage-plan.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,41 @@ export = {
129129
}, ResourcePart.Properties));
130130

131131
test.done();
132-
}
132+
},
133+
134+
'UsagePlan can have multiple keys'(test: Test) {
135+
// GIVEN
136+
const stack = new cdk.Stack();
137+
const usagePlan = new apigateway.UsagePlan(stack, 'my-usage-plan');
138+
const apiKey1 = new apigateway.ApiKey(stack, 'my-api-key-1', {
139+
apiKeyName: 'my-api-key-1'
140+
});
141+
const apiKey2 = new apigateway.ApiKey(stack, 'my-api-key-2', {
142+
apiKeyName: 'my-api-key-2'
143+
});
144+
145+
// WHEN
146+
usagePlan.addApiKey(apiKey1);
147+
usagePlan.addApiKey(apiKey2);
148+
149+
// THEN
150+
expect(stack).to(haveResource('AWS::ApiGateway::ApiKey', {
151+
Name: 'my-api-key-1'
152+
}, ResourcePart.Properties));
153+
expect(stack).to(haveResource('AWS::ApiGateway::ApiKey', {
154+
Name: 'my-api-key-2'
155+
}, ResourcePart.Properties));
156+
expect(stack).to(haveResource('AWS::ApiGateway::UsagePlanKey', {
157+
KeyId: {
158+
Ref: 'myapikey11F723FC7'
159+
}
160+
}, ResourcePart.Properties));
161+
expect(stack).to(haveResource('AWS::ApiGateway::UsagePlanKey', {
162+
KeyId: {
163+
Ref: 'myapikey2ABDEF012'
164+
}
165+
}, ResourcePart.Properties));
166+
167+
test.done();
168+
},
133169
};

0 commit comments

Comments
 (0)