-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iotevents): add DetectorModel L2 Construct
- Loading branch information
Showing
8 changed files
with
282 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import { Resource } from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import { CfnDetectorModel } from './iotevents.generated'; | ||
import { State } from './state'; | ||
|
||
/** | ||
* Properties for defining an AWS IoT Events detector model | ||
*/ | ||
export interface DetectorModelProps { | ||
/** | ||
* The name of the detector model | ||
* | ||
* @default - CloudFormation will generate a unique name of the detector model | ||
*/ | ||
readonly detectorModelName?: string, | ||
|
||
/** | ||
* The state that is entered at the creation of each detector. | ||
*/ | ||
readonly initialState: State; | ||
} | ||
|
||
/** | ||
* Defines an AWS IoT Events detector model in this stack. | ||
*/ | ||
export class DetectorModel extends Resource { | ||
constructor(scope: Construct, id: string, props: DetectorModelProps) { | ||
super(scope, id, { | ||
physicalName: props.detectorModelName, | ||
}); | ||
|
||
const role = new iam.Role(this, 'DetectorModelRole', { | ||
assumedBy: new iam.ServicePrincipal('iotevents.amazonaws.com'), | ||
}); | ||
|
||
new CfnDetectorModel(this, 'Resource', { | ||
detectorModelName: this.physicalName, | ||
detectorModelDefinition: { | ||
initialStateName: props.initialState.stateName, | ||
states: [props.initialState.toStateJson()], | ||
}, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from './input'; | ||
export * from './detector-model'; | ||
export * from './state'; | ||
|
||
// AWS::IoTEvents CloudFormation Resources: | ||
export * from './iotevents.generated'; |
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,60 @@ | ||
import { CfnDetectorModel } from './iotevents.generated'; | ||
|
||
/** | ||
* Specifies the actions to be performed when the condition evaluates to TRUE. | ||
*/ | ||
export interface Event { | ||
/** | ||
* The name of the event | ||
*/ | ||
readonly eventName: string; | ||
|
||
/** | ||
* The Boolean expression that, when TRUE, causes the actions to be performed. | ||
* | ||
* @default None - Defaults to perform the actions always. | ||
*/ | ||
readonly condition?: string; | ||
} | ||
|
||
/** | ||
* Properties for defining a state of a detector | ||
*/ | ||
export interface StateProps { | ||
/** | ||
* The name of the state | ||
*/ | ||
readonly stateName: string | ||
|
||
/** | ||
* Specifies the actions that are performed when the state is entered and the `condition` is `TRUE` | ||
* | ||
* @default None | ||
*/ | ||
readonly onEnterEvents?: Event[] | ||
} | ||
|
||
/** | ||
* Defines a state of a detector | ||
*/ | ||
export class State { | ||
/** | ||
* The name of the state | ||
*/ | ||
public readonly stateName: string; | ||
|
||
constructor(private readonly props: StateProps) { | ||
this.stateName = props.stateName; | ||
} | ||
|
||
/** | ||
* Return the state property JSON | ||
*/ | ||
public toStateJson(): CfnDetectorModel.StateProperty { | ||
const { stateName, onEnterEvents } = this.props; | ||
return { | ||
stateName, | ||
onEnter: onEnterEvents && { events: onEnterEvents }, | ||
}; | ||
} | ||
} |
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
83 changes: 83 additions & 0 deletions
83
packages/@aws-cdk/aws-iotevents/test/detector-model.test.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,83 @@ | ||
import { Match, Template } from '@aws-cdk/assertions'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as iotevents from '../lib'; | ||
|
||
test('Default property', () => { | ||
const stack = new cdk.Stack(); | ||
|
||
// WHEN | ||
new iotevents.DetectorModel(stack, 'MyDetectorModel', { | ||
initialState: new iotevents.State({ | ||
stateName: 'test-state', | ||
}), | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', { | ||
DetectorModelDefinition: { | ||
InitialStateName: 'test-state', | ||
States: [{ | ||
StateName: 'test-state', | ||
}], | ||
}, | ||
RoleArn: { | ||
'Fn::GetAtt': ['MyDetectorModelDetectorModelRoleF2FB4D88', 'Arn'], | ||
}, | ||
}); | ||
|
||
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', { | ||
AssumeRolePolicyDocument: { | ||
Statement: [{ | ||
Action: 'sts:AssumeRole', | ||
Effect: 'Allow', | ||
Principal: { Service: 'iotevents.amazonaws.com' }, | ||
}], | ||
}, | ||
}); | ||
}); | ||
|
||
test('can set physical name', () => { | ||
const stack = new cdk.Stack(); | ||
|
||
// WHEN | ||
new iotevents.DetectorModel(stack, 'MyDetectorModel', { | ||
detectorModelName: 'test-detector-model', | ||
initialState: new iotevents.State({ stateName: 'test-state' }), | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', { | ||
DetectorModelName: 'test-detector-model', | ||
}); | ||
}); | ||
|
||
test('can set onEnterEvents', () => { | ||
const stack = new cdk.Stack(); | ||
|
||
// WHEN | ||
new iotevents.DetectorModel(stack, 'MyDetectorModel', { | ||
initialState: new iotevents.State({ | ||
stateName: 'test-state', | ||
onEnterEvents: [{ | ||
eventName: 'test-eventName', | ||
condition: 'test-eventCondition', | ||
}], | ||
}), | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', { | ||
DetectorModelDefinition: { | ||
States: [ | ||
Match.objectLike({ | ||
OnEnter: { | ||
Events: [{ | ||
EventName: 'test-eventName', | ||
Condition: 'test-eventCondition', | ||
}], | ||
}, | ||
}), | ||
], | ||
}, | ||
}); | ||
}); |
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