From a70391b19c6a48badce0045d10523a729eed99f2 Mon Sep 17 00:00:00 2001 From: yamatatsu Date: Wed, 9 Mar 2022 21:38:13 +0900 Subject: [PATCH 1/3] feat(iotevents): support SetVariable action --- .../@aws-cdk/aws-iotevents-actions/README.md | 29 ++++++ .../aws-iotevents-actions/lib/index.ts | 1 + .../lib/iotevents-set-variable-action.ts | 25 +++++ ...otevents-set-variable-action.expected.json | 95 +++++++++++++++++++ .../integ.iotevents-set-variable-action.ts | 42 ++++++++ .../iotevents-set-variable-action.test.ts | 43 +++++++++ 6 files changed, 235 insertions(+) create mode 100644 packages/@aws-cdk/aws-iotevents-actions/lib/iotevents-set-variable-action.ts create mode 100644 packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.expected.json create mode 100644 packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.ts create mode 100644 packages/@aws-cdk/aws-iotevents-actions/test/iotevents/iotevents-set-variable-action.test.ts diff --git a/packages/@aws-cdk/aws-iotevents-actions/README.md b/packages/@aws-cdk/aws-iotevents-actions/README.md index eb88dc82bb3c3..eb5430fa064ae 100644 --- a/packages/@aws-cdk/aws-iotevents-actions/README.md +++ b/packages/@aws-cdk/aws-iotevents-actions/README.md @@ -24,8 +24,37 @@ AWS IoT Events can trigger actions when it detects a specified event or transiti Currently supported are: +- Set variable to detector instanse - Invoke a Lambda function +## Set variable to detector instanse + +The code snippet below creates an Action that set variable to detector instanse +when it is triggered. + +```ts +import * as iotevents from '@aws-cdk/aws-iotevents'; +import * as actions from '@aws-cdk/aws-iotevents-actions'; + +declare const input: iotevents.IInput; + +const state = new iotevents.State({ + stateName: 'MyState', + onEnter: [{ + eventName: 'test-event', + condition: iotevents.Expression.currentInput(input), + actions: [ + actions: [ + new actions.IoteventsSetVariableAction( + 'MyVariable', + iotevents.Expression.inputAttribute(input, 'payload.temperature'), + ), + ], + ], + }], +}); +``` + ## Invoke a Lambda function The code snippet below creates an Action that invoke a Lambda function diff --git a/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts b/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts index 4b2ec39329315..7c2395d8ea8ea 100644 --- a/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts +++ b/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts @@ -1 +1,2 @@ +export * from './iotevents-set-variable-action'; export * from './lambda-invoke-action'; diff --git a/packages/@aws-cdk/aws-iotevents-actions/lib/iotevents-set-variable-action.ts b/packages/@aws-cdk/aws-iotevents-actions/lib/iotevents-set-variable-action.ts new file mode 100644 index 0000000000000..5a2e49503d761 --- /dev/null +++ b/packages/@aws-cdk/aws-iotevents-actions/lib/iotevents-set-variable-action.ts @@ -0,0 +1,25 @@ +import * as iotevents from '@aws-cdk/aws-iotevents'; +import { Construct } from 'constructs'; + +/** + * The action to create a variable with a specified value. + */ +export class IoteventsSetVariableAction implements iotevents.IAction { + /** + * @param variableName the name of the variable + * @param value the new value of the variable + */ + constructor(private readonly variableName: string, private readonly value: iotevents.Expression) { + } + + bind(_scope: Construct, _options: iotevents.ActionBindOptions): iotevents.ActionConfig { + return { + configuration: { + setVariable: { + variableName: this.variableName, + value: this.value.evaluate(), + }, + }, + }; + } +} diff --git a/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.expected.json b/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.expected.json new file mode 100644 index 0000000000000..0119afc958c1f --- /dev/null +++ b/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.expected.json @@ -0,0 +1,95 @@ +{ + "Resources": { + "MyInput08947B23": { + "Type": "AWS::IoTEvents::Input", + "Properties": { + "InputDefinition": { + "Attributes": [ + { + "JsonPath": "payload.deviceId" + }, + { + "JsonPath": "payload.temperature" + } + ] + }, + "InputName": "test_input" + } + }, + "MyDetectorModelDetectorModelRoleF2FB4D88": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "iotevents.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "MyDetectorModel559C0B0E": { + "Type": "AWS::IoTEvents::DetectorModel", + "Properties": { + "DetectorModelDefinition": { + "InitialStateName": "MyState", + "States": [ + { + "OnEnter": { + "Events": [ + { + "Actions": [ + { + "SetVariable": { + "Value": { + "Fn::Join": [ + "", + [ + "$input.", + { + "Ref": "MyInput08947B23" + }, + ".payload.temperature" + ] + ] + }, + "VariableName": "MyVariable" + } + } + ], + "Condition": { + "Fn::Join": [ + "", + [ + "currentInput(\"", + { + "Ref": "MyInput08947B23" + }, + "\")" + ] + ] + }, + "EventName": "enter-event" + } + ] + }, + "StateName": "MyState" + } + ] + }, + "RoleArn": { + "Fn::GetAtt": [ + "MyDetectorModelDetectorModelRoleF2FB4D88", + "Arn" + ] + }, + "Key": "payload.deviceId" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.ts b/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.ts new file mode 100644 index 0000000000000..522699fda461a --- /dev/null +++ b/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.ts @@ -0,0 +1,42 @@ +/** + * Stack verification steps: + * * put a message + * * aws iotevents-data batch-put-message --messages=messageId=(date | md5),inputName=test_input,payload=(echo '{"payload":{"temperature":31.9,"deviceId":"000"}}' | base64) + */ +import * as iotevents from '@aws-cdk/aws-iotevents'; +import * as cdk from '@aws-cdk/core'; +import * as actions from '../../lib'; + +class TestStack extends cdk.Stack { + constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { + super(scope, id, props); + + const input = new iotevents.Input(this, 'MyInput', { + inputName: 'test_input', + attributeJsonPaths: ['payload.deviceId', 'payload.temperature'], + }); + + const state = new iotevents.State({ + stateName: 'MyState', + onEnter: [{ + eventName: 'enter-event', + condition: iotevents.Expression.currentInput(input), + actions: [ + new actions.IoteventsSetVariableAction( + 'MyVariable', + iotevents.Expression.inputAttribute(input, 'payload.temperature'), + ), + ], + }], + }); + + new iotevents.DetectorModel(this, 'MyDetectorModel', { + detectorKey: 'payload.deviceId', + initialState: state, + }); + } +} + +const app = new cdk.App(); +new TestStack(app, 'iotevents-set-variable-action-test-stack'); +app.synth(); diff --git a/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/iotevents-set-variable-action.test.ts b/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/iotevents-set-variable-action.test.ts new file mode 100644 index 0000000000000..cd01ec1f72ce0 --- /dev/null +++ b/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/iotevents-set-variable-action.test.ts @@ -0,0 +1,43 @@ +import { Template } from '@aws-cdk/assertions'; +import * as iotevents from '@aws-cdk/aws-iotevents'; +import * as cdk from '@aws-cdk/core'; +import * as actions from '../../lib'; + +let stack: cdk.Stack; +let input: iotevents.IInput; +beforeEach(() => { + stack = new cdk.Stack(); + input = iotevents.Input.fromInputName(stack, 'MyInput', 'test-input'); +}); + +test('Default property', () => { + // WHEN + new iotevents.DetectorModel(stack, 'MyDetectorModel', { + initialState: new iotevents.State({ + stateName: 'test-state', + onEnter: [{ + eventName: 'test-eventName', + condition: iotevents.Expression.currentInput(input), + actions: [new actions.IoteventsSetVariableAction('MyVariable', iotevents.Expression.fromString('foo'))], + }], + }), + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', { + DetectorModelDefinition: { + States: [{ + OnEnter: { + Events: [{ + Actions: [{ + SetVariable: { + VariableName: 'MyVariable', + Value: 'foo', + }, + }], + }], + }, + }], + }, + }); +}); From eb67d7dd31a109781446885cc821fc67dd394969 Mon Sep 17 00:00:00 2001 From: yamatatsu Date: Thu, 10 Mar 2022 19:27:24 +0900 Subject: [PATCH 2/3] address comment --- packages/@aws-cdk/aws-iotevents-actions/lib/index.ts | 2 +- ...{iotevents-set-variable-action.ts => set-variable-action.ts} | 2 +- .../integ.set-variable-action.expected.json} | 0 .../integ.set-variable-action.ts} | 2 +- .../set-variable-action.test.ts} | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename packages/@aws-cdk/aws-iotevents-actions/lib/{iotevents-set-variable-action.ts => set-variable-action.ts} (90%) rename packages/@aws-cdk/aws-iotevents-actions/test/{iotevents/integ.iotevents-set-variable-action.expected.json => iot/integ.set-variable-action.expected.json} (100%) rename packages/@aws-cdk/aws-iotevents-actions/test/{iotevents/integ.iotevents-set-variable-action.ts => iot/integ.set-variable-action.ts} (96%) rename packages/@aws-cdk/aws-iotevents-actions/test/{iotevents/iotevents-set-variable-action.test.ts => iot/set-variable-action.test.ts} (90%) diff --git a/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts b/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts index 7c2395d8ea8ea..e51394d301376 100644 --- a/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts +++ b/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts @@ -1,2 +1,2 @@ -export * from './iotevents-set-variable-action'; +export * from './set-variable-action'; export * from './lambda-invoke-action'; diff --git a/packages/@aws-cdk/aws-iotevents-actions/lib/iotevents-set-variable-action.ts b/packages/@aws-cdk/aws-iotevents-actions/lib/set-variable-action.ts similarity index 90% rename from packages/@aws-cdk/aws-iotevents-actions/lib/iotevents-set-variable-action.ts rename to packages/@aws-cdk/aws-iotevents-actions/lib/set-variable-action.ts index 5a2e49503d761..1d2596ddb866b 100644 --- a/packages/@aws-cdk/aws-iotevents-actions/lib/iotevents-set-variable-action.ts +++ b/packages/@aws-cdk/aws-iotevents-actions/lib/set-variable-action.ts @@ -4,7 +4,7 @@ import { Construct } from 'constructs'; /** * The action to create a variable with a specified value. */ -export class IoteventsSetVariableAction implements iotevents.IAction { +export class SetVariableAction implements iotevents.IAction { /** * @param variableName the name of the variable * @param value the new value of the variable diff --git a/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.expected.json b/packages/@aws-cdk/aws-iotevents-actions/test/iot/integ.set-variable-action.expected.json similarity index 100% rename from packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.expected.json rename to packages/@aws-cdk/aws-iotevents-actions/test/iot/integ.set-variable-action.expected.json diff --git a/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.ts b/packages/@aws-cdk/aws-iotevents-actions/test/iot/integ.set-variable-action.ts similarity index 96% rename from packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.ts rename to packages/@aws-cdk/aws-iotevents-actions/test/iot/integ.set-variable-action.ts index 522699fda461a..e6256e14710b2 100644 --- a/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/integ.iotevents-set-variable-action.ts +++ b/packages/@aws-cdk/aws-iotevents-actions/test/iot/integ.set-variable-action.ts @@ -22,7 +22,7 @@ class TestStack extends cdk.Stack { eventName: 'enter-event', condition: iotevents.Expression.currentInput(input), actions: [ - new actions.IoteventsSetVariableAction( + new actions.SetVariableAction( 'MyVariable', iotevents.Expression.inputAttribute(input, 'payload.temperature'), ), diff --git a/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/iotevents-set-variable-action.test.ts b/packages/@aws-cdk/aws-iotevents-actions/test/iot/set-variable-action.test.ts similarity index 90% rename from packages/@aws-cdk/aws-iotevents-actions/test/iotevents/iotevents-set-variable-action.test.ts rename to packages/@aws-cdk/aws-iotevents-actions/test/iot/set-variable-action.test.ts index cd01ec1f72ce0..1f779644b1a94 100644 --- a/packages/@aws-cdk/aws-iotevents-actions/test/iotevents/iotevents-set-variable-action.test.ts +++ b/packages/@aws-cdk/aws-iotevents-actions/test/iot/set-variable-action.test.ts @@ -18,7 +18,7 @@ test('Default property', () => { onEnter: [{ eventName: 'test-eventName', condition: iotevents.Expression.currentInput(input), - actions: [new actions.IoteventsSetVariableAction('MyVariable', iotevents.Expression.fromString('foo'))], + actions: [new actions.SetVariableAction('MyVariable', iotevents.Expression.fromString('foo'))], }], }), }); From f73f24edfc0b9ea3fe518d47d630b394d4e03504 Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Thu, 10 Mar 2022 17:20:48 -0800 Subject: [PATCH 3/3] Update packages/@aws-cdk/aws-iotevents-actions/README.md --- packages/@aws-cdk/aws-iotevents-actions/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-iotevents-actions/README.md b/packages/@aws-cdk/aws-iotevents-actions/README.md index eb5430fa064ae..486ddd8124c34 100644 --- a/packages/@aws-cdk/aws-iotevents-actions/README.md +++ b/packages/@aws-cdk/aws-iotevents-actions/README.md @@ -45,7 +45,7 @@ const state = new iotevents.State({ condition: iotevents.Expression.currentInput(input), actions: [ actions: [ - new actions.IoteventsSetVariableAction( + new actions.SetVariableAction( 'MyVariable', iotevents.Expression.inputAttribute(input, 'payload.temperature'), ),