-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(stepfunctions): add support for Map state #4145
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0b88229
AWS Step Functions: Add support for Map state, add test for Parallel …
eb55229
removing trailing whitespace
d3e7686
Merge branch 'master' into master
5e2355f
Change sequential to maxConcurrency
rix0rrr 1a4578d
Merge remote-tracking branch 'origin/master' into pr/melalawi/4145
rix0rrr 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 hidden or 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 hidden or 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,164 @@ | ||
import cdk = require('@aws-cdk/core'); | ||
import { Chain } from '../chain'; | ||
import { StateGraph } from '../state-graph'; | ||
import { CatchProps, IChainable, INextable, RetryProps } from '../types'; | ||
import { StateType } from './private/state-type'; | ||
import { renderJsonPath, State } from './state'; | ||
|
||
/** | ||
* Properties for defining a Map state | ||
*/ | ||
export interface MapProps { | ||
/** | ||
* An optional description for this state | ||
* | ||
* @default No comment | ||
*/ | ||
readonly comment?: string; | ||
|
||
/** | ||
* JSONPath expression to select part of the state to be the input to this state. | ||
* | ||
* May also be the special value DISCARD, which will cause the effective | ||
* input to be the empty object {}. | ||
* | ||
* @default $ | ||
*/ | ||
readonly inputPath?: string; | ||
|
||
/** | ||
* JSONPath expression to select part of the state to be the output to this state. | ||
* | ||
* May also be the special value DISCARD, which will cause the effective | ||
* output to be the empty object {}. | ||
* | ||
* @default $ | ||
*/ | ||
readonly outputPath?: string; | ||
|
||
/** | ||
* JSONPath expression to indicate where to inject the state's output | ||
* | ||
* May also be the special value DISCARD, which will cause the state's | ||
* input to become its output. | ||
* | ||
* @default $ | ||
*/ | ||
readonly resultPath?: string; | ||
|
||
/** | ||
* JSONPath expression to select the array to iterate over | ||
* | ||
* @default $ | ||
*/ | ||
readonly itemsPath?: string; | ||
|
||
/** | ||
* The JSON that you want to override your default iteration input | ||
* | ||
* @default $ | ||
*/ | ||
readonly parameters?: { [key: string]: any }; | ||
|
||
/** | ||
* MaxConcurrency | ||
* | ||
* An upper bound on the number of iterations you want running at once. | ||
* | ||
* @default - full concurrency | ||
*/ | ||
readonly maxConcurrency?: number; | ||
} | ||
|
||
/** | ||
* Define a Map state in the state machine | ||
* | ||
* A Map state can be used to dynamically process elements of an array through sub state machines | ||
* | ||
* The Result of a Map state is the transformed array after processing through the iterator state machines. | ||
*/ | ||
export class Map extends State implements INextable { | ||
public readonly endStates: INextable[]; | ||
|
||
private readonly maxConcurrency?: number; | ||
private readonly itemsPath?: string; | ||
|
||
constructor(scope: cdk.Construct, id: string, props: MapProps = {}) { | ||
super(scope, id, props); | ||
this.endStates = [this]; | ||
this.maxConcurrency = props.maxConcurrency; | ||
this.itemsPath = props.itemsPath; | ||
} | ||
|
||
/** | ||
* Add retry configuration for this state | ||
* | ||
* This controls if and how the execution will be retried if a particular | ||
* error occurs. | ||
*/ | ||
public addRetry(props: RetryProps = {}): Map { | ||
super._addRetry(props); | ||
return this; | ||
} | ||
|
||
/** | ||
* Add a recovery handler for this state | ||
* | ||
* When a particular error occurs, execution will continue at the error | ||
* handler instead of failing the state machine execution. | ||
*/ | ||
public addCatch(handler: IChainable, props: CatchProps = {}): Map { | ||
super._addCatch(handler.startState, props); | ||
return this; | ||
} | ||
|
||
/** | ||
* Continue normal execution with the given state | ||
*/ | ||
public next(next: IChainable): Chain { | ||
super.makeNext(next.startState); | ||
return Chain.sequence(this, next); | ||
} | ||
|
||
/** | ||
* Define iterator state machine in Map | ||
*/ | ||
public iterator(iterator: IChainable): Map { | ||
const name = `Map ${this.stateId} Iterator`; | ||
super.addIterator(new StateGraph(iterator.startState, name)); | ||
return this; | ||
} | ||
|
||
/** | ||
* Return the Amazon States Language object for this state | ||
*/ | ||
public toStateJson(): object { | ||
return { | ||
Type: StateType.MAP, | ||
Comment: this.comment, | ||
ResultPath: renderJsonPath(this.resultPath), | ||
...this.renderNextEnd(), | ||
...this.renderInputOutput(), | ||
...this.renderRetryCatch(), | ||
...this.renderIterator(), | ||
...this.renderItemsPath(), | ||
MaxConcurrency: this.maxConcurrency | ||
}; | ||
} | ||
|
||
/** | ||
* Validate this state | ||
*/ | ||
protected validate(): string[] { | ||
if (!!this.iterator) { | ||
return ['Map state must have a non-empty iterator']; | ||
} | ||
return []; | ||
} | ||
|
||
private renderItemsPath(): any { | ||
return { | ||
ItemsPath: renderJsonPath(this.itemsPath) | ||
}; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,50 @@ | ||
import cdk = require('@aws-cdk/core'); | ||
import { Test } from 'nodeunit'; | ||
import stepfunctions = require('../lib'); | ||
|
||
export = { | ||
'State Machine With Map State'(test: Test) { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
|
||
// WHEN | ||
const map = new stepfunctions.Map(stack, 'Map State', { | ||
maxConcurrency: 1, | ||
itemsPath: stepfunctions.Data.stringAt('$.inputForMap'), | ||
parameters: { | ||
foo: 'foo', | ||
bar: stepfunctions.Data.stringAt('$.bar') | ||
} | ||
}); | ||
map.iterator(new stepfunctions.Pass(stack, 'Pass State')); | ||
|
||
// THEN | ||
test.deepEqual(render(map), { | ||
StartAt: 'Map State', | ||
States: { | ||
'Map State': { | ||
Type: 'Map', | ||
End: true, | ||
Parameters: { foo: 'foo', bar: '$.bar' }, | ||
Iterator: { | ||
StartAt: 'Pass State', | ||
States: { | ||
'Pass State': { | ||
Type: 'Pass', | ||
End: true | ||
} | ||
} | ||
}, | ||
ItemsPath: '$.inputForMap', | ||
MaxConcurrency: 1 | ||
} | ||
} | ||
}); | ||
|
||
test.done(); | ||
} | ||
}; | ||
|
||
function render(sm: stepfunctions.IChainable) { | ||
return new cdk.Stack().resolve(new stepfunctions.StateGraph(sm.startState, 'Test Graph').toGraphJson()); | ||
} |
This file contains hidden or 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,36 @@ | ||
import cdk = require('@aws-cdk/core'); | ||
import { Test } from 'nodeunit'; | ||
import stepfunctions = require('../lib'); | ||
|
||
export = { | ||
'State Machine With Parallel State'(test: Test) { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
|
||
// WHEN | ||
const parallel = new stepfunctions.Parallel(stack, 'Parallel State'); | ||
parallel.branch(new stepfunctions.Pass(stack, 'Branch 1')); | ||
parallel.branch(new stepfunctions.Pass(stack, 'Branch 2')); | ||
|
||
// THEN | ||
test.deepEqual(render(parallel), { | ||
StartAt: 'Parallel State', | ||
States: { | ||
'Parallel State': { | ||
Type: 'Parallel', | ||
End: true, | ||
Branches: [ | ||
{ StartAt: 'Branch 1', States: { 'Branch 1': { Type: 'Pass', End: true } } }, | ||
{ StartAt: 'Branch 2', States: { 'Branch 2': { Type: 'Pass', End: true } } } | ||
] | ||
} | ||
} | ||
}); | ||
|
||
test.done(); | ||
} | ||
}; | ||
|
||
function render(sm: stepfunctions.IChainable) { | ||
return new cdk.Stack().resolve(new stepfunctions.StateGraph(sm.startState, 'Test Graph').toGraphJson()); | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.