From cdb948c1d20637bf28bbef7d597669a3b424c800 Mon Sep 17 00:00:00 2001 From: yamatatsu Date: Fri, 29 Oct 2021 09:31:05 +0900 Subject: [PATCH 1/4] feat(iot): allow setting `description` and `enabled` of TopicRule 1. add properties `description` and `enabled` 2. add tests --- packages/@aws-cdk/aws-iot/lib/topic-rule.ts | 16 ++++++++++ .../test/integ.topic-rule.expected.json | 1 + .../@aws-cdk/aws-iot/test/topic-rule.test.ts | 31 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/packages/@aws-cdk/aws-iot/lib/topic-rule.ts b/packages/@aws-cdk/aws-iot/lib/topic-rule.ts index a8cd21fe2bd96..43cf3cc7ebbce 100644 --- a/packages/@aws-cdk/aws-iot/lib/topic-rule.ts +++ b/packages/@aws-cdk/aws-iot/lib/topic-rule.ts @@ -41,6 +41,20 @@ export interface TopicRuleProps { */ readonly actions?: IAction[]; + /** + * A textual description of the topic rule. + * + * @default None + */ + readonly description?: string; + + /** + * Specifies whether the rule is enabled. + * + * @default true + */ + readonly enabled?: boolean + /** * A simplified SQL syntax to filter messages received on an MQTT topic and push the data elsewhere. * @@ -102,6 +116,8 @@ export class TopicRule extends Resource implements ITopicRule { topicRulePayload: { actions: Lazy.any({ produce: () => this.actions }), awsIotSqlVersion: sqlConfig.awsIotSqlVersion, + description: props.description, + ruleDisabled: !(props.enabled ?? true), sql: sqlConfig.sql, }, }); diff --git a/packages/@aws-cdk/aws-iot/test/integ.topic-rule.expected.json b/packages/@aws-cdk/aws-iot/test/integ.topic-rule.expected.json index 9daad98410825..d143798642675 100644 --- a/packages/@aws-cdk/aws-iot/test/integ.topic-rule.expected.json +++ b/packages/@aws-cdk/aws-iot/test/integ.topic-rule.expected.json @@ -12,6 +12,7 @@ } ], "AwsIotSqlVersion": "2015-10-08", + "RuleDisabled": false, "Sql": "SELECT topic(2) as device_id FROM 'device/+/data'" } } diff --git a/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts b/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts index 66246e860dddb..d357a2ec45b7a 100644 --- a/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts +++ b/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts @@ -13,6 +13,7 @@ test('Default property', () => { TopicRulePayload: { Actions: [], Sql: "SELECT topic(2) as device_id, temperature FROM 'device/+/data'", + RuleDisabled: false, }, }); }); @@ -71,6 +72,36 @@ test('can set physical name', () => { }); }); +test('can set description', () => { + const stack = new cdk.Stack(); + + new iot.TopicRule(stack, 'MyTopicRule', { + description: 'test-description', + sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"), + }); + + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { + TopicRulePayload: { + Description: 'test-description', + }, + }); +}); + +test('can set ruleDisabled', () => { + const stack = new cdk.Stack(); + + new iot.TopicRule(stack, 'MyTopicRule', { + enabled: false, + sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"), + }); + + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { + TopicRulePayload: { + RuleDisabled: true, + }, + }); +}); + test.each([ ['fromStringAsVer20151008', iot.IotSql.fromStringAsVer20151008, '2015-10-08'], ['fromStringAsVer20160323', iot.IotSql.fromStringAsVer20160323, '2016-03-23'], From d32895e319e6d385888b91fef02e11171f53194e Mon Sep 17 00:00:00 2001 From: yamatatsu Date: Fri, 29 Oct 2021 17:09:41 +0900 Subject: [PATCH 2/4] test(iot-actions): fix-snapshot --- .../test/lambda/integ.lambda-function-action.expected.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.expected.json b/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.expected.json index 345ead052c921..bb3b57a9dd180 100644 --- a/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.expected.json +++ b/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.expected.json @@ -89,6 +89,7 @@ } ], "AwsIotSqlVersion": "2016-03-23", + "RuleDisabled": false, "Sql": "SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'" } } From 28abe0a4e45b012bcf619deb3c9b2ffbec2a16ff Mon Sep 17 00:00:00 2001 From: yamatatsu Date: Fri, 29 Oct 2021 17:22:12 +0900 Subject: [PATCH 3/4] doc(iot): write README --- packages/@aws-cdk/aws-iot/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/@aws-cdk/aws-iot/README.md b/packages/@aws-cdk/aws-iot/README.md index 6a9640629891a..42ab651f26f81 100644 --- a/packages/@aws-cdk/aws-iot/README.md +++ b/packages/@aws-cdk/aws-iot/README.md @@ -59,6 +59,8 @@ const func = new lambda.Function(this, 'MyFunction', { }); new iot.TopicRule(this, 'TopicRule', { + topicRuleName: 'MyTopicRule', // optional + description: 'invokes the lambda finction', // optional sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp FROM 'device/+/data'"), actions: [new actions.LambdaFunctionAction(func)], }); @@ -72,3 +74,12 @@ const topicRule = new iot.TopicRule(this, 'TopicRule', { }); topicRule.addAction(new actions.LambdaFunctionAction(func)) ``` + +If you wanna make the topic rule disable, add property `enabled: false` as following: + +```ts +new iot.TopicRule(this, 'TopicRule', { + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp FROM 'device/+/data'"), + enabled: false, +}); +``` From 15b50d73d1a20fb70db18c0680df5571ea5d92da Mon Sep 17 00:00:00 2001 From: yamatatsu Date: Sat, 30 Oct 2021 10:28:41 +0900 Subject: [PATCH 4/4] fix(iot): change default of RuleDisabled to undefined --- .../test/lambda/integ.lambda-function-action.expected.json | 1 - packages/@aws-cdk/aws-iot/lib/topic-rule.ts | 2 +- packages/@aws-cdk/aws-iot/test/integ.topic-rule.expected.json | 1 - packages/@aws-cdk/aws-iot/test/topic-rule.test.ts | 1 - 4 files changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.expected.json b/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.expected.json index bb3b57a9dd180..345ead052c921 100644 --- a/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.expected.json +++ b/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.expected.json @@ -89,7 +89,6 @@ } ], "AwsIotSqlVersion": "2016-03-23", - "RuleDisabled": false, "Sql": "SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'" } } diff --git a/packages/@aws-cdk/aws-iot/lib/topic-rule.ts b/packages/@aws-cdk/aws-iot/lib/topic-rule.ts index 43cf3cc7ebbce..8860a611e4af3 100644 --- a/packages/@aws-cdk/aws-iot/lib/topic-rule.ts +++ b/packages/@aws-cdk/aws-iot/lib/topic-rule.ts @@ -117,7 +117,7 @@ export class TopicRule extends Resource implements ITopicRule { actions: Lazy.any({ produce: () => this.actions }), awsIotSqlVersion: sqlConfig.awsIotSqlVersion, description: props.description, - ruleDisabled: !(props.enabled ?? true), + ruleDisabled: props.enabled === undefined ? undefined : !props.enabled, sql: sqlConfig.sql, }, }); diff --git a/packages/@aws-cdk/aws-iot/test/integ.topic-rule.expected.json b/packages/@aws-cdk/aws-iot/test/integ.topic-rule.expected.json index d143798642675..9daad98410825 100644 --- a/packages/@aws-cdk/aws-iot/test/integ.topic-rule.expected.json +++ b/packages/@aws-cdk/aws-iot/test/integ.topic-rule.expected.json @@ -12,7 +12,6 @@ } ], "AwsIotSqlVersion": "2015-10-08", - "RuleDisabled": false, "Sql": "SELECT topic(2) as device_id FROM 'device/+/data'" } } diff --git a/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts b/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts index d357a2ec45b7a..6e4a49c234e60 100644 --- a/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts +++ b/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts @@ -13,7 +13,6 @@ test('Default property', () => { TopicRulePayload: { Actions: [], Sql: "SELECT topic(2) as device_id, temperature FROM 'device/+/data'", - RuleDisabled: false, }, }); });