Skip to content

Commit

Permalink
Add optional integration response properties to the apigateway-based …
Browse files Browse the repository at this point in the history
…constructs.
  • Loading branch information
georgebearden committed Feb 1, 2023
1 parent 2349d37 commit e687a2c
Show file tree
Hide file tree
Showing 17 changed files with 2,304 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,19 @@ new ApiGatewayToDynamoDB(this, "test-api-gateway-dynamodb-default", new ApiGatew
|allowCreateOperation?|`boolean`|Whether to deploy an API Gateway Method for POST HTTP operations on the DynamoDB table (i.e. dynamodb:PutItem).|
|createRequestTemplate?|`string`|API Gateway Request Template for the create method for the default `application/json` content-type. This property is required if the `allowCreateOperation` property is set to true.|
|additionalCreateRequestTemplates?|`{ [contentType: string]: string; }`|Optional Create Request Templates for content-types other than `application/json`. Use the `createRequestTemplate` property to set the request template for the `application/json` content-type.|
|createIntegrationResponses?|[`api.IntegrationResponses[]`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html)|Optional, custom API Gateway Integration Response for the create method, if the `allowCreateOperation` property is set to true.|
|allowReadOperation?|`boolean`|Whether to deploy an API Gateway Method for GET HTTP operations on DynamoDB table (i.e. dynamodb:Query).|
|readRequestTemplate?|`string`|API Gateway Request Template for the read method for the default `application/json` content-type, if the `allowReadOperation` property is set to true. The default template only supports a partition key and not partition + sort keys.|
|additionalReadRequestTemplates?|`{ [contentType: string]: string; }`|Optional Read Request Templates for content-types other than `application/json`. Use the `readRequestTemplate` property to set the request template for the `application/json` content-type.|
|readIntegrationResponses?|[`api.IntegrationResponses[]`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html)|Optional, custom API Gateway Integration Response for the read method, if the `allowReadOperation` property is set to true.|
|allowUpdateOperation?|`boolean`|Whether to deploy API Gateway Method for PUT HTTP operations on DynamoDB table (i.e. dynamodb:UpdateItem).|
|updateRequestTemplate?|`string`|API Gateway Request Template for the update method, required if the `allowUpdateOperation` property is set to true.|
|additionalUpdateRequestTemplates?|`{ [contentType: string]: string; }`|Optional Update Request Templates for content-types other than `application/json`. Use the `updateRequestTemplate` property to set the request template for the `application/json` content-type.|
|updateIntegrationResponses?|[`api.IntegrationResponses[]`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html)|Optional, custom API Gateway Integration Response for the update method, if the `allowUpdateOperation` property is set to true.|
|allowDeleteOperation?|`boolean`|Whether to deploy API Gateway Method for DELETE HTTP operations on DynamoDB table (i.e. dynamodb:DeleteItem).|
|deleteRequestTemplate?|`string`|API Gateway Request Template for the delete method for the default `application/json` content-type, if the `allowDeleteOperation` property is set to true.|
|additionalDeleteRequestTemplates?|`{ [contentType: string]: string; }`|Optional Delete request templates for content-types other than `application/json`. Use the `deleteRequestTemplate` property to set the request template for the `application/json` content-type.|

|deleteIntegrationResponses?|[`api.IntegrationResponses[]`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html)|Optional, custom API Gateway Integration Response for the delete method, if the `allowDeleteOperation` property is set to true.|
|logGroupProps?|[`logs.LogGroupProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_logs.LogGroupProps.html)|User provided props to override the default props for for the CloudWatchLogs LogGroup.|

## Pattern Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as iam from 'aws-cdk-lib/aws-iam';
import * as defaults from '@aws-solutions-constructs/core';
// Note: To ensure CDKv2 compatibility, keep the import statement for Construct separate
import { Construct } from 'constructs';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import { getPartitionKeyNameFromTable } from '@aws-solutions-constructs/core';
import * as logs from 'aws-cdk-lib/aws-logs';
Expand Down Expand Up @@ -62,6 +63,12 @@ export interface ApiGatewayToDynamoDBProps {
* @default - None
*/
readonly additionalCreateRequestTemplates?: { [contentType: string]: string; };
/**
* Optional, custom API Gateway Integration Response for the create method, if the `allowCreateOperation` property is set to true.
*
* @default - [{statusCode:"200"},{statusCode:"500",responseTemplates:{"text/html":"Error"},selectionPattern:"500"}]
*/
readonly createIntegrationResponses?: apigateway.IntegrationResponse[];
/**
* Whether to deploy an API Gateway Method for GET HTTP operations on DynamoDB table (i.e. dynamodb:Query).
*
Expand Down Expand Up @@ -92,6 +99,12 @@ export interface ApiGatewayToDynamoDBProps {
* @default - None
*/
readonly additionalReadRequestTemplates?: { [contentType: string]: string; };
/**
* Optional, custom API Gateway Integration Response for the read method, if the `allowReadOperation` property is set to true.
*
* @default - [{statusCode:"200"},{statusCode:"500",responseTemplates:{"text/html":"Error"},selectionPattern:"500"}]
*/
readonly readIntegrationResponses?: apigateway.IntegrationResponse[];
/**
* Whether to deploy API Gateway Method for PUT HTTP operations on DynamoDB table (i.e. dynamodb:UpdateItem).
*
Expand All @@ -111,6 +124,12 @@ export interface ApiGatewayToDynamoDBProps {
* @default - None
*/
readonly additionalUpdateRequestTemplates?: { [contentType: string]: string; };
/**
* Optional, custom API Gateway Integration Response for the update method, if the `allowUpdateOperation` property is set to true.
*
* @default - [{statusCode:"200"},{statusCode:"500",responseTemplates:{"text/html":"Error"},selectionPattern:"500"}]
*/
readonly updateIntegrationResponses?: apigateway.IntegrationResponse[];
/**
* Whether to deploy API Gateway Method for DELETE HTTP operations on DynamoDB table (i.e. dynamodb:DeleteItem).
*
Expand Down Expand Up @@ -139,6 +158,12 @@ export interface ApiGatewayToDynamoDBProps {
* @default - None
*/
readonly additionalDeleteRequestTemplates?: { [contentType: string]: string; };
/**
* Optional, custom API Gateway Integration Response for the delete method, if the `allowDeleteOperation` property is set to true.
*
* @default - [{statusCode:"200"},{statusCode:"500",responseTemplates:{"text/html":"Error"},selectionPattern:"500"}]
*/
readonly deleteIntegrationResponses?: apigateway.IntegrationResponse[];
/**
* User provided props to override the default props for the CloudWatchLogs LogGroup.
*
Expand Down Expand Up @@ -208,7 +233,8 @@ export class ApiGatewayToDynamoDB extends Construct {
apiMethod: "POST",
apiResource: this.apiGateway.root,
requestTemplate: createRequestTemplate,
additionalRequestTemplates: props.additionalCreateRequestTemplates
additionalRequestTemplates: props.additionalCreateRequestTemplates,
integrationResponses: props.createIntegrationResponses
});
}
// Read
Expand All @@ -232,7 +258,8 @@ export class ApiGatewayToDynamoDB extends Construct {
apiMethod: "GET",
apiResource: apiGatewayResource,
requestTemplate: readRequestTemplate,
additionalRequestTemplates: props.additionalReadRequestTemplates
additionalRequestTemplates: props.additionalReadRequestTemplates,
integrationResponses: props.readIntegrationResponses
});
}
// Update
Expand All @@ -246,7 +273,8 @@ export class ApiGatewayToDynamoDB extends Construct {
apiMethod: "PUT",
apiResource: apiGatewayResource,
requestTemplate: updateRequestTemplate,
additionalRequestTemplates: props.additionalUpdateRequestTemplates
additionalRequestTemplates: props.additionalUpdateRequestTemplates,
integrationResponses: props.updateIntegrationResponses
});
}
// Delete
Expand All @@ -270,7 +298,8 @@ export class ApiGatewayToDynamoDB extends Construct {
apiMethod: "DELETE",
apiResource: apiGatewayResource,
requestTemplate: deleteRequestTemplate,
additionalRequestTemplates: props.additionalDeleteRequestTemplates
additionalRequestTemplates: props.additionalDeleteRequestTemplates,
integrationResponses: props.deleteIntegrationResponses
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,206 @@ test('Construct accepts additional delete request templates', () => {
}
}
});
});

test('Construct uses default integration responses', () => {
const stack = new Stack();
new ApiGatewayToDynamoDB(stack, 'api-gateway-dynamodb', {
allowCreateOperation: true,
allowReadOperation: true,
allowUpdateOperation: true,
allowDeleteOperation: true,
createRequestTemplate: 'create',
updateRequestTemplate: 'update'
});

expect(stack).toHaveResourceLike('AWS::ApiGateway::Method', {
HttpMethod: 'POST',
Integration: {
IntegrationResponses: [
{
StatusCode: '200'
},
{
ResponseTemplates: {
'text/html': 'Error'
},
SelectionPattern: '500',
StatusCode: '500'
}
]
}
});

expect(stack).toHaveResourceLike('AWS::ApiGateway::Method', {
HttpMethod: 'GET',
Integration: {
IntegrationResponses: [
{
StatusCode: '200'
},
{
ResponseTemplates: {
'text/html': 'Error'
},
SelectionPattern: '500',
StatusCode: '500'
}
]
}
});

expect(stack).toHaveResourceLike('AWS::ApiGateway::Method', {
HttpMethod: 'PUT',
Integration: {
IntegrationResponses: [
{
StatusCode: '200'
},
{
ResponseTemplates: {
'text/html': 'Error'
},
SelectionPattern: '500',
StatusCode: '500'
}
]
}
});

expect(stack).toHaveResourceLike('AWS::ApiGateway::Method', {
HttpMethod: 'DELETE',
Integration: {
IntegrationResponses: [
{
StatusCode: '200'
},
{
ResponseTemplates: {
'text/html': 'Error'
},
SelectionPattern: '500',
StatusCode: '500'
}
]
}
});
});

test('Construct uses custom createIntegrationResponses property', () => {
const stack = new Stack();
new ApiGatewayToDynamoDB(stack, 'api-gateway-dynamodb', {
allowCreateOperation: true,
createRequestTemplate: 'OK',
createIntegrationResponses: [
{
statusCode: '200',
responseTemplates: {
'text/html': 'OK'
}
}
]
});

expect(stack).toHaveResourceLike('AWS::ApiGateway::Method', {
HttpMethod: 'POST',
Integration: {
IntegrationResponses: [
{
ResponseTemplates: {
'text/html': 'OK'
},
StatusCode: '200'
}
]
}
});
});

test('Construct uses custom readIntegrationResponses property', () => {
const stack = new Stack();
new ApiGatewayToDynamoDB(stack, 'api-gateway-dynamodb', {
allowReadOperation: true,
readIntegrationResponses: [
{
statusCode: '200',
responseTemplates: {
'text/html': 'OK'
}
}
]
});

expect(stack).toHaveResourceLike('AWS::ApiGateway::Method', {
HttpMethod: 'GET',
Integration: {
IntegrationResponses: [
{
ResponseTemplates: {
'text/html': 'OK'
},
StatusCode: '200'
}
]
}
});
});

test('Construct uses custom updateIntegrationResponses property', () => {
const stack = new Stack();
new ApiGatewayToDynamoDB(stack, 'api-gateway-dynamodb', {
allowUpdateOperation: true,
updateRequestTemplate: 'OK',
updateIntegrationResponses: [
{
statusCode: '200',
responseTemplates: {
'text/html': 'OK'
}
}
]
});

expect(stack).toHaveResourceLike('AWS::ApiGateway::Method', {
HttpMethod: 'PUT',
Integration: {
IntegrationResponses: [
{
ResponseTemplates: {
'text/html': 'OK'
},
StatusCode: '200'
}
]
}
});
});

test('Construct uses custom deleteIntegrationResponses property', () => {
const stack = new Stack();
new ApiGatewayToDynamoDB(stack, 'api-gateway-dynamodb', {
allowDeleteOperation: true,
deleteIntegrationResponses: [
{
statusCode: '200',
responseTemplates: {
'text/html': 'OK'
}
}
]
});

expect(stack).toHaveResourceLike('AWS::ApiGateway::Method', {
HttpMethod: 'DELETE',
Integration: {
IntegrationResponses: [
{
ResponseTemplates: {
'text/html': 'OK'
},
StatusCode: '200'
}
]
}
});
});
Loading

0 comments on commit e687a2c

Please sign in to comment.