Skip to content

Commit

Permalink
chore: forward merge 'master' into 'v2-main' (#19121)
Browse files Browse the repository at this point in the history
Automated action from aws/cdk-ops
  • Loading branch information
mergify[bot] committed Feb 23, 2022
2 parents ef88939 + 351c36f commit c8e6ccc
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 24 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
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-eks/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ abstract class ClusterBase extends Resource implements ICluster {
if (!this._spotInterruptHandler) {
this._spotInterruptHandler = this.addHelmChart('spot-interrupt-handler', {
chart: 'aws-node-termination-handler',
version: '0.13.2',
version: '1.14.1',
repository: 'https://aws.github.io/eks-charts',
namespace: 'kube-system',
values: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2712,7 +2712,7 @@
},
"Release": "ksclustertestclusterchartspotinterrupthandlerf41ba997",
"Chart": "aws-node-termination-handler",
"Version": "0.13.2",
"Version": "1.14.1",
"Values": "{\"nodeSelector\":{\"lifecycle\":\"Ec2Spot\"}}",
"Namespace": "kube-system",
"Repository": "https://aws.github.io/eks-charts",
Expand Down
53 changes: 31 additions & 22 deletions packages/aws-cdk/lib/notices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,40 @@ export interface NoticeDataSource {
export class WebsiteNoticeDataSource implements NoticeDataSource {
fetch(): Promise<Notice[]> {
return new Promise((resolve) => {
https.get('https://cli.cdk.dev-tools.aws.dev/notices.json', res => {
if (res.statusCode === 200) {
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
try {
const data = JSON.parse(rawData).notices as Notice[];
resolve(data ?? []);
} catch (e) {
debug(`Failed to parse notices: ${e}`);
try {
const req = https.get('https://cli.cdk.dev-tools.aws.dev/notices.json', res => {
if (res.statusCode === 200) {
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
try {
const data = JSON.parse(rawData).notices as Notice[];
resolve(data ?? []);
} catch (e) {
debug(`Failed to parse notices: ${e}`);
resolve([]);
}
});
res.on('error', e => {
debug(`Failed to fetch notices: ${e}`);
resolve([]);
}
});
res.on('error', e => {
debug(`Failed to fetch notices: ${e}`);
});
} else {
debug(`Failed to fetch notices. Status code: ${res.statusCode}`);
resolve([]);
});
} else {
debug(`Failed to fetch notices. Status code: ${res.statusCode}`);
}
});
req.on('error', e => {
debug(`Error on request: ${e}`);
resolve([]);
}
});
});
} catch (e) {
debug(`HTTPS 'get' call threw an error: ${e}`);
resolve([]);
}
});
}
}
Expand Down
22 changes: 22 additions & 0 deletions packages/aws-cdk/test/notices.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as https from 'https';
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs-extra';
Expand Down Expand Up @@ -211,6 +212,27 @@ describe('cli notices', () => {
expect(result).toEqual([]);
});

test('returns empty array when HTTPS call throws', async () => {
const mockGet = jest.spyOn(https, 'get')
.mockImplementation(() => { throw new Error('No connection'); });

const result = await dataSource.fetch();

expect(result).toEqual([]);

mockGet.mockRestore();
});

test('returns empty array when the request has an error', async () => {
nock('https://cli.cdk.dev-tools.aws.dev')
.get('/notices.json')
.replyWithError('DNS resolution failed');

const result = await dataSource.fetch();

expect(result).toEqual([]);
});

function mockCall(statusCode: number, body: any): Promise<Notice[]> {
nock('https://cli.cdk.dev-tools.aws.dev')
.get('/notices.json')
Expand Down

0 comments on commit c8e6ccc

Please sign in to comment.