|
| 1 | +import { ArnFormat, Resource, Stack, IResource } from '@aws-cdk/core'; |
| 2 | +import { Construct } from 'constructs'; |
| 3 | +import { IotSql } from './iot-sql'; |
| 4 | +import { CfnTopicRule } from './iot.generated'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Represents an AWS IoT Rule |
| 8 | + */ |
| 9 | +export interface ITopicRule extends IResource { |
| 10 | + /** |
| 11 | + * The value of the topic rule Amazon Resource Name (ARN), such as |
| 12 | + * arn:aws:iot:us-east-2:123456789012:rule/rule_name |
| 13 | + * |
| 14 | + * @attribute |
| 15 | + */ |
| 16 | + readonly topicRuleArn: string; |
| 17 | + |
| 18 | + /** |
| 19 | + * The name topic rule |
| 20 | + * |
| 21 | + * @attribute |
| 22 | + */ |
| 23 | + readonly topicRuleName: string; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Properties for defining an AWS IoT Rule |
| 28 | + */ |
| 29 | +export interface TopicRuleProps { |
| 30 | + /** |
| 31 | + * The name of the rule. |
| 32 | + * @default None |
| 33 | + */ |
| 34 | + readonly topicRuleName?: string; |
| 35 | + |
| 36 | + /** |
| 37 | + * A simplified SQL syntax to filter messages received on an MQTT topic and push the data elsewhere. |
| 38 | + * |
| 39 | + * @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-reference.html |
| 40 | + */ |
| 41 | + readonly sql: IotSql; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Defines an AWS IoT Rule in this stack. |
| 46 | + */ |
| 47 | +export class TopicRule extends Resource implements ITopicRule { |
| 48 | + /** |
| 49 | + * Import an existing AWS IoT Rule provided an ARN |
| 50 | + * |
| 51 | + * @param scope The parent creating construct (usually `this`). |
| 52 | + * @param id The construct's name. |
| 53 | + * @param topicRuleArn AWS IoT Rule ARN (i.e. arn:aws:iot:<region>:<account-id>:rule/MyRule). |
| 54 | + */ |
| 55 | + public static fromTopicRuleArn(scope: Construct, id: string, topicRuleArn: string): ITopicRule { |
| 56 | + const parts = Stack.of(scope).splitArn(topicRuleArn, ArnFormat.SLASH_RESOURCE_NAME); |
| 57 | + if (!parts.resourceName) { |
| 58 | + throw new Error(`Missing topic rule name in ARN: '${topicRuleArn}'`); |
| 59 | + } |
| 60 | + const resourceName = parts.resourceName; |
| 61 | + |
| 62 | + class Import extends Resource implements ITopicRule { |
| 63 | + public readonly topicRuleArn = topicRuleArn; |
| 64 | + public readonly topicRuleName = resourceName; |
| 65 | + } |
| 66 | + return new Import(scope, id, { |
| 67 | + environmentFromArn: topicRuleArn, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Arn of this rule |
| 73 | + * @attribute |
| 74 | + */ |
| 75 | + public readonly topicRuleArn: string; |
| 76 | + |
| 77 | + /** |
| 78 | + * Name of this rule |
| 79 | + * @attribute |
| 80 | + */ |
| 81 | + public readonly topicRuleName: string; |
| 82 | + |
| 83 | + constructor(scope: Construct, id: string, props: TopicRuleProps) { |
| 84 | + super(scope, id, { |
| 85 | + physicalName: props.topicRuleName, |
| 86 | + }); |
| 87 | + |
| 88 | + const sqlConfig = props.sql.bind(this); |
| 89 | + |
| 90 | + const resource = new CfnTopicRule(this, 'Resource', { |
| 91 | + ruleName: this.physicalName, |
| 92 | + topicRulePayload: { |
| 93 | + actions: [], |
| 94 | + awsIotSqlVersion: sqlConfig.awsIotSqlVersion, |
| 95 | + sql: sqlConfig.sql, |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + this.topicRuleArn = this.getResourceArnAttribute(resource.attrArn, { |
| 100 | + service: 'iot', |
| 101 | + resource: 'rule', |
| 102 | + resourceName: this.physicalName, |
| 103 | + }); |
| 104 | + this.topicRuleName = this.getResourceNameAttribute(resource.ref); |
| 105 | + } |
| 106 | +} |
0 commit comments