Skip to content

Commit b0f74d5

Browse files
committed
fix(sqs): unable to use CfnParameter 'valueAsNumber' to specify queue properties
validation that was being performed was not taking into account that tokens could be provided for these parameters. added a check and some tests to allow parameters to be supplied. Fixes #7126
1 parent ac001b8 commit b0f74d5

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

packages/@aws-cdk/aws-sqs/lib/validate-props.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Token } from '@aws-cdk/core';
12
import { QueueProps } from './index';
23

34
export function validateProps(props: QueueProps) {
@@ -10,7 +11,7 @@ export function validateProps(props: QueueProps) {
1011
}
1112

1213
function validateRange(label: string, value: number | undefined, minValue: number, maxValue: number, unit?: string) {
13-
if (value === undefined) { return; }
14+
if (value === undefined || Token.isUnresolved(value)) { return; }
1415
const unitSuffix = unit ? ` ${unit}` : '';
1516
if (value < minValue) { throw new Error(`${label} must be ${minValue}${unitSuffix} or more, but ${value} was provided`); }
1617
if (value > maxValue) { throw new Error(`${label} must be ${maxValue}${unitSuffix} of less, but ${value} was provided`); }

packages/@aws-cdk/aws-sqs/test/test.sqs.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect, haveResource } from '@aws-cdk/assert';
22
import * as iam from '@aws-cdk/aws-iam';
33
import * as kms from '@aws-cdk/aws-kms';
4-
import { Duration, Stack } from '@aws-cdk/core';
4+
import { CfnParameter, Duration, Stack } from '@aws-cdk/core';
55
import { Test } from 'nodeunit';
66
import * as sqs from '../lib';
77

@@ -54,6 +54,58 @@ export = {
5454
test.done();
5555
},
5656

57+
'message retention period must be between 1 minute to 14 days'(test: Test) {
58+
// GIVEN
59+
const stack = new Stack();
60+
61+
// THEN
62+
test.throws(() => new sqs.Queue(stack, 'MyQueue', {
63+
retentionPeriod: Duration.seconds(30),
64+
}), /message retention period must be 60 seconds or more/);
65+
66+
test.throws(() => new sqs.Queue(stack, 'AnotherQueue', {
67+
retentionPeriod: Duration.days(15),
68+
}), /message retention period must be 1209600 seconds of less/);
69+
70+
test.done();
71+
},
72+
73+
'message retention period can be provided as a parameter'(test: Test) {
74+
// GIVEN
75+
const stack = new Stack();
76+
const parameter = new CfnParameter(stack, 'my-retention-period', {
77+
type: 'Number',
78+
default: 30,
79+
});
80+
81+
// WHEN
82+
new sqs.Queue(stack, 'MyQueue', {
83+
retentionPeriod: Duration.seconds(parameter.valueAsNumber),
84+
});
85+
86+
// THEN
87+
expect(stack).toMatch({
88+
'Parameters': {
89+
'myretentionperiod': {
90+
'Type': 'Number',
91+
'Default': 30,
92+
},
93+
},
94+
'Resources': {
95+
'MyQueueE6CA6235': {
96+
'Type': 'AWS::SQS::Queue',
97+
'Properties': {
98+
'MessageRetentionPeriod': {
99+
'Ref': 'myretentionperiod',
100+
},
101+
},
102+
},
103+
},
104+
});
105+
106+
test.done();
107+
},
108+
57109
'addToPolicy will automatically create a policy for this queue'(test: Test) {
58110
const stack = new Stack();
59111
const queue = new sqs.Queue(stack, 'MyQueue');

0 commit comments

Comments
 (0)