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

feat(aws-apigateway-dynamodb) added read and delete request template props #347

Merged
merged 4 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .viperlightignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ source/use_cases/aws-serverless-web-app/test/integ.backend-deployment.expected.j
source/patterns/@aws-solutions-constructs/aws-apigateway-lambda/test/integ.deployFunction.expected.json:204
source/patterns/@aws-solutions-constructs/aws-apigateway-lambda/test/integ.existingFunction.expected.json:204
source/patterns/@aws-solutions-constructs/aws-cloudfront-apigateway/test/integ.no-arguments.expected.json:204
source/patterns/@aws-solutions-constructs/aws-apigateway-dynamodb/test/integ.apigateway-dynamodb-existing-table.expected.json:60
CODE_OF_CONDUCT.md:4
CONTRIBUTING.md:238
source/patterns/@aws-solutions-constructs/core/test/step-function-helper.test.ts:114
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ _Parameters_

| **Name** | **Type** | **Description** |
|:-------------|:----------------|-----------------|
|dynamoTableProps|[`dynamodb.TableProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.TableProps.html)|Optional user provided props to override the default props for DynamoDB Table|
|dynamoTableProps?|[`dynamodb.TableProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.TableProps.html)|Optional user provided props to override the default props for DynamoDB Table.|
|existingTableObj?|[`dynamodb.Table`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.Table.html)|Existing instance of DynamoDB table object, providing both this and `dynamoTableProps` will cause an error.|
|apiGatewayProps?|[`api.RestApiProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.RestApiProps.html)|Optional user-provided props to override the default props for the API Gateway.|
|allowCreateOperation|`boolean`|Whether to deploy API Gateway Method for Create operation on DynamoDB table.|
|createRequestTemplate|`string`|API Gateway Request template for Create method, required if allowCreateOperation set to true|
|allowReadOperation|`boolean`|Whether to deploy API Gateway Method for Read operation on DynamoDB table.|
|allowUpdateOperation|`boolean`|Whether to deploy API Gateway Method for Update operation on DynamoDB table.|
|updateRequestTemplate|`string`|API Gateway Request template for Update method, required if allowUpdateOperation set to true|
|allowDeleteOperation|`boolean`|Whether to deploy API Gateway Method for Delete operation on DynamoDB table.|
|allowCreateOperation?|`boolean`|Whether to deploy API Gateway Method for Create operation on DynamoDB table.|
|createRequestTemplate?|`string`|API Gateway Request template for Create method, required if `allowCreateOperation` set to true.|
|allowReadOperation?|`boolean`|Whether to deploy API Gateway Method for Read operation on DynamoDB table.|
|readRequestTemplate?|`string`|Optional API Gateway Request template for Read method, it will use the default template if `allowReadOperation` is true and `readRequestTemplate` is not provided. The default template only supports a partition key and not partition + sort keys.|
|allowUpdateOperation?|`boolean`|Whether to deploy API Gateway Method for Update operation on DynamoDB table.|
|updateRequestTemplate?|`string`|API Gateway Request template for Update method, required if `allowUpdateOperation` set to true.|
|allowDeleteOperation?|`boolean`|Whether to deploy API Gateway Method for Delete operation on DynamoDB table.|
|deleteRequestTemplate?|`string`|Optional API Gateway Request template for Delete method, it will use the default template if `allowDeleteOperation` is true and `deleteRequestTemplate` is not provided. The default template only supports a partition key and not partition + sort keys.|
|logGroupProps?|[`logs.LogGroupProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_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 @@ -59,6 +59,14 @@ export interface ApiGatewayToDynamoDBProps {
* @default - true
*/
readonly allowReadOperation?: boolean,
/**
* Optional API Gateway Request template for Read method, it will use the default template if
* allowReadOperation is true and readRequestTemplate is not provided.
* The default template only supports a partition key and not partition + sort keys.
*
* @default - None
*/
readonly readRequestTemplate?: string,
/**
* Whether to deploy API Gateway Method for Update operation on DynamoDB table.
*
Expand All @@ -77,6 +85,14 @@ export interface ApiGatewayToDynamoDBProps {
* @default - false
*/
readonly allowDeleteOperation?: boolean,
/**
* Optional API Gateway Request template for Delete method, it will use the default template if
* allowDeleteOperation is true and deleteRequestTemplate is not provided.
* The default template only supports a partition key and not partition + sort keys.
*
* @default - None
*/
readonly deleteRequestTemplate?: string,
/**
* User provided props to override the default props for the CloudWatchLogs LogGroup.
*
Expand Down Expand Up @@ -155,15 +171,31 @@ export class ApiGatewayToDynamoDB extends Construct {
}
// Read
if (props.allowReadOperation === undefined || props.allowReadOperation === true) {
const getRequestTemplate = "{\r\n\"TableName\": \"" + this.dynamoTable.tableName + "\",\r\n \"KeyConditionExpression\": \"" + partitionKeyName + " = :v1\",\r\n \"ExpressionAttributeValues\": {\r\n \":v1\": {\r\n \"S\": \"$input.params('" + partitionKeyName + "')\"\r\n }\r\n }\r\n}";
let readRequestTemplate;

if (props.readRequestTemplate) {
readRequestTemplate = props.readRequestTemplate;
} else {
readRequestTemplate =
`{ \
"TableName": "${this.dynamoTable.tableName}", \
"KeyConditionExpression": "${partitionKeyName} = :v1", \
"ExpressionAttributeValues": { \
":v1": { \
"S": "$input.params('${partitionKeyName}')" \
} \
} \
}`;
}

this.addActionToPolicy("dynamodb:Query");
defaults.addProxyMethodToApiResource({
service: "dynamodb",
action: "Query",
apiGatewayRole: this.apiGatewayRole,
apiMethod: "GET",
apiResource: apiGatewayResource,
requestTemplate: getRequestTemplate
requestTemplate: readRequestTemplate
});
}
// Update
Expand All @@ -181,7 +213,23 @@ export class ApiGatewayToDynamoDB extends Construct {
}
// Delete
if (props.allowDeleteOperation && props.allowDeleteOperation === true) {
const deleteRequestTemplate = "{\r\n \"TableName\": \"" + this.dynamoTable.tableName + "\",\r\n \"Key\": {\r\n \"" + partitionKeyName + "\": {\r\n \"S\": \"$input.params('" + partitionKeyName + "')\"\r\n }\r\n },\r\n \"ConditionExpression\": \"attribute_not_exists(Replies)\",\r\n \"ReturnValues\": \"ALL_OLD\"\r\n}";
let deleteRequestTemplate;

if (props.deleteRequestTemplate) {
deleteRequestTemplate = props.deleteRequestTemplate;
} else {
deleteRequestTemplate =
`{ \
"TableName": "${this.dynamoTable.tableName}", \
"Key": { \
"${partitionKeyName}": { \
"S": "$input.params('${partitionKeyName}')" \
} \
}, \
"ReturnValues": "ALL_OLD" \
}`;
}

this.addActionToPolicy("dynamodb:DeleteItem");
defaults.addProxyMethodToApiResource({
service: "dynamodb",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Object {
},
"Type": "AWS::ApiGateway::RestApi",
},
"testapigatewaydynamodbdefaultRestApiDeploymentFAC726F3c3e5f83088fae14392dcefa7a52bda00": Object {
"testapigatewaydynamodbdefaultRestApiDeploymentFAC726F3818e0ad130f9a49152a4afbd35ada7b6": Object {
"DependsOn": Array [
"testapigatewaydynamodbdefaultRestApiidGET94B6F433",
"testapigatewaydynamodbdefaultRestApiidFD6A9E91",
Expand Down Expand Up @@ -195,7 +195,7 @@ Object {
"Format": "{\\"requestId\\":\\"$context.requestId\\",\\"ip\\":\\"$context.identity.sourceIp\\",\\"user\\":\\"$context.identity.user\\",\\"caller\\":\\"$context.identity.caller\\",\\"requestTime\\":\\"$context.requestTime\\",\\"httpMethod\\":\\"$context.httpMethod\\",\\"resourcePath\\":\\"$context.resourcePath\\",\\"status\\":\\"$context.status\\",\\"protocol\\":\\"$context.protocol\\",\\"responseLength\\":\\"$context.responseLength\\"}",
},
"DeploymentId": Object {
"Ref": "testapigatewaydynamodbdefaultRestApiDeploymentFAC726F3c3e5f83088fae14392dcefa7a52bda00",
"Ref": "testapigatewaydynamodbdefaultRestApiDeploymentFAC726F3818e0ad130f9a49152a4afbd35ada7b6",
},
"MethodSettings": Array [
Object {
Expand Down Expand Up @@ -277,19 +277,11 @@ Object {
"Fn::Join": Array [
"",
Array [
"{
\\"TableName\\": \\"",
"{ \\"TableName\\": \\"",
Object {
"Ref": "testapigatewaydynamodbdefaultDynamoTable0720D92C",
},
"\\",
\\"KeyConditionExpression\\": \\"id = :v1\\",
\\"ExpressionAttributeValues\\": {
\\":v1\\": {
\\"S\\": \\"$input.params('id')\\"
}
}
}",
"\\", \\"KeyConditionExpression\\": \\"id = :v1\\", \\"ExpressionAttributeValues\\": { \\":v1\\": { \\"S\\": \\"$input.params('id')\\" } } }",
],
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"Name": "RestApi"
}
},
"testapigatewaydynamodbRestApiDeployment1898674Bf06c85f4d2c4a0739d5158ea0dd6288a": {
"testapigatewaydynamodbRestApiDeployment1898674B00abb8cad507ed5309ac584f2dddf379": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
Expand Down Expand Up @@ -99,7 +99,7 @@
"Format": "{\"requestId\":\"$context.requestId\",\"ip\":\"$context.identity.sourceIp\",\"user\":\"$context.identity.user\",\"caller\":\"$context.identity.caller\",\"requestTime\":\"$context.requestTime\",\"httpMethod\":\"$context.httpMethod\",\"resourcePath\":\"$context.resourcePath\",\"status\":\"$context.status\",\"protocol\":\"$context.protocol\",\"responseLength\":\"$context.responseLength\"}"
},
"DeploymentId": {
"Ref": "testapigatewaydynamodbRestApiDeployment1898674Bf06c85f4d2c4a0739d5158ea0dd6288a"
"Ref": "testapigatewaydynamodbRestApiDeployment1898674B00abb8cad507ed5309ac584f2dddf379"
},
"MethodSettings": [
{
Expand Down Expand Up @@ -168,11 +168,11 @@
"Fn::Join": [
"",
[
"{\r\n\"TableName\": \"",
"{ \"TableName\": \"",
{
"Ref": "testapigatewaydynamodbDynamoTableEEE3F463"
},
"\",\r\n \"KeyConditionExpression\": \"id = :v1\",\r\n \"ExpressionAttributeValues\": {\r\n \":v1\": {\r\n \"S\": \"$input.params('id')\"\r\n }\r\n }\r\n}"
"\", \"KeyConditionExpression\": \"id = :v1\", \"ExpressionAttributeValues\": { \":v1\": { \"S\": \"$input.params('id')\" } } }"
]
]
}
Expand Down Expand Up @@ -334,11 +334,11 @@
"Fn::Join": [
"",
[
"{\r\n \"TableName\": \"",
"{ \"TableName\": \"",
{
"Ref": "testapigatewaydynamodbDynamoTableEEE3F463"
},
"\",\r\n \"Key\": {\r\n \"id\": {\r\n \"S\": \"$input.params('id')\"\r\n }\r\n },\r\n \"ConditionExpression\": \"attribute_not_exists(Replies)\",\r\n \"ReturnValues\": \"ALL_OLD\"\r\n}"
"\", \"Key\": { \"id\": { \"S\": \"$input.params('id')\" } }, \"ReturnValues\": \"ALL_OLD\" }"
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"Name": "RestApi"
}
},
"testapigatewaydynamodbdefaultRestApiDeploymentFAC726F33f28fd8ef55265fd50a39b60c5ce8e54": {
"testapigatewaydynamodbdefaultRestApiDeploymentFAC726F35a5f302579207081dccaeca1e34daf59": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
Expand Down Expand Up @@ -96,7 +96,7 @@
"Format": "{\"requestId\":\"$context.requestId\",\"ip\":\"$context.identity.sourceIp\",\"user\":\"$context.identity.user\",\"caller\":\"$context.identity.caller\",\"requestTime\":\"$context.requestTime\",\"httpMethod\":\"$context.httpMethod\",\"resourcePath\":\"$context.resourcePath\",\"status\":\"$context.status\",\"protocol\":\"$context.protocol\",\"responseLength\":\"$context.responseLength\"}"
},
"DeploymentId": {
"Ref": "testapigatewaydynamodbdefaultRestApiDeploymentFAC726F33f28fd8ef55265fd50a39b60c5ce8e54"
"Ref": "testapigatewaydynamodbdefaultRestApiDeploymentFAC726F35a5f302579207081dccaeca1e34daf59"
},
"MethodSettings": [
{
Expand Down Expand Up @@ -165,11 +165,11 @@
"Fn::Join": [
"",
[
"{\r\n\"TableName\": \"",
"{ \"TableName\": \"",
{
"Ref": "existingtableE51CCC93"
},
"\",\r\n \"KeyConditionExpression\": \"oddName = :v1\",\r\n \"ExpressionAttributeValues\": {\r\n \":v1\": {\r\n \"S\": \"$input.params('oddName')\"\r\n }\r\n }\r\n}"
"\", \"KeyConditionExpression\": \"oddName = :v1\", \"ExpressionAttributeValues\": { \":v1\": { \"S\": \"$input.params('oddName')\" } } }"
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"Name": "RestApi"
}
},
"testapigatewaydynamodbdefaultRestApiDeploymentFAC726F3c3e5f83088fae14392dcefa7a52bda00": {
"testapigatewaydynamodbdefaultRestApiDeploymentFAC726F3818e0ad130f9a49152a4afbd35ada7b6": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
Expand Down Expand Up @@ -96,7 +96,7 @@
"Format": "{\"requestId\":\"$context.requestId\",\"ip\":\"$context.identity.sourceIp\",\"user\":\"$context.identity.user\",\"caller\":\"$context.identity.caller\",\"requestTime\":\"$context.requestTime\",\"httpMethod\":\"$context.httpMethod\",\"resourcePath\":\"$context.resourcePath\",\"status\":\"$context.status\",\"protocol\":\"$context.protocol\",\"responseLength\":\"$context.responseLength\"}"
},
"DeploymentId": {
"Ref": "testapigatewaydynamodbdefaultRestApiDeploymentFAC726F3c3e5f83088fae14392dcefa7a52bda00"
"Ref": "testapigatewaydynamodbdefaultRestApiDeploymentFAC726F3818e0ad130f9a49152a4afbd35ada7b6"
},
"MethodSettings": [
{
Expand Down Expand Up @@ -165,11 +165,11 @@
"Fn::Join": [
"",
[
"{\r\n\"TableName\": \"",
"{ \"TableName\": \"",
{
"Ref": "testapigatewaydynamodbdefaultDynamoTable0720D92C"
},
"\",\r\n \"KeyConditionExpression\": \"id = :v1\",\r\n \"ExpressionAttributeValues\": {\r\n \":v1\": {\r\n \"S\": \"$input.params('id')\"\r\n }\r\n }\r\n}"
"\", \"KeyConditionExpression\": \"id = :v1\", \"ExpressionAttributeValues\": { \":v1\": { \"S\": \"$input.params('id')\" } } }"
]
]
}
Expand Down