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 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
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-iot/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 === undefined ? undefined : !props.enabled,
sql: sqlConfig.sql,
},
});
Expand Down
30 changes: 30 additions & 0 deletions packages/@aws-cdk/aws-iot/test/topic-rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,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