Skip to content

Commit

Permalink
feat(apigateway): accept MethodResponses along with IntegrationRespon…
Browse files Browse the repository at this point in the history
…ses (#1146)

* Add MethodResponses Code Complete

* Second Commit

* Update Docs
  • Loading branch information
biffgaut committed Jul 11, 2024
1 parent c8b79a7 commit c351953
Show file tree
Hide file tree
Showing 56 changed files with 6,038 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,22 @@ new ApiGatewayToDynamoDB(this, "test-api-gateway-dynamodb-default", new ApiGatew
|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. This property can only be specified if the `allowCreateOperation` property is set to true.|
|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. This property can only be specified if the `allowCreateOperation` property is set to true.|
|createMethodResponses?|[`api.MethodResponses[]`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html)| Optional, custom API Gateway Method Responses for the create action. default: [{ statusCode: "200", responseParameters: { "method.response.header.Content-Type": true }},{ statusCode: "500", responseParameters: { "method.response.header.Content-Type": 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. 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.|
|allowUpdateOperation?|`boolean`|Whether to deploy API Gateway Method for PUT HTTP operations on DynamoDB table (i.e. dynamodb:UpdateItem).|
|readMethodResponses?|[`api.MethodResponses[]`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html)| Optional, custom API Gateway Method Responses for the read action. default: [{ statusCode: "200", responseParameters: { "method.response.header.Content-Type": true }},{ statusCode: "500", responseParameters: { "method.response.header.Content-Type": true } } ]|
|updateRequestTemplate?|`string`|API Gateway Request Template for the update method. This property is 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. This property can only be specified if the `allowUpdateOperation` property is set to true.|
|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. This property can only be specified if the `allowUpdateOperation` property is set to true.|
|updateMethodResponses?|[`api.MethodResponses[]`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html)| Optional, custom API Gateway Method Responses for the update action. default: [{ statusCode: "200", responseParameters: { "method.response.header.Content-Type": true }},{ statusCode: "500", responseParameters: { "method.response.header.Content-Type": 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. |
|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. This property can only be specified if the `allowDeleteOperation` property is set to true.|
|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. This property can only be specified if the `allowDeleteOperation` property is set to true.|
|deleteMethodResponses?|[`api.MethodResponses[]`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html)| Optional, custom API Gateway Method Responses for the delete action. default: [{ statusCode: "200", responseParameters: { "method.response.header.Content-Type": true }},{ statusCode: "500", responseParameters: { "method.response.header.Content-Type": 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 @@ -77,6 +77,25 @@ export interface ApiGatewayToDynamoDBProps {
* @default - [{statusCode:"200"},{statusCode:"500",responseTemplates:{"text/html":"Error"},selectionPattern:"500"}]
*/
readonly createIntegrationResponses?: api.IntegrationResponse[];
/**
* Optional, custom API Gateway Method Responses for the create action.
*
* @default - [
* {
* statusCode: "200",
* responseParameters: {
* "method.response.header.Content-Type": true
* }
* },
* {
* statusCode: "500",
* responseParameters: {
* "method.response.header.Content-Type": true
* },
* }
* ]
*/
readonly createMethodResponses?: api.MethodResponse[];
/**
* Whether to deploy an API Gateway Method for GET HTTP operations on DynamoDB table (i.e. dynamodb:Query).
*
Expand Down Expand Up @@ -112,6 +131,25 @@ export interface ApiGatewayToDynamoDBProps {
* @default - [{statusCode:"200"},{statusCode:"500",responseTemplates:{"text/html":"Error"},selectionPattern:"500"}]
*/
readonly readIntegrationResponses?: api.IntegrationResponse[];
/**
* Optional, custom API Gateway Method Responses for the read action.
*
* @default - [
* {
* statusCode: "200",
* responseParameters: {
* "method.response.header.Content-Type": true
* }
* },
* {
* statusCode: "500",
* responseParameters: {
* "method.response.header.Content-Type": true
* },
* }
* ]
*/
readonly readMethodResponses?: api.MethodResponse[];
/**
* Whether to deploy API Gateway Method for PUT HTTP operations on DynamoDB table (i.e. dynamodb:UpdateItem).
*
Expand Down Expand Up @@ -140,6 +178,25 @@ export interface ApiGatewayToDynamoDBProps {
* @default - [{statusCode:"200"},{statusCode:"500",responseTemplates:{"text/html":"Error"},selectionPattern:"500"}]
*/
readonly updateIntegrationResponses?: api.IntegrationResponse[];
/**
* Optional, custom API Gateway Method Responses for the update action.
*
* @default - [
* {
* statusCode: "200",
* responseParameters: {
* "method.response.header.Content-Type": true
* }
* },
* {
* statusCode: "500",
* responseParameters: {
* "method.response.header.Content-Type": true
* },
* }
* ]
*/
readonly updateMethodResponses?: api.MethodResponse[];
/**
* Whether to deploy API Gateway Method for DELETE HTTP operations on DynamoDB table (i.e. dynamodb:DeleteItem).
*
Expand Down Expand Up @@ -176,6 +233,25 @@ export interface ApiGatewayToDynamoDBProps {
* @default - [{statusCode:"200"},{statusCode:"500",responseTemplates:{"text/html":"Error"},selectionPattern:"500"}]
*/
readonly deleteIntegrationResponses?: api.IntegrationResponse[];
/**
* Optional, custom API Gateway Method Responses for the delete action.
*
* @default - [
* {
* statusCode: "200",
* responseParameters: {
* "method.response.header.Content-Type": true
* }
* },
* {
* statusCode: "500",
* responseParameters: {
* "method.response.header.Content-Type": true
* },
* }
* ]
*/
readonly deleteMethodResponses?: api.MethodResponse[];
/**
* User provided props to override the default props for the CloudWatchLogs LogGroup.
*
Expand Down Expand Up @@ -261,6 +337,7 @@ export class ApiGatewayToDynamoDB extends Construct {
// ImplementCreateOperation has confirmed that createRequestTemplate exists)
const createRequestTemplate = props.createRequestTemplate!.replace("${Table}", this.dynamoTable.tableName);
this.addActionToPolicy("dynamodb:PutItem");
const createMethodOptions: api.MethodOptions = props.createMethodResponses ? { methodResponses: props.createMethodResponses } : {};
defaults.addProxyMethodToApiResource({
service: "dynamodb",
action: "PutItem",
Expand All @@ -269,7 +346,8 @@ export class ApiGatewayToDynamoDB extends Construct {
apiResource: this.apiGateway.root,
requestTemplate: createRequestTemplate,
additionalRequestTemplates: props.additionalCreateRequestTemplates,
integrationResponses: props.createIntegrationResponses
integrationResponses: props.createIntegrationResponses,
methodOptions: createMethodOptions
});
}
// Read
Expand All @@ -286,6 +364,7 @@ export class ApiGatewayToDynamoDB extends Construct {
}`;

this.addActionToPolicy("dynamodb:Query");
const readMethodOptions: api.MethodOptions = props.readMethodResponses ? { methodResponses: props.readMethodResponses } : {};
defaults.addProxyMethodToApiResource({
service: "dynamodb",
action: "Query",
Expand All @@ -294,14 +373,16 @@ export class ApiGatewayToDynamoDB extends Construct {
apiResource: apiGatewayResource,
requestTemplate: readRequestTemplate,
additionalRequestTemplates: props.additionalReadRequestTemplates,
integrationResponses: props.readIntegrationResponses
integrationResponses: props.readIntegrationResponses,
methodOptions: readMethodOptions
});
}
// Update
if (this.ImplementUpdateOperation(props)) {
// ImplementUpdateOperation confirmed the existence of updateRequestTemplate
const updateRequestTemplate = props.updateRequestTemplate!.replace("${Table}", this.dynamoTable.tableName);
this.addActionToPolicy("dynamodb:UpdateItem");
const updateMethodOptions: api.MethodOptions = props.updateMethodResponses ? { methodResponses: props.updateMethodResponses } : {};
defaults.addProxyMethodToApiResource({
service: "dynamodb",
action: "UpdateItem",
Expand All @@ -310,7 +391,8 @@ export class ApiGatewayToDynamoDB extends Construct {
apiResource: apiGatewayResource,
requestTemplate: updateRequestTemplate,
additionalRequestTemplates: props.additionalUpdateRequestTemplates,
integrationResponses: props.updateIntegrationResponses
integrationResponses: props.updateIntegrationResponses,
methodOptions: updateMethodOptions
});
}
// Delete
Expand All @@ -327,6 +409,7 @@ export class ApiGatewayToDynamoDB extends Construct {
}`;

this.addActionToPolicy("dynamodb:DeleteItem");
const deleteMethodOptions: api.MethodOptions = props.deleteMethodResponses ? { methodResponses: props.deleteMethodResponses } : {};
defaults.addProxyMethodToApiResource({
service: "dynamodb",
action: "DeleteItem",
Expand All @@ -335,7 +418,8 @@ export class ApiGatewayToDynamoDB extends Construct {
apiResource: apiGatewayResource,
requestTemplate: deleteRequestTemplate,
additionalRequestTemplates: props.additionalDeleteRequestTemplates,
integrationResponses: props.deleteIntegrationResponses
integrationResponses: props.deleteIntegrationResponses,
methodOptions: deleteMethodOptions
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,112 @@ test('Test that CheckDynamoDBProps is getting called', () => {
// Assertion
expect(app).toThrowError(/Error - Either provide existingTableObj or dynamoTableProps, but not both.\n/);
});

test("provide createMethodResponses", () => {
const stack = new Stack();
const apiGatewayToDynamoDBProps: ApiGatewayToDynamoDBProps = {
allowCreateOperation: true,
createRequestTemplate: "fingerprint",
createMethodResponses: [{
statusCode: "100"
}]
};
new ApiGatewayToDynamoDB(stack, "test-api-gateway-dynamodb", apiGatewayToDynamoDBProps);

const template = Template.fromStack(stack);
// Checking for fingerprint ensures we're looking at the correct Method
template.hasResourceProperties("AWS::ApiGateway::Method", {
HttpMethod: "POST",
Integration: {
RequestTemplates: {
"application/json": "fingerprint"
}
},
MethodResponses: [
{
StatusCode: "100"
}
]
});
});

test("provide readMethodResponses", () => {
const stack = new Stack();
const apiGatewayToDynamoDBProps: ApiGatewayToDynamoDBProps = {
allowReadOperation: true,
readRequestTemplate: "fingerprint",
readMethodResponses: [{
statusCode: "100"
}]
};
new ApiGatewayToDynamoDB(stack, "test-api-gateway-dynamodb", apiGatewayToDynamoDBProps);

const template = Template.fromStack(stack);
template.hasResourceProperties("AWS::ApiGateway::Method", {
HttpMethod: "GET",
Integration: {
RequestTemplates: {
"application/json": "fingerprint"
}
},
MethodResponses: [
{
StatusCode: "100"
}
]
});
});

test("provide updateMethodResponses", () => {
const stack = new Stack();
const apiGatewayToDynamoDBProps: ApiGatewayToDynamoDBProps = {
allowUpdateOperation: true,
updateRequestTemplate: "fingerprint",
updateMethodResponses: [{
statusCode: "100"
}]
};
new ApiGatewayToDynamoDB(stack, "test-api-gateway-dynamodb", apiGatewayToDynamoDBProps);

const template = Template.fromStack(stack);
template.hasResourceProperties("AWS::ApiGateway::Method", {
HttpMethod: "PUT",
Integration: {
RequestTemplates: {
"application/json": "fingerprint"
}
},
MethodResponses: [
{
StatusCode: "100"
}
]
});
});

test("provide deleteMethodResponses", () => {
const stack = new Stack();
const apiGatewayToDynamoDBProps: ApiGatewayToDynamoDBProps = {
allowDeleteOperation: true,
deleteRequestTemplate: "fingerprint",
deleteMethodResponses: [{
statusCode: "100"
}]
};
new ApiGatewayToDynamoDB(stack, "test-api-gateway-dynamodb", apiGatewayToDynamoDBProps);

const template = Template.fromStack(stack);
template.hasResourceProperties("AWS::ApiGateway::Method", {
HttpMethod: "DELETE",
Integration: {
RequestTemplates: {
"application/json": "fingerprint"
}
},
MethodResponses: [
{
StatusCode: "100"
}
]
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "36.0.0",
"files": {
"7913a7050e5c393d80027306c7e295d8910401b542b654afce863313ba52d253": {
"source": {
"path": "apiddb-custom-method-response.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "7913a7050e5c393d80027306c7e295d8910401b542b654afce863313ba52d253.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
}
},
"dockerImages": {}
}
Loading

0 comments on commit c351953

Please sign in to comment.