|
| 1 | +import { Construct, IResource as IResourceBase, Resource } from '@aws-cdk/cdk'; |
| 2 | +import { CfnApiKey } from './apigateway.generated'; |
| 3 | +import { ResourceOptions } from "./resource"; |
| 4 | +import { RestApi } from './restapi'; |
| 5 | + |
| 6 | +/** |
| 7 | + * API keys are alphanumeric string values that you distribute to |
| 8 | + * app developer customers to grant access to your API |
| 9 | + */ |
| 10 | +export interface ApiKeyAttributes { |
| 11 | + /** |
| 12 | + * The API key ID. |
| 13 | + * @attribute |
| 14 | + */ |
| 15 | + readonly keyId: string; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * API keys are alphanumeric string values that you distribute to |
| 20 | + * app developer customers to grant access to your API |
| 21 | + */ |
| 22 | +export interface IApiKey extends IResourceBase { |
| 23 | + /** |
| 24 | + * The API key ID. |
| 25 | + * @attribute |
| 26 | + */ |
| 27 | + readonly keyId: string; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * ApiKey Properties. |
| 32 | + */ |
| 33 | +export interface ApiKeyProps extends ResourceOptions { |
| 34 | + /** |
| 35 | + * A list of resources this api key is associated with. |
| 36 | + * @default none |
| 37 | + */ |
| 38 | + readonly resources?: RestApi[]; |
| 39 | + |
| 40 | + /** |
| 41 | + * An AWS Marketplace customer identifier to use when integrating with the AWS SaaS Marketplace. |
| 42 | + * @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-customerid |
| 43 | + * @default none |
| 44 | + */ |
| 45 | + readonly customerId?: string; |
| 46 | + |
| 47 | + /** |
| 48 | + * A description of the purpose of the API key. |
| 49 | + * @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-description |
| 50 | + * @default none |
| 51 | + */ |
| 52 | + readonly description?: string; |
| 53 | + |
| 54 | + /** |
| 55 | + * Indicates whether the API key can be used by clients. |
| 56 | + * @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-enabled |
| 57 | + * @default true |
| 58 | + */ |
| 59 | + readonly enabled?: boolean; |
| 60 | + |
| 61 | + /** |
| 62 | + * Specifies whether the key identifier is distinct from the created API key value. |
| 63 | + * @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-generatedistinctid |
| 64 | + * @default false |
| 65 | + */ |
| 66 | + readonly generateDistinctId?: boolean; |
| 67 | + |
| 68 | + /** |
| 69 | + * 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. |
| 70 | + * @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-name |
| 71 | + * @default automically generated name |
| 72 | + */ |
| 73 | + readonly name?: string; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * An API Gateway ApiKey. |
| 78 | + * |
| 79 | + * An ApiKey can be distributed to API clients that are executing requests |
| 80 | + * for Method resources that require an Api Key. |
| 81 | + */ |
| 82 | +export class ApiKey extends Resource implements IApiKey { |
| 83 | + public readonly keyId: string; |
| 84 | + |
| 85 | + constructor(scope: Construct, id: string, props: ApiKeyProps = { }) { |
| 86 | + super(scope, id); |
| 87 | + |
| 88 | + const resource = new CfnApiKey(this, 'Resource', { |
| 89 | + customerId: props.customerId, |
| 90 | + description: props.description, |
| 91 | + enabled: props.enabled || true, |
| 92 | + generateDistinctId: props.generateDistinctId, |
| 93 | + name: props.name, |
| 94 | + stageKeys: this.renderStageKeys(props.resources) |
| 95 | + }); |
| 96 | + |
| 97 | + this.keyId = resource.ref; |
| 98 | + } |
| 99 | + |
| 100 | + private renderStageKeys(resources: RestApi[] | undefined): CfnApiKey.StageKeyProperty[] | undefined { |
| 101 | + if (!resources) { |
| 102 | + return undefined; |
| 103 | + } |
| 104 | + |
| 105 | + return resources.map((resource: RestApi) => { |
| 106 | + const restApi = resource; |
| 107 | + const restApiId = restApi.restApiId; |
| 108 | + const stageName = restApi.deploymentStage!.stageName.toString(); |
| 109 | + return { restApiId, stageName }; |
| 110 | + }); |
| 111 | + } |
| 112 | +} |
0 commit comments