-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iot-actions): Support to send message to IoT Events
- Loading branch information
Showing
13 changed files
with
956 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/@aws-cdk/aws-iot-actions/lib/iotevents-put-message-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import * as iot from '@aws-cdk/aws-iot'; | ||
import * as iotevents from '@aws-cdk/aws-iotevents'; | ||
import { CommonActionProps } from './common-action-props'; | ||
import { singletonActionRole } from './private/role'; | ||
|
||
/** | ||
* Configuration properties of an action for the IoT Events. | ||
*/ | ||
export interface IotEventsPutMessageActionProps extends CommonActionProps { | ||
/** | ||
* Whether to process the event actions as a batch. | ||
* | ||
* When batchMode is true, you can't specify a messageId. | ||
* | ||
* When batchMode is true and the rule SQL statement evaluates to an Array, | ||
* each Array element is treated as a separate message when Events by calling BatchPutMessage. | ||
* The resulting array can't have more than 10 messages. | ||
* | ||
* @default false | ||
*/ | ||
readonly batchMode?: boolean; | ||
|
||
/** | ||
* The ID of the message. | ||
* | ||
* When batchMode is true, you can't specify a messageId--a new UUID value will be assigned. | ||
* Assign a value to this property to ensure that only one input (message) with a given messageId will be processed by an AWS IoT Events detector. | ||
* | ||
* @default - none -- a new UUID value will be assigned | ||
*/ | ||
readonly messageId?: string; | ||
} | ||
|
||
/** | ||
* The action to put the message from an MQTT message to the IoT Events input. | ||
*/ | ||
export class IotEventsPutMessageAction implements iot.IAction { | ||
private readonly batchMode?: boolean; | ||
private readonly messageId?: string; | ||
private readonly role?: iam.IRole; | ||
|
||
/** | ||
* @param input The IoT Events input to put messages. | ||
* @param props Optional properties to not use default | ||
*/ | ||
constructor(private readonly input: iotevents.IInput, props: IotEventsPutMessageActionProps = {}) { | ||
this.batchMode = props.batchMode; | ||
this.messageId = props.messageId; | ||
this.role = props.role; | ||
|
||
if (this.batchMode && this.messageId) { | ||
throw new Error('messageId is not allowed when batchMode is true'); | ||
} | ||
} | ||
|
||
bind(rule: iot.ITopicRule): iot.ActionConfig { | ||
const role = this.role ?? singletonActionRole(rule); | ||
this.input.grantWrite(role); | ||
|
||
return { | ||
configuration: { | ||
iotEvents: { | ||
batchMode: this.batchMode, | ||
inputName: this.input.inputName, | ||
messageId: this.messageId, | ||
roleArn: role.roleArn, | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/@aws-cdk/aws-iot-actions/test/iot/integ.iotevents-put-message-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Stack verification steps: | ||
* * aws iot-data publish --topic device/mydevice/data --qos 1 --payload (echo '[{"payload":{"deviceId":"001"}},{"payload":{"deviceId":"002"}}]' | base64) --region us-east-1 | ||
*/ | ||
import * as iot from '@aws-cdk/aws-iot'; | ||
import * as iotevents from '@aws-cdk/aws-iotevents'; | ||
import * as logs from '@aws-cdk/aws-logs'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import { IntegTest } from '@aws-cdk/integ-tests'; | ||
import * as actions from '../../lib'; | ||
|
||
class TestStack extends cdk.Stack { | ||
public readonly detectorModelName: string; | ||
|
||
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { | ||
super(scope, id, props); | ||
|
||
const logGroup = new logs.LogGroup(this, 'logs', { removalPolicy: cdk.RemovalPolicy.DESTROY }); | ||
const topicRule = new iot.TopicRule(this, 'TopicRule', { | ||
sql: iot.IotSql.fromStringAsVer20160323( | ||
"SELECT * FROM 'device/+/data'", | ||
), | ||
errorAction: new actions.CloudWatchLogsAction(logGroup), | ||
}); | ||
|
||
const input = new iotevents.Input(this, 'MyInput', { | ||
attributeJsonPaths: ['payload.deviceId'], | ||
}); | ||
|
||
const detectorModel = new iotevents.DetectorModel(this, 'MyDetectorModel', { | ||
detectorKey: 'payload.deviceId', | ||
initialState: new iotevents.State({ | ||
stateName: 'initialState', | ||
onEnter: [{ | ||
eventName: 'enter', | ||
condition: iotevents.Expression.currentInput(input), | ||
}], | ||
}), | ||
}); | ||
|
||
topicRule.addAction( | ||
new actions.IotEventsPutMessageAction(input, { | ||
batchMode: true, | ||
}), | ||
); | ||
|
||
this.detectorModelName = detectorModel.detectorModelName; | ||
} | ||
} | ||
|
||
const app = new cdk.App(); | ||
const stack = new TestStack(app, 'iotevents-put-message-action-test-stack'); | ||
new IntegTest(app, 'iotevents', { testCases: [stack] }); |
1 change: 1 addition & 0 deletions
1
...ges/@aws-cdk/aws-iot-actions/test/iot/iotevents-put-message-action.integ.snapshot/cdk.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"version":"20.0.0"} |
11 changes: 11 additions & 0 deletions
11
.../@aws-cdk/aws-iot-actions/test/iot/iotevents-put-message-action.integ.snapshot/integ.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"version": "20.0.0", | ||
"testCases": { | ||
"iotevents/DefaultTest": { | ||
"stacks": [ | ||
"iotevents-put-message-action-test-stack" | ||
], | ||
"assertionStack": "ioteventsDefaultTestDeployAssertE216288D" | ||
} | ||
} | ||
} |
Oops, something went wrong.