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): add optional resourceName parameter #898

Merged
merged 5 commits into from
Feb 15, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ new ApiGatewayToDynamoDB(this, "test-api-gateway-dynamodb-default", new ApiGatew
|dynamoTableProps?|[`dynamodb.TableProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.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/v2/docs/aws-cdk-lib.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/v2/docs/aws-cdk-lib.aws_apigateway.RestApiProps.html)|Optional user-provided props to override the default props for the API Gateway.|
|resourceName?|`string`|Optional name of the resource on the API Gateway. Defaults to the table's partitionKeyName|
|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. This property can only be specified if the `allowCreateOperation` property is set to true.|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export interface ApiGatewayToDynamoDBProps {
* @default - Default properties are used.
*/
readonly apiGatewayProps?: api.RestApiProps;
/**
* Optional resource name on the API
* This property is useful if your integration does not directly use the partition key name
*
* @default - partition key name, retrieved from the DynamoDB table object
*/
readonly resourceName?: string;
/**
* Whether to deploy an API Gateway Method for POST HTTP operations on the DynamoDB table (i.e. dynamodb:PutItem).
*
Expand Down Expand Up @@ -228,6 +235,8 @@ export class ApiGatewayToDynamoDB extends Construct {
partitionKeyName = getPartitionKeyNameFromTable(props.existingTableObj);
}

const resourceName = props.resourceName ?? partitionKeyName;

// Since we are only invoking this function with an existing Table or tableProps,
// (not a table interface), we know that the implementation will always return
// a Table object and we can safely cast away the optional aspect of the type.
Expand All @@ -246,7 +255,7 @@ export class ApiGatewayToDynamoDB extends Construct {
});

// Setup the API Gateway Resource
const apiGatewayResource: api.Resource = this.apiGateway.root.addResource("{" + partitionKeyName + "}");
const apiGatewayResource: api.Resource = this.apiGateway.root.addResource("{" + resourceName + "}");

// Setup API Gateway Method
// Create
Expand All @@ -272,7 +281,7 @@ export class ApiGatewayToDynamoDB extends Construct {
"KeyConditionExpression": "${partitionKeyName} = :v1", \
"ExpressionAttributeValues": { \
":v1": { \
"S": "$input.params('${partitionKeyName}')" \
"S": "$input.params('${resourceName}')" \
} \
} \
}`;
Expand Down Expand Up @@ -311,7 +320,7 @@ export class ApiGatewayToDynamoDB extends Construct {
"TableName": "${this.dynamoTable.tableName}", \
"Key": { \
"${partitionKeyName}": { \
"S": "$input.params('${partitionKeyName}')" \
"S": "$input.params('${resourceName}')" \
} \
}, \
"ReturnValues": "ALL_OLD" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,17 @@ test('Construct accepts additional delete request templates', () => {
});
});

test('Construct can customize the api resourceName', () => {
const stack = new Stack();
new ApiGatewayToDynamoDB(stack, 'api-gateway-dynamodb', {
resourceName: 'my-resource-name',
});

expect(stack).toHaveResource("AWS::ApiGateway::Resource", {
PathPart: "{my-resource-name}",
});
});

test('Construct uses default integration responses', () => {
const stack = new Stack();
new ApiGatewayToDynamoDB(stack, 'api-gateway-dynamodb', {
Expand Down
Loading