-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(iotevents): add IoT Events input L2 Construct #17847
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3e069e9
feat(iotevents): add IoT Events input L2 Construct
yamatatsu 4cda347
add test for getting inputName
yamatatsu 0f15502
add IInput
yamatatsu a9ddebb
add at-default comment
yamatatsu d1adc50
add a validation for empty json path
yamatatsu ed80896
Update docs of the inputName property
skinny85 fb25883
Merge branch 'master' into iotevents-input
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can
attributeJsonPaths
be empty? If not, can we validate that it's not? If it can be empty, can we make it optional, and default to[]
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you said,
attributeJsonPaths
can not be empty. So I've been thinking that it is better to validate it too!On the other hand, I thought I had to make my initial L2 PR smaller so as not to repeat my previous mistakes😅 .
I never mind which ways, implement in this PR or after!😉I've fixed it!