Skip to content

Commit bbffe2f

Browse files
committed
feat(aws-apigateway-sqs): update construct to allow custom path params for CRD operations on the SQS
1 parent b7cdf3d commit bbffe2f

2 files changed

Lines changed: 73 additions & 11 deletions

File tree

source/patterns/@aws-solutions-constructs/aws-apigateway-sqs/lib/index.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,27 @@ export interface ApiGatewayToSqsProps {
169169
* @default - None
170170
*/
171171
readonly encryptionKeyProps?: kms.KeyProps;
172+
/**
173+
* Optional, custom API Gateway path for the GET method.
174+
* This property can only be specified if the `allowReadOperation` property is not set to false.
175+
*
176+
* @default - ""
177+
*/
178+
readonly readRequestPath?: string;
179+
/**
180+
* Optional, custom API Gateway path for the POST method.
181+
* This property can only be specified if the `allowCreateOperation` property is not set to false.
182+
*
183+
* @default - ""
184+
*/
185+
readonly createRequestPath?: string;
186+
/**
187+
* Optional, custom API Gateway path for the DELETE method.
188+
* This property can only be specif||ied if the `allowDeleteOperation` property is not set to false.
189+
*
190+
* @default - "message"
191+
*/
192+
readonly deleteRequestPath?: string;
172193
}
173194

