forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
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 for sending messages to iot-events (aws#19953
) This PR includes to support the action for sending messages to IoT Events. This feature is described [this documentation](https://docs.aws.amazon.com/iot/latest/developerguide/iotevents-rule-action.html). I actually confirmed that the behavior of the action deployed by integ-test is all right. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
586b8eb
commit 1009e73
Showing
29 changed files
with
1,028 additions
and
28 deletions.
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
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
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
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
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
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'); | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public _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
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
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
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
52 changes: 52 additions & 0 deletions
52
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,52 @@ | ||
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; | ||
} | ||
} | ||
|
||
// App | ||
const app = new cdk.App(); | ||
const stack = new TestStack(app, 'iotevents-put-message-action-test-stack'); | ||
new IntegTest(app, 'iotevents', { testCases: [stack] }); | ||
|
||
app.synth(); |
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.