Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apigateway): allow multiple api keys to the same usage plan #4903

Merged
merged 4 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export class RestApi extends Resource implements IRestApi {
/**
* Adds a usage plan.
*/
public addUsagePlan(id: string, props: UsagePlanProps): UsagePlan {
public addUsagePlan(id: string, props: UsagePlanProps = {}): UsagePlan {
return new UsagePlan(this, id, props);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-apigateway/lib/usage-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ export class UsagePlan extends Resource {
* @param apiKey
*/
public addApiKey(apiKey: IApiKey): void {
new CfnUsagePlanKey(this, 'UsagePlanKeyResource', {
const prefix = 'UsagePlanKeyResource';
const id = this.node.tryFindChild(prefix) ? `${prefix}:${apiKey.node.id}` : prefix;
nija-at marked this conversation as resolved.
Show resolved Hide resolved
new CfnUsagePlanKey(this, id, {
keyId: apiKey.keyId,
keyType: UsagePlanKeyType.API_KEY,
usagePlanId: this.usagePlanId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"Resources": {
"myusageplan4B391740": {
"Type": "AWS::ApiGateway::UsagePlan",
"Properties": {}
},
"myusageplanUsagePlanKeyResource095B4EA9": {
"Type": "AWS::ApiGateway::UsagePlanKey",
"Properties": {
"KeyId": {
"Ref": "myapikey18B056ACE"
},
"KeyType": "API_KEY",
"UsagePlanId": {
"Ref": "myusageplan4B391740"
}
}
},
"myusageplanUsagePlanKeyResourcemyapikey25756C43A": {
"Type": "AWS::ApiGateway::UsagePlanKey",
"Properties": {
"KeyId": {
"Ref": "myapikey250C8F11B"
},
"KeyType": "API_KEY",
"UsagePlanId": {
"Ref": "myusageplan4B391740"
}
}
},
"myapikey18B056ACE": {
"Type": "AWS::ApiGateway::ApiKey",
"Properties": {
"Enabled": true
}
},
"myapikey250C8F11B": {
"Type": "AWS::ApiGateway::ApiKey",
"Properties": {
"Enabled": true
}
}
}
}
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/integ.usage-plan.multikey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import cdk = require('@aws-cdk/core');
import apigateway = require('../lib');

class Test extends cdk.Stack {
constructor(scope: cdk.App, id: string) {
super(scope, id);

const usageplan = new apigateway.UsagePlan(this, 'myusageplan');
const apikey1 = new apigateway.ApiKey(this, 'myapikey1');
const apikey2 = new apigateway.ApiKey(this, 'myapikey2');
usageplan.addApiKey(apikey1);
usageplan.addApiKey(apikey2);
}
}

const app = new cdk.App();

new Test(app, 'test-apigateway-usageplan-multikey');

app.synth();
38 changes: 37 additions & 1 deletion packages/@aws-cdk/aws-apigateway/test/test.usage-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,41 @@ export = {
}, ResourcePart.Properties));

test.done();
}
},

'UsagePlan can have multiple keys'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const usagePlan = new apigateway.UsagePlan(stack, 'my-usage-plan');
const apiKey1 = new apigateway.ApiKey(stack, 'my-api-key-1', {
apiKeyName: 'my-api-key-1'
});
const apiKey2 = new apigateway.ApiKey(stack, 'my-api-key-2', {
apiKeyName: 'my-api-key-2'
});

// WHEN
usagePlan.addApiKey(apiKey1);
usagePlan.addApiKey(apiKey2);

// THEN
expect(stack).to(haveResource('AWS::ApiGateway::ApiKey', {
Name: 'my-api-key-1'
}, ResourcePart.Properties));
expect(stack).to(haveResource('AWS::ApiGateway::ApiKey', {
Name: 'my-api-key-2'
}, ResourcePart.Properties));
expect(stack).to(haveResource('AWS::ApiGateway::UsagePlanKey', {
KeyId: {
Ref: 'myapikey11F723FC7'
}
}, ResourcePart.Properties));
expect(stack).to(haveResource('AWS::ApiGateway::UsagePlanKey', {
KeyId: {
Ref: 'myapikey2ABDEF012'
}
}, ResourcePart.Properties));

test.done();
},
};