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(iotevents): add IoT Events input L2 Construct (aws#17847)
This is proposed by aws#17711. This PR was created for implemeting `Input` L2 Construct. Implementing it is needed before `DetectorModel`. The reason is described in here: aws#17711 (comment) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
8 changed files
with
221 additions
and
16 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './input'; | ||
|
||
// 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,71 @@ | ||
import { Resource, IResource } from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import { CfnInput } from './iotevents.generated'; | ||
|
||
/** | ||
* Represents an AWS IoT Events input | ||
*/ | ||
export interface IInput extends IResource { | ||
/** | ||
* The name of the input | ||
* @attribute | ||
*/ | ||
readonly inputName: string; | ||
} | ||
|
||
/** | ||
* Properties for defining an AWS IoT Events input | ||
*/ | ||
export interface InputProps { | ||
/** | ||
* The name of the input | ||
* | ||
* @default - CloudFormation will generate a unique name of the input | ||
*/ | ||
readonly inputName?: string, | ||
|
||
/** | ||
* An expression that specifies an attribute-value pair in a JSON structure. | ||
* Use this to specify an attribute from the JSON payload that is made available | ||
* by the input. Inputs are derived from messages sent to AWS IoT Events (BatchPutMessage). | ||
* Each such message contains a JSON payload. The attribute (and its paired value) | ||
* specified here are available for use in the condition expressions used by detectors. | ||
*/ | ||
readonly attributeJsonPaths: string[]; | ||
} | ||
|
||
/** | ||
* Defines an AWS IoT Events input in this stack. | ||
*/ | ||
export class Input extends Resource implements IInput { | ||
/** | ||
* Import an existing input | ||
*/ | ||
public static fromInputName(scope: Construct, id: string, inputName: string): IInput { | ||
class Import extends Resource implements IInput { | ||
public readonly inputName = inputName; | ||
} | ||
return new Import(scope, id); | ||
} | ||
|
||
public readonly inputName: string; | ||
|
||
constructor(scope: Construct, id: string, props: InputProps) { | ||
super(scope, id, { | ||
physicalName: props.inputName, | ||
}); | ||
|
||
if (props.attributeJsonPaths.length === 0) { | ||
throw new Error('attributeJsonPaths property cannot be empty'); | ||
} | ||
|
||
const resource = new CfnInput(this, 'Resource', { | ||
inputName: this.physicalName, | ||
inputDefinition: { | ||
attributes: props.attributeJsonPaths.map(path => ({ jsonPath: path })), | ||
}, | ||
}); | ||
|
||
this.inputName = this.getResourceNameAttribute(resource.ref); | ||
} | ||
} |
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,78 @@ | ||
import { 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.Input(stack, 'MyInput', { | ||
attributeJsonPaths: ['payload.temperature'], | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::Input', { | ||
InputDefinition: { | ||
Attributes: [{ JsonPath: 'payload.temperature' }], | ||
}, | ||
}); | ||
}); | ||
|
||
test('can get input name', () => { | ||
const stack = new cdk.Stack(); | ||
// GIVEN | ||
const input = new iotevents.Input(stack, 'MyInput', { | ||
attributeJsonPaths: ['payload.temperature'], | ||
}); | ||
|
||
// WHEN | ||
new cdk.CfnResource(stack, 'Res', { | ||
type: 'Test::Resource', | ||
properties: { | ||
InputName: input.inputName, | ||
}, | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('Test::Resource', { | ||
InputName: { Ref: 'MyInput08947B23' }, | ||
}); | ||
}); | ||
|
||
test('can set physical name', () => { | ||
const stack = new cdk.Stack(); | ||
|
||
// WHEN | ||
new iotevents.Input(stack, 'MyInput', { | ||
inputName: 'test_input', | ||
attributeJsonPaths: ['payload.temperature'], | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::Input', { | ||
InputName: 'test_input', | ||
}); | ||
}); | ||
|
||
test('can import a Input by inputName', () => { | ||
const stack = new cdk.Stack(); | ||
|
||
// WHEN | ||
const inputName = 'test-input-name'; | ||
const topicRule = iotevents.Input.fromInputName(stack, 'InputFromInputName', inputName); | ||
|
||
// THEN | ||
expect(topicRule).toMatchObject({ | ||
inputName, | ||
}); | ||
}); | ||
|
||
test('cannot be created with an empty array of attributeJsonPaths', () => { | ||
const stack = new cdk.Stack(); | ||
|
||
expect(() => { | ||
new iotevents.Input(stack, 'MyInput', { | ||
attributeJsonPaths: [], | ||
}); | ||
}).toThrow('attributeJsonPaths property cannot be empty'); | ||
}); |
17 changes: 17 additions & 0 deletions
17
packages/@aws-cdk/aws-iotevents/test/integ.detector-model.expected.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,17 @@ | ||
{ | ||
"Resources": { | ||
"MyInput08947B23": { | ||
"Type": "AWS::IoTEvents::Input", | ||
"Properties": { | ||
"InputDefinition": { | ||
"Attributes": [ | ||
{ | ||
"JsonPath": "payload.temperature" | ||
} | ||
] | ||
}, | ||
"InputName": "test_input" | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/@aws-cdk/aws-iotevents/test/integ.detector-model.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,18 @@ | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as iotevents from '../lib'; | ||
|
||
const app = new cdk.App(); | ||
|
||
class TestStack extends cdk.Stack { | ||
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { | ||
super(scope, id, props); | ||
|
||
new iotevents.Input(this, 'MyInput', { | ||
inputName: 'test_input', | ||
attributeJsonPaths: ['payload.temperature'], | ||
}); | ||
} | ||
} | ||
|
||
new TestStack(app, 'test-stack'); | ||
app.synth(); |
This file was deleted.
Oops, something went wrong.