-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apigateway): support for UsagePlan, ApiKey, UsagePlanKey (#2564)
Add support for UsagePlan, ApiKey, UsagePlanKey. Fixes #723.
- Loading branch information
1 parent
1a7d4db
commit 203f114
Showing
12 changed files
with
704 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { Construct, IResource as IResourceBase, Resource } from '@aws-cdk/cdk'; | ||
import { CfnApiKey } from './apigateway.generated'; | ||
import { ResourceOptions } from "./resource"; | ||
import { RestApi } from './restapi'; | ||
|
||
/** | ||
* API keys are alphanumeric string values that you distribute to | ||
* app developer customers to grant access to your API | ||
*/ | ||
export interface ApiKeyAttributes { | ||
/** | ||
* The API key ID. | ||
* @attribute | ||
*/ | ||
readonly keyId: string; | ||
} | ||
|
||
/** | ||
* API keys are alphanumeric string values that you distribute to | ||
* app developer customers to grant access to your API | ||
*/ | ||
export interface IApiKey extends IResourceBase { | ||
/** | ||
* The API key ID. | ||
* @attribute | ||
*/ | ||
readonly keyId: string; | ||
} | ||
|
||
/** | ||
* ApiKey Properties. | ||
*/ | ||
export interface ApiKeyProps extends ResourceOptions { | ||
/** | ||
* A list of resources this api key is associated with. | ||
* @default none | ||
*/ | ||
readonly resources?: RestApi[]; | ||
|
||
/** | ||
* An AWS Marketplace customer identifier to use when integrating with the AWS SaaS Marketplace. | ||
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-customerid | ||
* @default none | ||
*/ | ||
readonly customerId?: string; | ||
|
||
/** | ||
* A description of the purpose of the API key. | ||
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-description | ||
* @default none | ||
*/ | ||
readonly description?: string; | ||
|
||
/** | ||
* Indicates whether the API key can be used by clients. | ||
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-enabled | ||
* @default true | ||
*/ | ||
readonly enabled?: boolean; | ||
|
||
/** | ||
* Specifies whether the key identifier is distinct from the created API key value. | ||
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-generatedistinctid | ||
* @default false | ||
*/ | ||
readonly generateDistinctId?: boolean; | ||
|
||
/** | ||
* A name for the API key. If you don't specify a name, AWS CloudFormation generates a unique physical ID and uses that ID for the API key name. | ||
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-name | ||
* @default automically generated name | ||
*/ | ||
readonly name?: string; | ||
} | ||
|
||
/** | ||
* An API Gateway ApiKey. | ||
* | ||
* An ApiKey can be distributed to API clients that are executing requests | ||
* for Method resources that require an Api Key. | ||
*/ | ||
export class ApiKey extends Resource implements IApiKey { | ||
public readonly keyId: string; | ||
|
||
constructor(scope: Construct, id: string, props: ApiKeyProps = { }) { | ||
super(scope, id); | ||
|
||
const resource = new CfnApiKey(this, 'Resource', { | ||
customerId: props.customerId, | ||
description: props.description, | ||
enabled: props.enabled || true, | ||
generateDistinctId: props.generateDistinctId, | ||
name: props.name, | ||
stageKeys: this.renderStageKeys(props.resources) | ||
}); | ||
|
||
this.keyId = resource.ref; | ||
} | ||
|
||
private renderStageKeys(resources: RestApi[] | undefined): CfnApiKey.StageKeyProperty[] | undefined { | ||
if (!resources) { | ||
return undefined; | ||
} | ||
|
||
return resources.map((resource: RestApi) => { | ||
const restApi = resource; | ||
const restApiId = restApi.restApiId; | ||
const stageName = restApi.deploymentStage!.stageName.toString(); | ||
return { restApiId, stageName }; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.