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

feat(apigatewayv2): Import existing WebSocketApi from attributes #18958

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2/README.md
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
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
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