Skip to content

Commit

Permalink
Fix to use the custom state name if provided else default to the cons…
Browse files Browse the repository at this point in the history
…truct id
  • Loading branch information
SankyRed committed Oct 3, 2023
1 parent 81c6d94 commit 1163071
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-stepfunctions/lib/states/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-stepfunctions/lib/states/pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-stepfunctions/lib/states/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand All @@ -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',
Expand All @@ -62,7 +64,7 @@ describe('Custom State', () => {
},
ResultPath: null,
},
MyPass: {
'MyPass': {
Type: 'Pass',
End: true,
},
Expand Down
62 changes: 60 additions & 2 deletions packages/aws-cdk-lib/aws-stepfunctions/test/fail.test.ts
Original file line number Diff line number Diff line change
@@ -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',
},
},
},
);
});
});
5 changes: 3 additions & 2 deletions packages/aws-cdk-lib/aws-stepfunctions/test/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-stepfunctions/test/parallel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 } } },
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
21 changes: 21 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/test/wait.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
});
});

});

0 comments on commit 1163071

Please sign in to comment.