From efc8afdb496d3855d31dc33983d3bf16bf6d1734 Mon Sep 17 00:00:00 2001 From: John Shaskin Date: Fri, 18 Jan 2019 10:32:19 -0700 Subject: [PATCH] Add MethodResponse support for aws-apigateway Remove Dockerfile that was no longer needed Update python base image from 3.6 to 3.6.5 Make the dockerfile work Add MethodResponse to API Gateway Method. Add some documentation to the MethodResponse properties. Update the test for MethodResponse with response models. Fix some formatting and finish adding code documentation for MethodResponse. Remove Dockerfile from this branch --- .dockerignore | 1 - Dockerfile | 19 ------ packages/@aws-cdk/aws-apigateway/lib/index.ts | 1 + .../@aws-cdk/aws-apigateway/lib/method.ts | 10 ++- .../aws-apigateway/lib/methodresponse.ts | 21 +++++++ .../aws-apigateway/test/test.method.ts | 62 +++++++++++++++++++ 6 files changed, 92 insertions(+), 22 deletions(-) delete mode 100644 .dockerignore delete mode 100644 Dockerfile create mode 100644 packages/@aws-cdk/aws-apigateway/lib/methodresponse.ts diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index bea64c8e45ab3..0000000000000 --- a/.dockerignore +++ /dev/null @@ -1 +0,0 @@ -packages/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 2230ecdd54a98..0000000000000 --- a/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM python:3.7.2 -RUN apt-get update && \ - apt-get -y install make g++ rsync git zip vim && \ - curl -sL https://deb.nodesource.com/setup_8.x | bash - && \ - apt-get -y install nodejs && \ - npm install -g lerna -ENV NPM_CONFIG_PREFIX=/home/root/.npm-global - -# USER node -RUN mkdir -p /home/node/build -WORKDIR /home/node/build - -# Add lerna aliases -RUN echo "alias lr='lerna run --stream --scope \$(node -p \"require(\\\"./package.json\\\").name\")'" >> ~/.bashrc && \ - echo "alias lb='lr build'" >> ~/.bashrc && \ - echo "alias lt='lr test'" >> ~/.bashrc && \ - echo "alias lw='lr watch'" >> ~/.bashrc - -CMD ["bash"] \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigateway/lib/index.ts b/packages/@aws-cdk/aws-apigateway/lib/index.ts index 1ccd62b520805..dc6601f650122 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/index.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/index.ts @@ -7,6 +7,7 @@ export * from './stage'; export * from './integrations'; export * from './lambda-api'; export * from './vpc-link'; +export * from './methodresponse'; // AWS::ApiGateway CloudFormation Resources: export * from './apigateway.generated'; diff --git a/packages/@aws-cdk/aws-apigateway/lib/method.ts b/packages/@aws-cdk/aws-apigateway/lib/method.ts index cb263bc5083cc..21d3fb6f6bbf2 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/method.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/method.ts @@ -2,6 +2,7 @@ import cdk = require('@aws-cdk/cdk'); import { CfnMethod, CfnMethodProps } from './apigateway.generated'; import { ConnectionType, Integration } from './integration'; import { MockIntegration } from './integrations/mock'; +import { MethodResponse } from './methodresponse'; import { IRestApiResource } from './resource'; import { RestApi } from './restapi'; import { validateHttpMethod } from './util'; @@ -34,11 +35,15 @@ export interface MethodOptions { */ apiKeyRequired?: boolean; + /** + * The responses that can be sent to the client who calls the method. + */ + methodResponses?: MethodResponse[] + // TODO: // - RequestValidatorId // - RequestModels // - RequestParameters - // - MethodResponses requestParameters?: { [param: string]: boolean }; } @@ -93,7 +98,8 @@ export class Method extends cdk.Construct { authorizationType: options.authorizationType || defaultMethodOptions.authorizationType || AuthorizationType.None, authorizerId: options.authorizerId || defaultMethodOptions.authorizerId, requestParameters: options.requestParameters, - integration: this.renderIntegration(props.integration) + integration: this.renderIntegration(props.integration), + methodResponses: options.methodResponses || defaultMethodOptions.methodResponses }; const resource = new CfnMethod(this, 'Resource', methodProps); diff --git a/packages/@aws-cdk/aws-apigateway/lib/methodresponse.ts b/packages/@aws-cdk/aws-apigateway/lib/methodresponse.ts new file mode 100644 index 0000000000000..49f0453a3cc4d --- /dev/null +++ b/packages/@aws-cdk/aws-apigateway/lib/methodresponse.ts @@ -0,0 +1,21 @@ +export interface MethodResponse { + + /** + * The method response's status code, which you map to an IntegrationResponse. + */ + statusCode: string; + + /** + * Response parameters that API Gateway sends to the client that called a method. + * Specify response parameters as key-value pairs (string-to-Boolean maps), with + * a destination as the key and a Boolean as the value. Specify the destination + * using the following pattern: method.response.header.name, where the name is a + * valid, unique header name. The Boolean specifies whether a parameter is required. + */ + responseParameters?: { [destination: string]: boolean }; + + /** + * The method response's status code, which you map to an IntegrationResponse. + */ + responseModels?: { [destination: string]: string }; +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigateway/test/test.method.ts b/packages/@aws-cdk/aws-apigateway/test/test.method.ts index 645d1f9f9f398..efd94af768796 100644 --- a/packages/@aws-cdk/aws-apigateway/test/test.method.ts +++ b/packages/@aws-cdk/aws-apigateway/test/test.method.ts @@ -263,6 +263,11 @@ export = { const stack = new cdk.Stack(); const api = new apigateway.RestApi(stack, 'test-api', { deploy: false }); + // WHEN + // GIVEN + const stack = new cdk.Stack(); + const api = new apigateway.RestApi(stack, 'test-api', { deploy: false }); + // WHEN const integration = new apigateway.Integration({ type: apigateway.IntegrationType.HttpProxy, @@ -301,6 +306,63 @@ export = { // THEN test.throws(() => api.root.addMethod('GET', integration), /cannot set 'vpcLink' where 'connectionType' is INTERNET/); + test.done(); + }, + + 'methodResponse set one or more method responses via options'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const api = new apigateway.RestApi(stack, 'test-api', { deploy: false }); + + // WHEN + new apigateway.Method(stack, 'method-man', { + httpMethod: 'GET', + resource: api.root, + options: { + methodResponses: [{ + statusCode: '200' + }, { + statusCode: "400", + responseParameters: { + 'method.response.header.killerbees': false + } + }, { + statusCode: "500", + responseParameters: { + 'method.response.header.errthing': true + }, + responseModels: { + 'application/json': 'Empty', + 'text/plain': 'Empty' + } + } + ] + } + }); + + // THEN + expect(stack).to(haveResource('AWS::ApiGateway::Method', { + HttpMethod: 'GET', + MethodResponses: [{ + StatusCode: "200" + }, { + StatusCode: "400", + ResponseParameters: { + 'method.response.header.killerbees': false + } + }, { + StatusCode: "500", + ResponseParameters: { + 'method.response.header.errthing': true + }, + ResponseModels: { + 'application/json': 'Empty', + 'text/plain': 'Empty' + } + } + ] + })); + test.done(); } };