174195
/**
@@ -200,15 +221,15 @@ export class ApiGatewayToSqs extends Construct {
200221

201222
if (this.CheckCreateRequestProps(props)) {
202223
throw new Error(`The 'allowCreateOperation' property must be set to true when setting any of the following: ` +
203-
`'createRequestTemplate', 'additionalCreateRequestTemplates', 'createIntegrationResponses'`);
224+
`'createRequestTemplate', 'additionalCreateRequestTemplates', 'createIntegrationResponses', 'createRequestPath'`);
204225
}
205226
if (this.CheckReadRequestProps(props)) {
206227
throw new Error(`The 'allowReadOperation' property must be set to true or undefined when setting any of the following: ` +
207-
`'readRequestTemplate', 'additionalReadRequestTemplates', 'readIntegrationResponses'`);
228+
`'readRequestTemplate', 'additionalReadRequestTemplates', 'readIntegrationResponses', 'readRequestPath'`);
208229
}
209230
if (this.CheckDeleteRequestProps(props)) {
210231
throw new Error(`The 'allowDeleteOperation' property must be set to true when setting any of the following: ` +
211-
`'deleteRequestTemplate', 'additionalDeleteRequestTemplates', 'deleteIntegrationResponses'`);
232+
`'deleteRequestTemplate', 'additionalDeleteRequestTemplates', 'deleteIntegrationResponses', 'deleteRequestPath'`);
212233
}
213234

214235
// Setup the dead letter queue, if applicable
@@ -244,13 +265,14 @@ export class ApiGatewayToSqs extends Construct {
244265
// Create
245266
const createRequestTemplate = props.createRequestTemplate ?? this.defaultCreateRequestTemplate;
246267
if (props.allowCreateOperation && props.allowCreateOperation === true) {
268+
const apiCreateRequestResource = props.createRequestPath ? this.apiGateway.root.addResource(props.createRequestPath) : this.apiGateway.root;
247269
this.addActionToPolicy("sqs:SendMessage");
248270
defaults.addProxyMethodToApiResource({
249271
service: "sqs",
250272
path: `${cdk.Aws.ACCOUNT_ID}/${this.sqsQueue.queueName}`,
251273
apiGatewayRole: this.apiGatewayRole,
252274
apiMethod: "POST",
253-
apiResource: this.apiGateway.root,
275+
apiResource: apiCreateRequestResource,
254276
requestTemplate: createRequestTemplate,
255277
additionalRequestTemplates: props.additionalCreateRequestTemplates,
256278
contentType: "'application/x-www-form-urlencoded'",
@@ -261,13 +283,14 @@ export class ApiGatewayToSqs extends Construct {
261283
// Read
262284
const readRequestTemplate = props.readRequestTemplate ?? this.defaultReadRequestTemplate;
263285
if (props.allowReadOperation === undefined || props.allowReadOperation === true) {
286+
const apiReadRequestResource = props.readRequestPath ? this.apiGateway.root.addResource(props.readRequestPath) : this.apiGateway.root;
264287
this.addActionToPolicy("sqs:ReceiveMessage");
265288
defaults.addProxyMethodToApiResource({
266289
service: "sqs",
267290
path: `${cdk.Aws.ACCOUNT_ID}/${this.sqsQueue.queueName}`,
268291
apiGatewayRole: this.apiGatewayRole,
269292
apiMethod: "GET",
270-
apiResource: this.apiGateway.root,
293+
apiResource: apiReadRequestResource,
271294
requestTemplate: readRequestTemplate,
272295
additionalRequestTemplates: props.additionalReadRequestTemplates,
273296
contentType: "'application/x-www-form-urlencoded'",
@@ -278,14 +301,14 @@ export class ApiGatewayToSqs extends Construct {
278301
// Delete
279302
const deleteRequestTemplate = props.deleteRequestTemplate ?? this.defaultDeleteRequestTemplate;
280303
if (props.allowDeleteOperation && props.allowDeleteOperation === true) {
281-
const apiGatewayResource = this.apiGateway.root.addResource('message');
304+
const apiDeleteRequestResource = this.apiGateway.root.addResource(props.deleteRequestPath ?? 'message');
282305
this.addActionToPolicy("sqs:DeleteMessage");
283306
defaults.addProxyMethodToApiResource({
284307
service: "sqs",
285308
path: `${cdk.Aws.ACCOUNT_ID}/${this.sqsQueue.queueName}`,
286309
apiGatewayRole: this.apiGatewayRole,
287310
apiMethod: "DELETE",
288-
apiResource: apiGatewayResource,
311+
apiResource: apiDeleteRequestResource,
289312
requestTemplate: deleteRequestTemplate,
290313
additionalRequestTemplates: props.additionalDeleteRequestTemplates,
291314
contentType: "'application/x-www-form-urlencoded'",
@@ -294,22 +317,22 @@ export class ApiGatewayToSqs extends Construct {
294317
}
295318
}
296319
private CheckReadRequestProps(props: ApiGatewayToSqsProps): boolean {
297-
if ((props.readRequestTemplate || props.additionalReadRequestTemplates || props.readIntegrationResponses)
320+
if ((props.readRequestTemplate || props.additionalReadRequestTemplates || props.readIntegrationResponses || props.readRequestPath)
298321
&& props.allowReadOperation === false) {
299322
return true;
300323
}
301324
return false;
302325
}
303326
private CheckDeleteRequestProps(props: ApiGatewayToSqsProps): boolean {
304-
if ((props.deleteRequestTemplate || props.additionalDeleteRequestTemplates || props.deleteIntegrationResponses)
327+
if ((props.deleteRequestTemplate || props.additionalDeleteRequestTemplates || props.deleteIntegrationResponses || props.deleteRequestPath)
305328
&& props.allowDeleteOperation !== true) {
306329
return true;
307330
}
308331
return false;
309332
}
310333

311334
private CheckCreateRequestProps(props: ApiGatewayToSqsProps): boolean {
312-
if ((props.createRequestTemplate || props.additionalCreateRequestTemplates || props.createIntegrationResponses)
335+
if ((props.createRequestTemplate || props.additionalCreateRequestTemplates || props.createIntegrationResponses || props.createRequestPath)
313336
&& props.allowCreateOperation !== true) {
314337
return true;
315338
}

source/patterns/@aws-solutions-constructs/aws-apigateway-sqs/test/apigateway-sqs.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test('Test properties', () => {
6969
deployDeadLetterQueue: true,
7070
maxReceiveCount: 3
7171
});
72-
// Assertion 1
72+
// Assertion 1
7373
expect(pattern.apiGateway).toBeDefined();
7474
// Assertion 2
7575
expect(pattern.sqsQueue).toBeDefined();
@@ -163,6 +163,45 @@ test('Test deployment for override ApiGateway deleteRequestTemplate', () => {
163163
});
164164
});
165165

166+
test('Test deployment for override ApiGateway createRequestPath', () => {
167+
const stack = new Stack();
168+
169+
new ApiGatewayToSqs(stack, 'api-gateway-sqs', {
170+
createRequestPath: "testPath",
171+
allowCreateOperation: true
172+
});
173+
const template = Template.fromStack(stack);
174+
template.hasResourceProperties('AWS::ApiGateway::Resource', {
175+
PathPart: "testPath"
176+
});
177+
});
178+
179+
test('Test deployment for override ApiGateway getRequestPath', () => {
180+
const stack = new Stack();
181+
182+
new ApiGatewayToSqs(stack, 'api-gateway-sqs', {
183+
readRequestPath: "testPath",
184+
allowReadOperation: true
185+
});
186+
const template = Template.fromStack(stack);
187+
template.hasResourceProperties('AWS::ApiGateway::Resource', {
188+
PathPart: "testPath"
189+
});
190+
});
191+
192+
test('Test deployment for for override ApiGateway deleteRequestTemplate', () => {
193+
const stack = new Stack();
194+
195+
new ApiGatewayToSqs(stack, 'api-gateway-sqs', {
196+
allowDeleteOperation: true,
197+
deleteRequestPath: "testPath"
198+
});
199+
const template = Template.fromStack(stack);
200+
template.hasResourceProperties('AWS::ApiGateway::Resource', {
201+
PathPart: "testPath"
202+
});
203+
});
204+
166205
test('Test deployment for disallow delete operation', () => {
167206
const stack = new Stack();
168207

0 commit comments

Comments
 (0)