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

fix(apigateway): changes to gateway response does not trigger auto deployment #11068

Merged
merged 3 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/@aws-cdk/aws-apigateway/lib/gateway-response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IResource, Resource } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnGatewayResponse } from './apigateway.generated';
import { CfnGatewayResponse, CfnGatewayResponseProps } from './apigateway.generated';
import { IRestApi } from './restapi';

/**
Expand Down Expand Up @@ -58,13 +58,25 @@ export class GatewayResponse extends Resource implements IGatewayResponse {
constructor(scope: Construct, id: string, props: GatewayResponseProps) {
super(scope, id);

const resource = new CfnGatewayResponse(this, 'Resource', {
const gatewayResponseProps: CfnGatewayResponseProps = {
restApiId: props.restApi.restApiId,
responseType: props.type.responseType,
responseParameters: this.buildResponseParameters(props.responseHeaders),
responseTemplates: props.templates,
statusCode: props.statusCode,
});
};

const resource = new CfnGatewayResponse(this, 'Resource', gatewayResponseProps);

const deployment = props.restApi.latestDeployment;
if (deployment) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there value in generalizing this?

deployment.node.addDependency(resource);
deployment.addToLogicalId({
gatewayResponse: {
...gatewayResponseProps,
},
});
}

this.node.defaultChild = resource;
}
Expand Down
114 changes: 114 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/gateway-response.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import '@aws-cdk/assert/jest';
import { ABSENT } from '@aws-cdk/assert';
import { Stack } from '@aws-cdk/core';
import { ResponseType, RestApi } from '../lib';

describe('gateway response', () => {
test('gateway response resource is created', () => {
// GIVEN
const stack = new Stack();

// WHEN
const api = new RestApi(stack, 'restapi', {
deploy: false,
cloudWatchRole: false,
});

api.root.addMethod('GET');
api.addGatewayResponse('test-response', {
type: ResponseType.ACCESS_DENIED,
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::GatewayResponse', {
ResponseType: 'ACCESS_DENIED',
RestApiId: stack.resolve(api.restApiId),
StatusCode: ABSENT,
ResponseParameters: ABSENT,
ResponseTemplates: ABSENT,
});
});

test('gateway response resource is created with parameters', () => {
// GIVEN
const stack = new Stack();

// WHEN
const api = new RestApi(stack, 'restapi', {
deploy: false,
cloudWatchRole: false,
});

api.root.addMethod('GET');
api.addGatewayResponse('test-response', {
type: ResponseType.AUTHORIZER_FAILURE,
statusCode: '500',
responseHeaders: {
'Access-Control-Allow-Origin': 'test.com',
'test-key': 'test-value',
},
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::GatewayResponse', {
ResponseType: 'AUTHORIZER_FAILURE',
RestApiId: stack.resolve(api.restApiId),
StatusCode: '500',
ResponseParameters: {
'gatewayresponse.header.Access-Control-Allow-Origin': 'test.com',
'gatewayresponse.header.test-key': 'test-value',
},
ResponseTemplates: ABSENT,
});
});

test('gateway response resource is created with templates', () => {
// GIVEN
const stack = new Stack();

// WHEN
const api = new RestApi(stack, 'restapi', {
deploy: false,
cloudWatchRole: false,
});

api.root.addMethod('GET');
api.addGatewayResponse('test-response', {
type: ResponseType.AUTHORIZER_FAILURE,
statusCode: '500',
templates: {
'application/json': '{ "message": $context.error.messageString, "statusCode": "488" }',
},
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::GatewayResponse', {
ResponseType: 'AUTHORIZER_FAILURE',
RestApiId: stack.resolve(api.restApiId),
StatusCode: '500',
ResponseParameters: ABSENT,
ResponseTemplates: {
'application/json': '{ "message": $context.error.messageString, "statusCode": "488" }',
},
});
});

test('deployment changes when gateway response is updated', () => {
// GIVEN
const stack = new Stack();
const restApi = new RestApi(stack, 'restapi', {
deploy: true,
});
const deploymentResource = restApi.latestDeployment!.node.defaultChild;
const logicalId = (deploymentResource as any).calculateLogicalId();

// WHEN
restApi.addGatewayResponse('gatewayResponse', {
type: ResponseType.AUTHORIZER_CONFIGURATION_ERROR,
});
const newLogicalId = (deploymentResource as any).calculateLogicalId();

// THEN
expect(newLogicalId).not.toEqual(logicalId);
});
});
91 changes: 1 addition & 90 deletions packages/@aws-cdk/aws-apigateway/test/restapi.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@aws-cdk/assert/jest';
import { ABSENT, ResourcePart, SynthUtils } from '@aws-cdk/assert';
import { ResourcePart, SynthUtils } from '@aws-cdk/assert';
import { GatewayVpcEndpoint } from '@aws-cdk/aws-ec2';
import { App, CfnElement, CfnResource, Stack } from '@aws-cdk/core';
import * as apigw from '../lib';
Expand Down Expand Up @@ -807,95 +807,6 @@ describe('restapi', () => {
});
});

test('gateway response resource is created', () => {
// GIVEN
const stack = new Stack();

// WHEN
const api = new apigw.RestApi(stack, 'restapi', {
deploy: false,
cloudWatchRole: false,
});

api.root.addMethod('GET');
api.addGatewayResponse('test-response', {
type: apigw.ResponseType.ACCESS_DENIED,
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::GatewayResponse', {
ResponseType: 'ACCESS_DENIED',
RestApiId: stack.resolve(api.restApiId),
StatusCode: ABSENT,
ResponseParameters: ABSENT,
ResponseTemplates: ABSENT,
});
});

test('gateway response resource is created with parameters', () => {
// GIVEN
const stack = new Stack();

// WHEN
const api = new apigw.RestApi(stack, 'restapi', {
deploy: false,
cloudWatchRole: false,
});

api.root.addMethod('GET');
api.addGatewayResponse('test-response', {
type: apigw.ResponseType.AUTHORIZER_FAILURE,
statusCode: '500',
responseHeaders: {
'Access-Control-Allow-Origin': 'test.com',
'test-key': 'test-value',
},
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::GatewayResponse', {
ResponseType: 'AUTHORIZER_FAILURE',
RestApiId: stack.resolve(api.restApiId),
StatusCode: '500',
ResponseParameters: {
'gatewayresponse.header.Access-Control-Allow-Origin': 'test.com',
'gatewayresponse.header.test-key': 'test-value',
},
ResponseTemplates: ABSENT,
});
});

test('gateway response resource is created with templates', () => {
// GIVEN
const stack = new Stack();

// WHEN
const api = new apigw.RestApi(stack, 'restapi', {
deploy: false,
cloudWatchRole: false,
});

api.root.addMethod('GET');
api.addGatewayResponse('test-response', {
type: apigw.ResponseType.AUTHORIZER_FAILURE,
statusCode: '500',
templates: {
'application/json': '{ "message": $context.error.messageString, "statusCode": "488" }',
},
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::GatewayResponse', {
ResponseType: 'AUTHORIZER_FAILURE',
RestApiId: stack.resolve(api.restApiId),
StatusCode: '500',
ResponseParameters: ABSENT,
ResponseTemplates: {
'application/json': '{ "message": $context.error.messageString, "statusCode": "488" }',
},
});
});

test('"restApi" and "api" properties return the RestApi correctly', () => {
// GIVEN
const stack = new Stack();
Expand Down