From 7c4f83eec0c78921ef0ef9dcfcd22f6c2b0754da Mon Sep 17 00:00:00 2001 From: SankyRed Date: Tue, 26 Sep 2023 22:47:35 -0500 Subject: [PATCH 1/7] Adding custom state name prop --- .../aws-cdk-lib/aws-stepfunctions/lib/state-machine.ts | 2 +- .../aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts | 6 ++++++ .../aws-stepfunctions/lib/states/custom-state.ts | 1 + packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts | 6 ++++++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts | 6 ++++++ .../aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts | 6 ++++++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts | 6 ++++++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts | 7 +++++++ .../aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts | 6 ++++++ .../aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts | 6 ++++++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts | 6 ++++++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts | 6 ++++++ .../aws-stepfunctions/test/custom-state.test.ts | 1 + .../aws-stepfunctions/test/state-machine-resources.test.ts | 3 +++ packages/aws-cdk-lib/aws-stepfunctions/test/state.test.ts | 2 ++ 15 files changed, 69 insertions(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/state-machine.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/state-machine.ts index 5c946704f1dc0..d3670e9775ec6 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/state-machine.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/state-machine.ts @@ -101,7 +101,7 @@ export interface StateMachineProps { readonly definitionBody?: DefinitionBody; /** - * substitutions for the definition body aas a key-value map + * substitutions for the definition body as a key-value map */ readonly definitionSubstitutions?: { [key: string]: string }; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts index 55db170551b34..f5447d92eae8b 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts @@ -9,6 +9,11 @@ import { IChainable, INextable } from '../types'; * Properties for defining a Choice state */ export interface ChoiceProps { + /** + * Optional name for this state + */ + readonly stateName?: string; + /** * An optional description for this state * @@ -91,6 +96,7 @@ export class Choice extends State { public toStateJson(): object { return { Type: StateType.CHOICE, + StateName: this.stateName, Comment: this.comment, ...this.renderInputOutput(), ...this.renderChoices(), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts index 3bf14fc249b80..2579f24e6231b 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts @@ -47,6 +47,7 @@ export class CustomState extends State implements IChainable, INextable { */ public toStateJson(): object { return { + StateName: this.stateName, ...this.renderNextEnd(), ...this.stateJson, }; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts index 8ec9a35145020..c8ac95a74e25f 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts @@ -7,6 +7,11 @@ import { INextable } from '../types'; * Properties for defining a Fail state */ export interface FailProps { + /** + * Optional name for this state + */ + readonly stateName?: string; + /** * An optional description for this state * @@ -71,6 +76,7 @@ export class Fail extends State { public toStateJson(): object { return { Type: StateType.FAIL, + StateName: this.stateName, Comment: this.comment, Error: this.error, ErrorPath: renderJsonPath(this.errorPath), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts index 4777b0d3fddbd..41e9eb4e8d365 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts @@ -11,6 +11,11 @@ import { CatchProps, IChainable, INextable, RetryProps } from '../types'; * Properties for defining a Map state */ export interface MapProps { + /** + * Optional name for this state + */ + readonly stateName?: string; + /** * An optional description for this state * @@ -168,6 +173,7 @@ export class Map extends State implements INextable { public toStateJson(): object { return { Type: StateType.MAP, + StateName: this.stateName, Comment: this.comment, ResultPath: renderJsonPath(this.resultPath), ...this.renderNextEnd(), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts index 1656392b1eff1..d7707942fb222 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts @@ -9,6 +9,11 @@ import { CatchProps, IChainable, INextable, RetryProps } from '../types'; * Properties for defining a Parallel state */ export interface ParallelProps { + /** + * Optional name for this state + */ + readonly stateName?: string; + /** * An optional description for this state * @@ -138,6 +143,7 @@ export class Parallel extends State implements INextable { public toStateJson(): object { return { Type: StateType.PARALLEL, + StateName: this.stateName, Comment: this.comment, ResultPath: renderJsonPath(this.resultPath), ...this.renderNextEnd(), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts index f1b6459bf2225..c50282d23b533 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts @@ -56,6 +56,11 @@ export class Result { * Properties for defining a Pass state */ export interface PassProps { + /** + * Optional name for this state + */ + readonly stateName?: string; + /** * An optional description for this state * @@ -144,6 +149,7 @@ export class Pass extends State implements INextable { public toStateJson(): object { return { Type: StateType.PASS, + StateName: this.stateName, Comment: this.comment, Result: this.result?.value, ResultPath: renderJsonPath(this.resultPath), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts index 64e7952260e46..9005bbe23ba52 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts @@ -9,6 +9,11 @@ import { CatchProps, Errors, IChainable, INextable, RetryProps } from '../types' * Properties shared by all states */ export interface StateProps { + /** + * Optional name for this state + */ + readonly stateName?: string; + /** * A comment describing this state * @@ -155,6 +160,7 @@ export abstract class State extends Construct implements IChainable { // features are shared by a couple of states, and it becomes cumbersome to // slice it out across all states. This is not great design, but it is // pragmatic! + protected readonly stateName?: string; protected readonly comment?: string; protected readonly inputPath?: string; protected readonly parameters?: object; @@ -194,6 +200,7 @@ export abstract class State extends Construct implements IChainable { this.startState = this; + this.stateName = props.stateName; this.comment = props.comment; this.inputPath = props.inputPath; this.parameters = props.parameters; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts index 4ef104b262e68..8b38bd798d38a 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts @@ -7,6 +7,11 @@ import { INextable } from '../types'; * Properties for defining a Succeed state */ export interface SucceedProps { + /** + * Optional name for this state + */ + readonly stateName?: string; + /** * An optional description for this state * @@ -54,6 +59,7 @@ export class Succeed extends State { public toStateJson(): object { return { Type: StateType.SUCCEED, + StateName: this.stateName, Comment: this.comment, ...this.renderInputOutput(), }; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts index f64dad6b799b0..6e147fd0999e2 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts @@ -13,6 +13,11 @@ import { CatchProps, IChainable, INextable, RetryProps } from '../types'; * Props that are common to all tasks */ export interface TaskStateBaseProps { + /** + * Optional name for this state + */ + readonly stateName?: string; + /** * An optional description for this state * @@ -331,6 +336,7 @@ export abstract class TaskStateBase extends State implements INextable { private renderTaskBase() { return { Type: 'Task', + StateName: this.stateName, Comment: this.comment, TimeoutSeconds: this.timeout?.toSeconds() ?? this.taskTimeout?.seconds, TimeoutSecondsPath: this.taskTimeout?.path, diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts index 7d50ba3556c3e..524cdbecdc7f5 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts @@ -21,6 +21,11 @@ export interface TaskProps { */ readonly task: IStepFunctionsTask; + /** + * Optional name for this state + */ + readonly stateName?: string; + /** * An optional description for this state * @@ -161,6 +166,7 @@ export class Task extends State implements INextable { ...this.renderRetryCatch(), ...this.renderInputOutput(), Type: StateType.TASK, + StateName: this.stateName, Comment: this.comment, Resource: this.taskProps.resourceArn, Parameters: this.taskProps.parameters && FieldUtils.renderObject(this.taskProps.parameters), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts index 9a33c498f5357..1299b21dd1485 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts @@ -50,6 +50,11 @@ export class WaitTime { * Properties for defining a Wait state */ export interface WaitProps { + /** + * Optional name for this state + */ + readonly stateName?: string; + /** * An optional description for this state * @@ -94,6 +99,7 @@ export class Wait extends State implements INextable { public toStateJson(): object { return { Type: StateType.WAIT, + StateName: this.stateName, Comment: this.comment, ...this.time._json, ...this.renderNextEnd(), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts index daa069b15e37d..bfe4d2dc66ee6 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts @@ -32,6 +32,7 @@ describe('Custom State', () => { // THEN expect(customState.toStateJson()).toStrictEqual({ + StateName: undefined, ...stateJson, End: true, }); diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts index d9ebf9219f9a3..aaf2436275d85 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts @@ -133,6 +133,7 @@ describe('State Machine Resources', () => { 'booleanArgument': true, 'arrayArgument': ['a', 'b', 'c'], }, + StateName: undefined, OutputPath: '$.state', Type: 'Task', Comment: undefined, @@ -179,6 +180,7 @@ describe('State Machine Resources', () => { OutputPath: '$.state', Type: 'Task', Comment: undefined, + StateName: undefined, Resource: 'resource', ResultPath: undefined, TimeoutSeconds: undefined, @@ -670,6 +672,7 @@ describe('State Machine Resources', () => { 'arrayArgument': ['a', 'b', 'c'], }, Type: 'Pass', + StateName: undefined, Comment: undefined, Result: undefined, ResultPath: undefined, diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/state.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/state.test.ts index bfa29d0fd032b..1ff9703238289 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/state.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/state.test.ts @@ -8,6 +8,7 @@ test('JsonPath.DISCARD can be used to discard a state\'s output', () => { const app = new cdk.App(); const stack = new cdk.Stack(app, 'TestStack'); const task = new FakeTask(stack, 'my-state', { + stateName: 'new-faketask-state', inputPath: JsonPath.DISCARD, outputPath: JsonPath.DISCARD, resultPath: JsonPath.DISCARD, @@ -28,6 +29,7 @@ test('JsonPath.DISCARD can be used to discard a state\'s output', () => { expect(definition).toMatchObject({ States: { 'my-state': { + StateName: 'new-faketask-state', InputPath: null, OutputPath: null, ResultPath: null, From 63b9fa69719c9a9ae0899e39e3d42b4a49a9490c Mon Sep 17 00:00:00 2001 From: SankyRed Date: Wed, 27 Sep 2023 08:50:22 -0500 Subject: [PATCH 2/7] Fixing the default values for state name --- packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts | 2 ++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts | 2 ++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts | 2 ++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts | 2 ++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts | 2 ++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts | 2 ++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts | 2 ++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts | 2 ++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts | 2 ++ packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts | 2 ++ 10 files changed, 20 insertions(+) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts index f5447d92eae8b..507cfadbfed17 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts @@ -11,6 +11,8 @@ import { IChainable, INextable } from '../types'; export interface ChoiceProps { /** * Optional name for this state + * + * @default LogicalResourceId */ readonly stateName?: string; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts index c8ac95a74e25f..691ea85ea1251 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts @@ -9,6 +9,8 @@ import { INextable } from '../types'; export interface FailProps { /** * Optional name for this state + * + * @default LogicalResourceId */ readonly stateName?: string; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts index 41e9eb4e8d365..b8f96bfbb08d5 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts @@ -13,6 +13,8 @@ import { CatchProps, IChainable, INextable, RetryProps } from '../types'; export interface MapProps { /** * Optional name for this state + * + * @default LogicalResourceId */ readonly stateName?: string; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts index d7707942fb222..9db28b8af9521 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts @@ -11,6 +11,8 @@ import { CatchProps, IChainable, INextable, RetryProps } from '../types'; export interface ParallelProps { /** * Optional name for this state + * + * @default LogicalResourceId */ readonly stateName?: string; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts index c50282d23b533..3ab7690d9e304 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts @@ -58,6 +58,8 @@ export class Result { export interface PassProps { /** * Optional name for this state + * + * @default LogicalResourceId */ readonly stateName?: string; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts index 9005bbe23ba52..ecb8dd52ec7c3 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts @@ -11,6 +11,8 @@ import { CatchProps, Errors, IChainable, INextable, RetryProps } from '../types' export interface StateProps { /** * Optional name for this state + * + * @default LogicalResourceId */ readonly stateName?: string; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts index 8b38bd798d38a..cfb91e90cb5ef 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts @@ -9,6 +9,8 @@ import { INextable } from '../types'; export interface SucceedProps { /** * Optional name for this state + * + * @default LogicalResourceId */ readonly stateName?: string; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts index 6e147fd0999e2..d91d71a607693 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts @@ -15,6 +15,8 @@ import { CatchProps, IChainable, INextable, RetryProps } from '../types'; export interface TaskStateBaseProps { /** * Optional name for this state + * + * @default LogicalResourceId */ readonly stateName?: string; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts index 524cdbecdc7f5..3b270a315451b 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts @@ -23,6 +23,8 @@ export interface TaskProps { /** * Optional name for this state + * + * @default LogicalResourceId */ readonly stateName?: string; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts index 1299b21dd1485..8e672703b563e 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts @@ -52,6 +52,8 @@ export class WaitTime { export interface WaitProps { /** * Optional name for this state + * + * @default LogicalResourceId */ readonly stateName?: string; From b36689e3ef0c4e0d215e3228782a26fc96056c02 Mon Sep 17 00:00:00 2001 From: SankyRed Date: Wed, 27 Sep 2023 16:30:24 -0500 Subject: [PATCH 3/7] Updating the default value --- packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts | 3 +-- .../aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts | 1 - packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts | 3 +-- packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts | 3 +-- packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts | 3 +-- packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts | 3 +-- packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts | 3 +-- packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts | 3 +-- packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts | 3 +-- packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts | 3 +-- .../aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts | 1 - .../aws-stepfunctions/test/state-machine-resources.test.ts | 3 --- packages/aws-cdk-lib/aws-stepfunctions/test/state.test.ts | 2 -- 13 files changed, 9 insertions(+), 25 deletions(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts index 507cfadbfed17..1b35552b08a0b 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts @@ -12,7 +12,7 @@ export interface ChoiceProps { /** * Optional name for this state * - * @default LogicalResourceId + * @default - The construct ID will be used as state name */ readonly stateName?: string; @@ -98,7 +98,6 @@ export class Choice extends State { public toStateJson(): object { return { Type: StateType.CHOICE, - StateName: this.stateName, Comment: this.comment, ...this.renderInputOutput(), ...this.renderChoices(), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts index 2579f24e6231b..3bf14fc249b80 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts @@ -47,7 +47,6 @@ export class CustomState extends State implements IChainable, INextable { */ public toStateJson(): object { return { - StateName: this.stateName, ...this.renderNextEnd(), ...this.stateJson, }; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts index 691ea85ea1251..efe067c8ba3a5 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts @@ -10,7 +10,7 @@ export interface FailProps { /** * Optional name for this state * - * @default LogicalResourceId + * @default - The construct ID will be used as state name */ readonly stateName?: string; @@ -78,7 +78,6 @@ export class Fail extends State { public toStateJson(): object { return { Type: StateType.FAIL, - StateName: this.stateName, Comment: this.comment, Error: this.error, ErrorPath: renderJsonPath(this.errorPath), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts index b8f96bfbb08d5..b42de5fafee4c 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts @@ -14,7 +14,7 @@ export interface MapProps { /** * Optional name for this state * - * @default LogicalResourceId + * @default - The construct ID will be used as state name */ readonly stateName?: string; @@ -175,7 +175,6 @@ export class Map extends State implements INextable { public toStateJson(): object { return { Type: StateType.MAP, - StateName: this.stateName, Comment: this.comment, ResultPath: renderJsonPath(this.resultPath), ...this.renderNextEnd(), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts index 9db28b8af9521..1199f3d84e8b7 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts @@ -12,7 +12,7 @@ export interface ParallelProps { /** * Optional name for this state * - * @default LogicalResourceId + * @default - The construct ID will be used as state name */ readonly stateName?: string; @@ -145,7 +145,6 @@ export class Parallel extends State implements INextable { public toStateJson(): object { return { Type: StateType.PARALLEL, - StateName: this.stateName, Comment: this.comment, ResultPath: renderJsonPath(this.resultPath), ...this.renderNextEnd(), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts index 3ab7690d9e304..2a2a19db7d47c 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts @@ -59,7 +59,7 @@ export interface PassProps { /** * Optional name for this state * - * @default LogicalResourceId + * @default - The construct ID will be used as state name */ readonly stateName?: string; @@ -151,7 +151,6 @@ export class Pass extends State implements INextable { public toStateJson(): object { return { Type: StateType.PASS, - StateName: this.stateName, Comment: this.comment, Result: this.result?.value, ResultPath: renderJsonPath(this.resultPath), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts index cfb91e90cb5ef..80eab204832c4 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts @@ -10,7 +10,7 @@ export interface SucceedProps { /** * Optional name for this state * - * @default LogicalResourceId + * @default - The construct ID will be used as state name */ readonly stateName?: string; @@ -61,7 +61,6 @@ export class Succeed extends State { public toStateJson(): object { return { Type: StateType.SUCCEED, - StateName: this.stateName, Comment: this.comment, ...this.renderInputOutput(), }; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts index d91d71a607693..37db0828ab8d0 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts @@ -16,7 +16,7 @@ export interface TaskStateBaseProps { /** * Optional name for this state * - * @default LogicalResourceId + * @default - The construct ID will be used as state name */ readonly stateName?: string; @@ -338,7 +338,6 @@ export abstract class TaskStateBase extends State implements INextable { private renderTaskBase() { return { Type: 'Task', - StateName: this.stateName, Comment: this.comment, TimeoutSeconds: this.timeout?.toSeconds() ?? this.taskTimeout?.seconds, TimeoutSecondsPath: this.taskTimeout?.path, diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts index 3b270a315451b..0e43839b301eb 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts @@ -24,7 +24,7 @@ export interface TaskProps { /** * Optional name for this state * - * @default LogicalResourceId + * @default - The construct ID will be used as state name */ readonly stateName?: string; @@ -168,7 +168,6 @@ export class Task extends State implements INextable { ...this.renderRetryCatch(), ...this.renderInputOutput(), Type: StateType.TASK, - StateName: this.stateName, Comment: this.comment, Resource: this.taskProps.resourceArn, Parameters: this.taskProps.parameters && FieldUtils.renderObject(this.taskProps.parameters), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts index 8e672703b563e..d81042f1e2909 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts @@ -53,7 +53,7 @@ export interface WaitProps { /** * Optional name for this state * - * @default LogicalResourceId + * @default - The construct ID will be used as state name */ readonly stateName?: string; @@ -101,7 +101,6 @@ export class Wait extends State implements INextable { public toStateJson(): object { return { Type: StateType.WAIT, - StateName: this.stateName, Comment: this.comment, ...this.time._json, ...this.renderNextEnd(), diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts index bfe4d2dc66ee6..daa069b15e37d 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts @@ -32,7 +32,6 @@ describe('Custom State', () => { // THEN expect(customState.toStateJson()).toStrictEqual({ - StateName: undefined, ...stateJson, End: true, }); diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts index aaf2436275d85..d9ebf9219f9a3 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts @@ -133,7 +133,6 @@ describe('State Machine Resources', () => { 'booleanArgument': true, 'arrayArgument': ['a', 'b', 'c'], }, - StateName: undefined, OutputPath: '$.state', Type: 'Task', Comment: undefined, @@ -180,7 +179,6 @@ describe('State Machine Resources', () => { OutputPath: '$.state', Type: 'Task', Comment: undefined, - StateName: undefined, Resource: 'resource', ResultPath: undefined, TimeoutSeconds: undefined, @@ -672,7 +670,6 @@ describe('State Machine Resources', () => { 'arrayArgument': ['a', 'b', 'c'], }, Type: 'Pass', - StateName: undefined, Comment: undefined, Result: undefined, ResultPath: undefined, diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/state.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/state.test.ts index 1ff9703238289..bfa29d0fd032b 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/state.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/state.test.ts @@ -8,7 +8,6 @@ test('JsonPath.DISCARD can be used to discard a state\'s output', () => { const app = new cdk.App(); const stack = new cdk.Stack(app, 'TestStack'); const task = new FakeTask(stack, 'my-state', { - stateName: 'new-faketask-state', inputPath: JsonPath.DISCARD, outputPath: JsonPath.DISCARD, resultPath: JsonPath.DISCARD, @@ -29,7 +28,6 @@ test('JsonPath.DISCARD can be used to discard a state\'s output', () => { expect(definition).toMatchObject({ States: { 'my-state': { - StateName: 'new-faketask-state', InputPath: null, OutputPath: null, ResultPath: null, From 25586c5fe81eb13d4bd416422f19aeb6aa20586e Mon Sep 17 00:00:00 2001 From: SankyRed Date: Tue, 3 Oct 2023 10:21:20 -0500 Subject: [PATCH 4/7] Fix to use the custom state name if provided else default to the construct id --- .../aws-stepfunctions/lib/states/choice.ts | 2 +- .../lib/states/custom-state.ts | 9 ++- .../aws-stepfunctions/lib/states/fail.ts | 2 +- .../aws-stepfunctions/lib/states/map.ts | 2 +- .../aws-stepfunctions/lib/states/parallel.ts | 2 +- .../aws-stepfunctions/lib/states/pass.ts | 2 +- .../aws-stepfunctions/lib/states/state.ts | 4 +- .../aws-stepfunctions/lib/states/succeed.ts | 2 +- .../aws-stepfunctions/lib/states/task-base.ts | 2 +- .../aws-stepfunctions/lib/states/task.ts | 2 +- .../aws-stepfunctions/lib/states/wait.ts | 2 +- .../test/custom-state.test.ts | 8 ++- .../aws-stepfunctions/test/fail.test.ts | 62 ++++++++++++++++++- .../aws-stepfunctions/test/map.test.ts | 5 +- .../aws-stepfunctions/test/parallel.test.ts | 4 +- .../test/state-machine-resources.test.ts | 1 + .../aws-stepfunctions/test/wait.test.ts | 21 +++++++ 17 files changed, 111 insertions(+), 21 deletions(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts index 1b35552b08a0b..e524b35cdb684 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts @@ -54,7 +54,7 @@ export class Choice extends State { public readonly endStates: INextable[] = []; constructor(scope: Construct, id: string, props: ChoiceProps = {}) { - super(scope, id, props); + super(scope, props.stateName? props.stateName: id, props); } /** diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts index 3bf14fc249b80..a663c2a2b8d5e 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts @@ -7,6 +7,13 @@ import { IChainable, INextable } from '../types'; * Properties for defining a custom state definition */ export interface CustomStateProps { + /** + * Optional name for this state + * + * @default - The construct ID will be used as state name + */ + readonly stateName: string; + /** * Amazon States Language (JSON-based) definition of the state * @@ -28,7 +35,7 @@ export class CustomState extends State implements IChainable, INextable { private readonly stateJson: { [key: string]: any}; constructor(scope: Construct, id: string, props: CustomStateProps) { - super(scope, id, {}); + super(scope, props.stateName? props.stateName: id, {}); this.endStates = [this]; this.stateJson = props.stateJson; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts index efe067c8ba3a5..7517f241d6443 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts @@ -64,7 +64,7 @@ export class Fail extends State { private readonly causePath?: string; constructor(scope: Construct, id: string, props: FailProps = {}) { - super(scope, id, props); + super(scope, props.stateName? props.stateName: id, props); this.error = props.error; this.errorPath = props.errorPath; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts index b42de5fafee4c..b4a8af86e1876 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts @@ -124,7 +124,7 @@ export class Map extends State implements INextable { private readonly itemsPath?: string; constructor(scope: Construct, id: string, props: MapProps = {}) { - super(scope, id, props); + super(scope, props.stateName? props.stateName: id, props); this.endStates = [this]; this.maxConcurrency = props.maxConcurrency; this.itemsPath = props.itemsPath; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts index 1199f3d84e8b7..24fdf2a02cd7d 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts @@ -81,7 +81,7 @@ export class Parallel extends State implements INextable { private readonly _branches: IChainable[] = []; constructor(scope: Construct, id: string, props: ParallelProps = {}) { - super(scope, id, props); + super(scope, props.stateName? props.stateName: id, props); this.endStates = [this]; } diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts index 2a2a19db7d47c..26b8b640b003a 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts @@ -131,7 +131,7 @@ export class Pass extends State implements INextable { private readonly result?: Result; constructor(scope: Construct, id: string, props: PassProps = {}) { - super(scope, id, props); + super(scope, props.stateName? props.stateName: id, props); this.result = props.result; this.endStates = [this]; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts index ecb8dd52ec7c3..eac5f343b541e 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts @@ -12,7 +12,7 @@ export interface StateProps { /** * Optional name for this state * - * @default LogicalResourceId + * @default - The construct ID will be used as state name */ readonly stateName?: string; @@ -198,7 +198,7 @@ export abstract class State extends Construct implements IChainable { private readonly incomingStates: State[] = []; constructor(scope: Construct, id: string, props: StateProps) { - super(scope, id); + super(scope, props.stateName? props.stateName: id); this.startState = this; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts index 80eab204832c4..6ed2d1a7d1899 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts @@ -52,7 +52,7 @@ export class Succeed extends State { public readonly endStates: INextable[] = []; constructor(scope: Construct, id: string, props: SucceedProps = {}) { - super(scope, id, props); + super(scope, props.stateName? props.stateName: id, props); } /** diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts index 37db0828ab8d0..add498ad7bba7 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts @@ -157,7 +157,7 @@ export abstract class TaskStateBase extends State implements INextable { private readonly credentials?: Credentials; constructor(scope: Construct, id: string, props: TaskStateBaseProps) { - super(scope, id, props); + super(scope, props.stateName ? props.stateName: id, props); this.endStates = [this]; this.timeout = props.timeout; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts index 0e43839b301eb..3ceef616f6a5c 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts @@ -117,7 +117,7 @@ export class Task extends State implements INextable { private readonly taskProps: StepFunctionsTaskConfig; constructor(scope: Construct, id: string, props: TaskProps) { - super(scope, id, props); + super(scope, props.stateName? props.stateName: id, props); this.timeout = props.timeout; const taskProps = props.task.bind(this); diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts index d81042f1e2909..a305601300e51 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts @@ -81,7 +81,7 @@ export class Wait extends State implements INextable { private readonly time: WaitTime; constructor(scope: Construct, id: string, props: WaitProps) { - super(scope, id, props); + super(scope, props.stateName? props.stateName: id, props); this.time = props.time; this.endStates = [this]; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts index daa069b15e37d..f5b2a03e58d92 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts @@ -27,6 +27,7 @@ describe('Custom State', () => { test('maintains the state Json provided during construction', () => { // WHEN const customState = new sfn.CustomState(stack, 'Custom', { + stateName: 'my-custom-state-name', stateJson, }); @@ -40,15 +41,16 @@ describe('Custom State', () => { test('can add a next state to the chain', () => { // WHEN const definition = new sfn.CustomState(stack, 'Custom', { + stateName: 'my-custom-state-name', stateJson, }).next(new sfn.Pass(stack, 'MyPass')); // THEN expect(render(stack, definition)).toStrictEqual( { - StartAt: 'Custom', + StartAt: 'my-custom-state-name', States: { - Custom: { + 'my-custom-state-name': { Next: 'MyPass', Type: 'Task', Resource: 'arn:aws:states:::dynamodb:putItem', @@ -62,7 +64,7 @@ describe('Custom State', () => { }, ResultPath: null, }, - MyPass: { + 'MyPass': { Type: 'Pass', End: true, }, diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/fail.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/fail.test.ts index dcb38e3c4976c..e1dc0ee2ac006 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/fail.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/fail.test.ts @@ -1,10 +1,68 @@ import * as cdk from '../../core'; import * as stepfunctions from '../lib'; +import { render } from './private/render-util'; describe('Fail State', () => { - test('Props are optional', () => { - const stack = new cdk.Stack(); + let stack: cdk.Stack; + let stateJson: any; + + beforeEach(() => { + // GIVEN + stack = new cdk.Stack(); + stateJson = { + Type: 'Task', + Resource: 'arn:aws:states:::dynamodb:putItem', + Parameters: { + TableName: 'MyTable', + Item: { + id: { + S: 'MyEntry', + }, + }, + }, + ResultPath: null, + }; + }); + test('Props are optional', () => { new stepfunctions.Fail(stack, 'Fail'); }); + + test('can add a fail state to the chain with custom state name', () => { + // WHEN + const definition = new stepfunctions.CustomState(stack, 'Custom1', { + stateName: 'my-custom-state-name', + stateJson, + }).next(new stepfunctions.Pass(stack, 'MyPass')) + .next(new stepfunctions.Fail(stack, 'Fail', { + stateName: 'my-fail-state', + comment: 'failing state', + errorPath: stepfunctions.JsonPath.stringAt('$.error'), + causePath: stepfunctions.JsonPath.stringAt('$.cause'), + })); + + // THEN + expect(render(stack, definition)).toStrictEqual( + { + StartAt: 'my-custom-state-name', + States: { + 'my-custom-state-name': { + Next: 'MyPass', + Type: 'Task', + ...stateJson, + }, + 'MyPass': { + Type: 'Pass', + Next: 'my-fail-state', + }, + 'my-fail-state': { + Comment: 'failing state', + Type: 'Fail', + CausePath: '$.cause', + ErrorPath: '$.error', + }, + }, + }, + ); + }); }); diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/map.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/map.test.ts index 9ea5dd0ee4f64..44a4417bdcd9d 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/map.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/map.test.ts @@ -8,6 +8,7 @@ describe('Map State', () => { // WHEN const map = new stepfunctions.Map(stack, 'Map State', { + stateName: 'My-Map-State', maxConcurrency: 1, itemsPath: stepfunctions.JsonPath.stringAt('$.inputForMap'), parameters: { @@ -19,9 +20,9 @@ describe('Map State', () => { // THEN expect(render(map)).toStrictEqual({ - StartAt: 'Map State', + StartAt: 'My-Map-State', States: { - 'Map State': { + 'My-Map-State': { Type: 'Map', End: true, Parameters: { diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/parallel.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/parallel.test.ts index 5744cb50ac595..2b8eb8b80dda6 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/parallel.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/parallel.test.ts @@ -8,7 +8,7 @@ describe('Parallel State', () => { // WHEN const parallel = new stepfunctions.Parallel(stack, 'Parallel State'); - parallel.branch(new stepfunctions.Pass(stack, 'Branch 1')); + parallel.branch(new stepfunctions.Pass(stack, 'Branch 1', { stateName: 'first-pass-state' })); parallel.branch(new stepfunctions.Pass(stack, 'Branch 2')); // THEN @@ -19,7 +19,7 @@ describe('Parallel State', () => { Type: 'Parallel', End: true, Branches: [ - { StartAt: 'Branch 1', States: { 'Branch 1': { Type: 'Pass', End: true } } }, + { StartAt: 'first-pass-state', States: { 'first-pass-state': { Type: 'Pass', End: true } } }, { StartAt: 'Branch 2', States: { 'Branch 2': { Type: 'Pass', End: true } } }, ], }, diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts index d9ebf9219f9a3..69d0a24e1a5d0 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine-resources.test.ts @@ -642,6 +642,7 @@ describe('State Machine Resources', () => { // GIVEN const stack = new cdk.Stack(); const task = new stepfunctions.Pass(stack, 'Pass', { + stateName: 'my-pass-state', inputPath: '$', outputPath: '$.state', parameters: { diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/wait.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/wait.test.ts index cb24ed19c0037..599baeaf44cf3 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/wait.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/wait.test.ts @@ -75,4 +75,25 @@ describe('Wait State', () => { }); }); + test('supports adding a custom state name', () => { + // GIVEN + const stack = new cdk.Stack(); + const waitTime = new Wait(stack, 'myWaitState', { + stateName: 'wait-state-custom-name', + time: WaitTime.duration(cdk.Duration.seconds(30)), + }); + + // THEN + expect(render(stack, waitTime)).toEqual({ + StartAt: 'wait-state-custom-name', + States: { + 'wait-state-custom-name': { + Seconds: 30, + Type: 'Wait', + End: true, + }, + }, + }); + }); + }); \ No newline at end of file From 31f752557658575817af70b6aef7a2501a0489c8 Mon Sep 17 00:00:00 2001 From: SankyRed Date: Tue, 3 Oct 2023 15:46:13 -0500 Subject: [PATCH 5/7] Fixing failing test --- packages/aws-cdk-lib/aws-stepfunctions/README.md | 10 ++++++++-- .../aws-stepfunctions/lib/states/custom-state.ts | 8 +------- .../aws-stepfunctions/test/custom-state.test.ts | 14 +++++++------- .../aws-stepfunctions/test/fail.test.ts | 7 +++---- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/README.md b/packages/aws-cdk-lib/aws-stepfunctions/README.md index 55ae6ec9a8781..f20e236e62bee 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/README.md +++ b/packages/aws-cdk-lib/aws-stepfunctions/README.md @@ -124,6 +124,7 @@ becomes new the new Data object. This behavior can be modified by supplying valu These properties impact how each individual step interacts with the state machine data: +* `stateName`: the custom state name to each state if required else we used the construct id * `inputPath`: the part of the data object that gets passed to the step (`itemsPath` for `Map` states) * `resultSelector`: the part of the step result that should be added to the state machine data * `resultPath`: where in the state machine data the step result should be inserted @@ -249,6 +250,8 @@ An arbitrary JSON object (specified at execution start) is passed from state to state and transformed during the execution of the workflow. For more information, see the States Language spec. +You can add custom state names to all states and if not it defaults to construct id. + ### Task A `Task` represents some work that needs to be done. Do not use the `Task` class directly. @@ -281,10 +284,12 @@ The `Pass` state also supports passing key-value pairs as input. Values can be static, or selected from the input with a path. The following example filters the `greeting` field from the state input -and also injects a field called `otherData`. +and also injects a field called `otherData`. You can also add custom state name +to Pass state. ```ts const pass = new sfn.Pass(this, 'Filter input and inject data', { + stateName: 'my-pass-state', parameters: { // input to the pass state input: sfn.JsonPath.stringAt('$.input.greeting'), otherData: 'some-extra-stuff', @@ -302,12 +307,13 @@ Learn more about the [Pass state](https://docs.aws.amazon.com/step-functions/lat A `Wait` state waits for a given number of seconds, or until the current time hits a particular time. The time to wait may be taken from the execution's JSON -state. +state. You can also add custom state name to Wait state. ```ts // Wait until it's the time mentioned in the the state object's "triggerTime" // field. const wait = new sfn.Wait(this, 'Wait For Trigger Time', { + stateName: 'new-wait-state', time: sfn.WaitTime.timestampPath('$.triggerTime'), }); diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts index a663c2a2b8d5e..8768521d7ebaf 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts @@ -7,12 +7,6 @@ import { IChainable, INextable } from '../types'; * Properties for defining a custom state definition */ export interface CustomStateProps { - /** - * Optional name for this state - * - * @default - The construct ID will be used as state name - */ - readonly stateName: string; /** * Amazon States Language (JSON-based) definition of the state @@ -35,7 +29,7 @@ export class CustomState extends State implements IChainable, INextable { private readonly stateJson: { [key: string]: any}; constructor(scope: Construct, id: string, props: CustomStateProps) { - super(scope, props.stateName? props.stateName: id, {}); + super(scope, id, {}); this.endStates = [this]; this.stateJson = props.stateJson; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts index f5b2a03e58d92..98ccf387bad49 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/custom-state.test.ts @@ -27,7 +27,6 @@ describe('Custom State', () => { test('maintains the state Json provided during construction', () => { // WHEN const customState = new sfn.CustomState(stack, 'Custom', { - stateName: 'my-custom-state-name', stateJson, }); @@ -41,17 +40,18 @@ describe('Custom State', () => { test('can add a next state to the chain', () => { // WHEN const definition = new sfn.CustomState(stack, 'Custom', { - stateName: 'my-custom-state-name', stateJson, - }).next(new sfn.Pass(stack, 'MyPass')); + }).next(new sfn.Pass(stack, 'MyPass', { + stateName: 'my-pass-state', + })); // THEN expect(render(stack, definition)).toStrictEqual( { - StartAt: 'my-custom-state-name', + StartAt: 'Custom', States: { - 'my-custom-state-name': { - Next: 'MyPass', + 'Custom': { + Next: 'my-pass-state', Type: 'Task', Resource: 'arn:aws:states:::dynamodb:putItem', Parameters: { @@ -64,7 +64,7 @@ describe('Custom State', () => { }, ResultPath: null, }, - 'MyPass': { + 'my-pass-state': { Type: 'Pass', End: true, }, diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/fail.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/fail.test.ts index e1dc0ee2ac006..0bdac483cede6 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/fail.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/fail.test.ts @@ -1,6 +1,6 @@ +import { render } from './private/render-util'; import * as cdk from '../../core'; import * as stepfunctions from '../lib'; -import { render } from './private/render-util'; describe('Fail State', () => { let stack: cdk.Stack; @@ -31,7 +31,6 @@ describe('Fail State', () => { test('can add a fail state to the chain with custom state name', () => { // WHEN const definition = new stepfunctions.CustomState(stack, 'Custom1', { - stateName: 'my-custom-state-name', stateJson, }).next(new stepfunctions.Pass(stack, 'MyPass')) .next(new stepfunctions.Fail(stack, 'Fail', { @@ -44,9 +43,9 @@ describe('Fail State', () => { // THEN expect(render(stack, definition)).toStrictEqual( { - StartAt: 'my-custom-state-name', + StartAt: 'Custom1', States: { - 'my-custom-state-name': { + 'Custom1': { Next: 'MyPass', Type: 'Task', ...stateJson, From 90189dd15a7b6f1daf23a57d47de1cadc75c5d61 Mon Sep 17 00:00:00 2001 From: SankyRed Date: Tue, 3 Oct 2023 16:15:29 -0500 Subject: [PATCH 6/7] clean up --- .../aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts index 8768521d7ebaf..3bf14fc249b80 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/custom-state.ts @@ -7,7 +7,6 @@ import { IChainable, INextable } from '../types'; * Properties for defining a custom state definition */ export interface CustomStateProps { - /** * Amazon States Language (JSON-based) definition of the state * From 146819a74cc487b27f9722930cadb66ac18f1889 Mon Sep 17 00:00:00 2001 From: SankyRed Date: Tue, 10 Oct 2023 08:31:53 -0500 Subject: [PATCH 7/7] Fixing the state name and cleanup on readme --- packages/aws-cdk-lib/aws-stepfunctions/README.md | 12 ++++-------- .../aws-stepfunctions/lib/states/choice.ts | 2 +- .../aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts | 2 +- .../aws-cdk-lib/aws-stepfunctions/lib/states/map.ts | 2 +- .../aws-stepfunctions/lib/states/parallel.ts | 2 +- .../aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts | 2 +- .../aws-stepfunctions/lib/states/state.ts | 4 ++-- .../aws-stepfunctions/lib/states/succeed.ts | 2 +- .../aws-stepfunctions/lib/states/task-base.ts | 2 +- .../aws-cdk-lib/aws-stepfunctions/lib/states/task.ts | 2 +- .../aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts | 2 +- 11 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/README.md b/packages/aws-cdk-lib/aws-stepfunctions/README.md index f20e236e62bee..d837201f2fcb7 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/README.md +++ b/packages/aws-cdk-lib/aws-stepfunctions/README.md @@ -124,7 +124,7 @@ becomes new the new Data object. This behavior can be modified by supplying valu These properties impact how each individual step interacts with the state machine data: -* `stateName`: the custom state name to each state if required else we used the construct id +* `stateName`: the name of the state in the state machine definition. If not supplied, defaults to the construct id. * `inputPath`: the part of the data object that gets passed to the step (`itemsPath` for `Map` states) * `resultSelector`: the part of the step result that should be added to the state machine data * `resultPath`: where in the state machine data the step result should be inserted @@ -250,8 +250,6 @@ An arbitrary JSON object (specified at execution start) is passed from state to state and transformed during the execution of the workflow. For more information, see the States Language spec. -You can add custom state names to all states and if not it defaults to construct id. - ### Task A `Task` represents some work that needs to be done. Do not use the `Task` class directly. @@ -284,12 +282,11 @@ The `Pass` state also supports passing key-value pairs as input. Values can be static, or selected from the input with a path. The following example filters the `greeting` field from the state input -and also injects a field called `otherData`. You can also add custom state name -to Pass state. +and also injects a field called `otherData`. ```ts const pass = new sfn.Pass(this, 'Filter input and inject data', { - stateName: 'my-pass-state', + stateName: 'my-pass-state', // the custom state name for the Pass state, defaults to 'Filter input and inject data' as the state name parameters: { // input to the pass state input: sfn.JsonPath.stringAt('$.input.greeting'), otherData: 'some-extra-stuff', @@ -307,13 +304,12 @@ Learn more about the [Pass state](https://docs.aws.amazon.com/step-functions/lat A `Wait` state waits for a given number of seconds, or until the current time hits a particular time. The time to wait may be taken from the execution's JSON -state. You can also add custom state name to Wait state. +state. ```ts // Wait until it's the time mentioned in the the state object's "triggerTime" // field. const wait = new sfn.Wait(this, 'Wait For Trigger Time', { - stateName: 'new-wait-state', time: sfn.WaitTime.timestampPath('$.triggerTime'), }); diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts index e524b35cdb684..1b35552b08a0b 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts @@ -54,7 +54,7 @@ export class Choice extends State { public readonly endStates: INextable[] = []; constructor(scope: Construct, id: string, props: ChoiceProps = {}) { - super(scope, props.stateName? props.stateName: id, props); + super(scope, id, props); } /** diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts index 7517f241d6443..efe067c8ba3a5 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts @@ -64,7 +64,7 @@ export class Fail extends State { private readonly causePath?: string; constructor(scope: Construct, id: string, props: FailProps = {}) { - super(scope, props.stateName? props.stateName: id, props); + super(scope, id, props); this.error = props.error; this.errorPath = props.errorPath; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts index b4a8af86e1876..b42de5fafee4c 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts @@ -124,7 +124,7 @@ export class Map extends State implements INextable { private readonly itemsPath?: string; constructor(scope: Construct, id: string, props: MapProps = {}) { - super(scope, props.stateName? props.stateName: id, props); + super(scope, id, props); this.endStates = [this]; this.maxConcurrency = props.maxConcurrency; this.itemsPath = props.itemsPath; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts index 24fdf2a02cd7d..1199f3d84e8b7 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts @@ -81,7 +81,7 @@ export class Parallel extends State implements INextable { private readonly _branches: IChainable[] = []; constructor(scope: Construct, id: string, props: ParallelProps = {}) { - super(scope, props.stateName? props.stateName: id, props); + super(scope, id, props); this.endStates = [this]; } diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts index 26b8b640b003a..2a2a19db7d47c 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts @@ -131,7 +131,7 @@ export class Pass extends State implements INextable { private readonly result?: Result; constructor(scope: Construct, id: string, props: PassProps = {}) { - super(scope, props.stateName? props.stateName: id, props); + super(scope, id, props); this.result = props.result; this.endStates = [this]; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts index eac5f343b541e..7598f092a396b 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts @@ -198,7 +198,7 @@ export abstract class State extends Construct implements IChainable { private readonly incomingStates: State[] = []; constructor(scope: Construct, id: string, props: StateProps) { - super(scope, props.stateName? props.stateName: id); + super(scope, id); this.startState = this; @@ -228,7 +228,7 @@ export abstract class State extends Construct implements IChainable { * Tokenized string that evaluates to the state's ID */ public get stateId(): string { - return this.prefixes.concat(this.id).join(''); + return this.prefixes.concat(this.stateName? this.stateName: this.id).join(''); } /** diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts index 6ed2d1a7d1899..80eab204832c4 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/succeed.ts @@ -52,7 +52,7 @@ export class Succeed extends State { public readonly endStates: INextable[] = []; constructor(scope: Construct, id: string, props: SucceedProps = {}) { - super(scope, props.stateName? props.stateName: id, props); + super(scope, id, props); } /** diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts index add498ad7bba7..37db0828ab8d0 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.ts @@ -157,7 +157,7 @@ export abstract class TaskStateBase extends State implements INextable { private readonly credentials?: Credentials; constructor(scope: Construct, id: string, props: TaskStateBaseProps) { - super(scope, props.stateName ? props.stateName: id, props); + super(scope, id, props); this.endStates = [this]; this.timeout = props.timeout; diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts index 3ceef616f6a5c..0e43839b301eb 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts @@ -117,7 +117,7 @@ export class Task extends State implements INextable { private readonly taskProps: StepFunctionsTaskConfig; constructor(scope: Construct, id: string, props: TaskProps) { - super(scope, props.stateName? props.stateName: id, props); + super(scope, id, props); this.timeout = props.timeout; const taskProps = props.task.bind(this); diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts index a305601300e51..d81042f1e2909 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts @@ -81,7 +81,7 @@ export class Wait extends State implements INextable { private readonly time: WaitTime; constructor(scope: Construct, id: string, props: WaitProps) { - super(scope, props.stateName? props.stateName: id, props); + super(scope, id, props); this.time = props.time; this.endStates = [this];