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(iot): allow setting description and enabled of TopicRule #17225

Merged
merged 6 commits into from Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -89,6 +89,7 @@
}
],
"AwsIotSqlVersion": "2016-03-23",
"RuleDisabled": false,
"Sql": "SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-iot/README.md
Expand Up @@ -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)],
});
Expand All @@ -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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"If you wanna make the topic rule disable" -> "If you want to make the topic rule disasbled"


```ts
new iot.TopicRule(this, 'TopicRule', {
sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp FROM 'device/+/data'"),
enabled: false,
});
```
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-iot/lib/topic-rule.ts
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave this as undefined if enabled is undefined (I assume the default in CloudFormation is false for the RuleDisabled property):

Suggested change
ruleDisabled: !(props.enabled ?? true),
ruleDisabled: props.enabled === undefined ? undefined : !props.enabled,

This way, there will be no changes in the existing templates.

sql: sqlConfig.sql,
},
});
Expand Down
Expand Up @@ -12,6 +12,7 @@
}
],
"AwsIotSqlVersion": "2015-10-08",
"RuleDisabled": false,
"Sql": "SELECT topic(2) as device_id FROM 'device/+/data'"
}
}
Expand Down
31 changes: 31 additions & 0 deletions packages/@aws-cdk/aws-iot/test/topic-rule.test.ts
Expand Up @@ -13,6 +13,7 @@ test('Default property', () => {
TopicRulePayload: {
Actions: [],
Sql: "SELECT topic(2) as device_id, temperature FROM 'device/+/data'",
RuleDisabled: false,
},
});
});
Expand Down Expand Up @@ -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'],
Expand Down