Skip to content

Commit

Permalink
feat(iot): allow setting description and enabled of TopicRule (aw…
Browse files Browse the repository at this point in the history
…s#17225)

I'm trying to implement aws-iot L2 Constructs.

This PR is one of steps after following PR: 
- aws#16681 (comment)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
yamatatsu authored and TikiTDO committed Feb 21, 2022
1 parent 4b76cd9 commit bd0275c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
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:

```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 === 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
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

0 comments on commit bd0275c

Please sign in to comment.