Skip to content

Commit

Permalink
feat(apigatewayv2): Import existing WebSocketApi from attributes (#18958
Browse files Browse the repository at this point in the history
)

Closes: #18755

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
vincent-turato authored Feb 23, 2022
1 parent d7b220e commit f203845
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2/test/websocket/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f203845

Please sign in to comment.