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): api key not supported for SpecRestApi #11235

Merged
merged 2 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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