Skip to content

Commit

Permalink
fix(apigateway): api key not supported for SpecRestApi (#11235)
Browse files Browse the repository at this point in the history
fix(apigateway): ApiKey not supported for SpecRestApi

closes #11079 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
ayush987goyal authored Nov 6, 2020
1 parent 9eff751 commit 52da8cb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
9 changes: 4 additions & 5 deletions packages/@aws-cdk/aws-apigateway/lib/api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IResource as IResourceBase, Resource, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnApiKey } from './apigateway.generated';
import { ResourceOptions } from './resource';
import { RestApi } from './restapi';
import { IRestApi } from './restapi';
import { QuotaSettings, ThrottleSettings, UsagePlan, UsagePlanPerApiStage } from './usage-plan';

/**
Expand Down Expand Up @@ -47,11 +47,10 @@ export interface ApiKeyOptions extends ResourceOptions {
*/
export interface ApiKeyProps extends ApiKeyOptions {
/**
* [disable-awslint:ref-via-interface]
* A list of resources this api key is associated with.
* @default none
*/
readonly resources?: RestApi[];
readonly resources?: IRestApi[];

/**
* An AWS Marketplace customer identifier to use when integrating with the AWS SaaS Marketplace.
Expand Down Expand Up @@ -183,12 +182,12 @@ export class ApiKey extends ApiKeyBase {
});
}

private renderStageKeys(resources: RestApi[] | undefined): CfnApiKey.StageKeyProperty[] | undefined {
private renderStageKeys(resources: IRestApi[] | undefined): CfnApiKey.StageKeyProperty[] | undefined {
if (!resources) {
return undefined;
}

return resources.map((resource: RestApi) => {
return resources.map((resource: IRestApi) => {
const restApi = resource;
const restApiId = restApi.restApiId;
const stageName = restApi.deploymentStage!.stageName.toString();
Expand Down
20 changes: 10 additions & 10 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@ export abstract class RestApiBase extends Resource implements IRestApi {
});
}

/**
* Add an ApiKey
*/
public addApiKey(id: string, options?: ApiKeyOptions): IApiKey {
return new ApiKey(this, id, {
resources: [this],
...options,
});
}

/**
* Returns the given named metric for this API
*/
Expand Down Expand Up @@ -706,16 +716,6 @@ export class RestApi extends RestApiBase {
return this.urlForPath();
}

/**
* Add an ApiKey
*/
public addApiKey(id: string, options?: ApiKeyOptions): IApiKey {
return new ApiKey(this, id, {
resources: [this],
...options,
});
}

/**
* Adds a new model.
*/
Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/restapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,34 @@ describe('restapi', () => {
},
});
});

test('addApiKey is supported', () => {
// GIVEN
const stack = new Stack();
const api = new apigw.SpecRestApi(stack, 'myapi', {
apiDefinition: apigw.ApiDefinition.fromInline({ foo: 'bar' }),
});
api.root.addMethod('OPTIONS');

// WHEN
api.addApiKey('myapikey', {
apiKeyName: 'myApiKey1',
value: '01234567890ABCDEFabcdef',
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
Enabled: true,
Name: 'myApiKey1',
StageKeys: [
{
RestApiId: { Ref: 'myapi162F20B8' },
StageName: { Ref: 'myapiDeploymentStageprod329F21FF' },
},
],
Value: '01234567890ABCDEFabcdef',
});
});
});

describe('Metrics', () => {
Expand Down

0 comments on commit 52da8cb

Please sign in to comment.