From f203845d26ae8333f467f1cb91ad965697087d85 Mon Sep 17 00:00:00 2001 From: vincent-turato <39069200+vincent-turato@users.noreply.github.com> Date: Wed, 23 Feb 2022 11:16:34 -0800 Subject: [PATCH] feat(apigatewayv2): Import existing WebSocketApi from attributes (#18958) Closes: https://github.com/aws/aws-cdk/issues/18755 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-apigatewayv2/README.md | 6 ++++ .../aws-apigatewayv2/lib/websocket/api.ts | 36 +++++++++++++++++++ .../test/websocket/api.test.ts | 19 ++++++++++ 3 files changed, 61 insertions(+) diff --git a/packages/@aws-cdk/aws-apigatewayv2/README.md b/packages/@aws-cdk/aws-apigatewayv2/README.md index 5e0a110082ab9..040ee20d0bf6a 100644 --- a/packages/@aws-cdk/aws-apigatewayv2/README.md +++ b/packages/@aws-cdk/aws-apigatewayv2/README.md @@ -404,6 +404,12 @@ webSocketApi.addRoute('sendmessage', { }); ``` +To import an existing WebSocketApi: + +```ts +const webSocketApi = apigwv2.WebSocketApi.fromWebSocketApiAttributes(this, 'mywsapi', { webSocketId: 'api-1234' }); +``` + ### Manage Connections Permission Grant permission to use API Gateway Management API of a WebSocket API by calling the `grantManageConnections` API. diff --git a/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts b/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts index da740d582bbad..f81eb7b899f1f 100644 --- a/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts +++ b/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts @@ -85,11 +85,47 @@ export interface WebSocketApiProps { readonly defaultRouteOptions?: WebSocketRouteOptions; } +/** + * Attributes for importing a WebSocketApi into the CDK + */ +export interface WebSocketApiAttributes { + /** + * The identifier of the WebSocketApi + */ + readonly webSocketId: string; + + /** + * The endpoint URL of the WebSocketApi + * @default - throw san error if apiEndpoint is accessed. + */ + readonly apiEndpoint?: string; +} + + /** * Create a new API Gateway WebSocket API endpoint. * @resource AWS::ApiGatewayV2::Api */ export class WebSocketApi extends ApiBase implements IWebSocketApi { + /** + * Import an existing WebSocket API into this CDK app. + */ + public static fromWebSocketApiAttributes(scope: Construct, id: string, attrs: WebSocketApiAttributes): IWebSocketApi { + class Import extends ApiBase { + public readonly apiId = attrs.webSocketId; + public readonly websocketApiId = attrs.webSocketId; + private readonly _apiEndpoint = attrs.apiEndpoint; + + public get apiEndpoint(): string { + if (!this._apiEndpoint) { + throw new Error('apiEndpoint is not configured on the imported WebSocketApi.'); + } + return this._apiEndpoint; + } + } + return new Import(scope, id); + } + public readonly apiId: string; public readonly apiEndpoint: string; diff --git a/packages/@aws-cdk/aws-apigatewayv2/test/websocket/api.test.ts b/packages/@aws-cdk/aws-apigatewayv2/test/websocket/api.test.ts index 1ac6cfbae315f..f9fb740c0e190 100644 --- a/packages/@aws-cdk/aws-apigatewayv2/test/websocket/api.test.ts +++ b/packages/@aws-cdk/aws-apigatewayv2/test/websocket/api.test.ts @@ -107,6 +107,25 @@ describe('WebSocketApi', () => { }); }); + test('import', () => { + // GIVEN + const stack = new Stack(); + const imported = WebSocketApi.fromWebSocketApiAttributes(stack, 'imported', { webSocketId: 'ws-1234', apiEndpoint: 'api-endpoint' }); + + // THEN + expect(imported.apiId).toEqual('ws-1234'); + expect(imported.apiEndpoint).toEqual('api-endpoint'); + }); + + test('apiEndpoint for imported', () => { + // GIVEN + const stack = new Stack(); + const api = WebSocketApi.fromWebSocketApiAttributes(stack, 'imported', { webSocketId: 'api-1234' }); + + // THEN + expect(() => api.apiEndpoint).toThrow(/apiEndpoint is not configured/); + }); + describe('grantManageConnections', () => { test('adds an IAM policy to the principal', () => { // GIVEN