Skip to content

Commit e87f1bf

Browse files
committed
simplify rendering of task json. move common state json into base class
1 parent 193b048 commit e87f1bf

File tree

5 files changed

+43
-70
lines changed

5 files changed

+43
-70
lines changed

packages/@aws-cdk/aws-stepfunctions-tasks/lib/lambda/invoke.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as iam from '@aws-cdk/aws-iam';
22
import * as lambda from '@aws-cdk/aws-lambda';
33
import * as sfn from '@aws-cdk/aws-stepfunctions';
44
import * as cdk from '@aws-cdk/core';
5-
import { getResourceArn, TaskStateConfig, taskStateJson, validatePatternSupported } from '../private/task-utils';
5+
import { integrationResourceArn, validatePatternSupported } from '../private/task-utils';
66

77
/**
88
* Properties for invoking a Lambda function with LambdaInvoke
@@ -90,18 +90,16 @@ export class LambdaInvoke extends sfn.TaskStateBase {
9090
* Provides the service integration task configuration
9191
*/
9292
protected renderTask(): any {
93-
const taskStateConfig: TaskStateConfig = {
94-
resourceArn: getResourceArn('lambda', 'invoke', this.integrationPattern),
95-
parameters: {
93+
return {
94+
Resource: integrationResourceArn('lambda', 'invoke', this.integrationPattern),
95+
Parameters: sfn.FieldUtils.renderObject({
9696
FunctionName: this.props.lambdaFunction.functionArn,
9797
Payload: this.props.payload ? this.props.payload.value : sfn.TaskInput.fromDataAt('$').value,
9898
InvocationType: this.props.invocationType,
9999
ClientContext: this.props.clientContext,
100100
Qualifier: this.props.qualifier,
101-
},
101+
}),
102102
};
103-
104-
return taskStateJson(taskStateConfig);
105103
}
106104
}
107105

Original file line numberDiff line numberDiff line change
@@ -1,48 +1,8 @@
11
import {
2-
FieldUtils,
32
IntegrationPattern,
4-
renderJsonPath,
5-
TaskStateBaseProps,
63
} from '@aws-cdk/aws-stepfunctions';
74
import { Aws } from '@aws-cdk/core';
85

9-
/**
10-
* Represents a service integration call to Step Functions
11-
*/
12-
export interface TaskStateConfig extends TaskStateBaseProps {
13-
/**
14-
* The ARN of resource that represents the work to be executed
15-
*/
16-
readonly resourceArn: string;
17-
18-
/**
19-
* Parameters pass a collection of key-value pairs, either static values or
20-
* JSON path expressions that select from the input.
21-
*
22-
* @see https://docs.aws.amazon.com/step-functions/latest/dg/input-output-inputpath-params.html#input-output-parameters
23-
*
24-
* @default - No parameters
25-
*/
26-
readonly parameters?: { [name: string]: any };
27-
}
28-
29-
/**
30-
* Generates the State JSON to define a task state
31-
*/
32-
export function taskStateJson(config: TaskStateConfig): any {
33-
return {
34-
Type: 'Task',
35-
Comment: config.comment,
36-
Resource: config.resourceArn,
37-
Parameters: config.parameters && FieldUtils.renderObject(config.parameters),
38-
TimeoutSeconds: config.timeout?.toSeconds(),
39-
HeartbeatSeconds: config.heartbeat?.toSeconds(),
40-
InputPath: renderJsonPath(config.inputPath),
41-
OutputPath: renderJsonPath(config.outputPath),
42-
ResultPath: renderJsonPath(config.resultPath),
43-
};
44-
}
45-
466
/**
477
* Verifies that a validation pattern is supported for a service integration
488
*
@@ -60,15 +20,16 @@ export function validatePatternSupported(integrationPattern: IntegrationPattern,
6020
*
6121
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-to-resource.html
6222
*/
63-
const resourceArnSuffix = new Map<IntegrationPattern, string>();
64-
resourceArnSuffix.set(IntegrationPattern.REQUEST_RESPONSE, '');
65-
resourceArnSuffix.set(IntegrationPattern.RUN_JOB, '.sync');
66-
resourceArnSuffix.set(IntegrationPattern.WAIT_FOR_TASK_TOKEN, '.waitForTaskToken');
23+
const resourceArnSuffix: Record<IntegrationPattern, string> = {
24+
[IntegrationPattern.REQUEST_RESPONSE]: '',
25+
[IntegrationPattern.RUN_JOB]: '.sync',
26+
[IntegrationPattern.WAIT_FOR_TASK_TOKEN]: '.waitForTaskToken',
27+
};
6728

68-
export function getResourceArn(service: string, api: string, integrationPattern: IntegrationPattern): string {
29+
export function integrationResourceArn(service: string, api: string, integrationPattern: IntegrationPattern): string {
6930
if (!service || !api) {
7031
throw new Error("Both 'service' and 'api' must be provided to build the resource ARN.");
7132
}
7233
return `arn:${Aws.PARTITION}:states:::${service}:${api}` +
73-
(integrationPattern ? resourceArnSuffix.get(integrationPattern) : '');
34+
(integrationPattern ? resourceArnSuffix[integrationPattern] : '');
7435
}

packages/@aws-cdk/aws-stepfunctions-tasks/lib/sns/publish.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as iam from '@aws-cdk/aws-iam';
22
import * as sns from '@aws-cdk/aws-sns';
33
import * as sfn from '@aws-cdk/aws-stepfunctions';
44
import * as cdk from '@aws-cdk/core';
5-
import { getResourceArn, TaskStateConfig, taskStateJson, validatePatternSupported } from '../private/task-utils';
5+
import { integrationResourceArn, validatePatternSupported } from '../private/task-utils';
66

77
/**
88
* Properties for PublishTask
@@ -87,16 +87,14 @@ export class SnsPublish extends sfn.TaskStateBase {
8787
}
8888

8989
protected renderTask(): any {
90-
const taskConfig: TaskStateConfig = {
91-
resourceArn: getResourceArn('sns', 'publish', this.integrationPattern),
92-
parameters: {
90+
return {
91+
Resource: integrationResourceArn('sns', 'publish', this.integrationPattern),
92+
Parameters: sfn.FieldUtils.renderObject({
9393
TopicArn: this.props.topic.topicArn,
9494
Message: this.props.message.value,
9595
MessageStructure: this.props.messagePerSubscriptionType ? 'json' : undefined,
9696
Subject: this.props.subject,
97-
},
97+
}),
9898
};
99-
100-
return taskStateJson(taskConfig);
10199
}
102100
}

packages/@aws-cdk/aws-stepfunctions-tasks/lib/sqs/send-message.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as iam from '@aws-cdk/aws-iam';
22
import * as sqs from '@aws-cdk/aws-sqs';
33
import * as sfn from '@aws-cdk/aws-stepfunctions';
44
import * as cdk from '@aws-cdk/core';
5-
import { getResourceArn, TaskStateConfig, taskStateJson, validatePatternSupported } from '../private/task-utils';
5+
import { integrationResourceArn, validatePatternSupported } from '../private/task-utils';
66

77
/**
88
* Properties for SendMessageTask
@@ -92,17 +92,15 @@ export class SqsSendMessage extends sfn.TaskStateBase {
9292
}
9393

9494
protected renderTask(): any {
95-
const taskConfig: TaskStateConfig = {
96-
resourceArn: getResourceArn('sqs', 'sendMessage', this.integrationPattern),
97-
parameters: {
95+
return {
96+
Resource: integrationResourceArn('sqs', 'sendMessage', this.integrationPattern),
97+
Parameters: sfn.FieldUtils.renderObject({
9898
QueueUrl: this.props.queue.queueUrl,
9999
MessageBody: this.props.messageBody.value,
100100
DelaySeconds: this.props.delay && this.props.delay.toSeconds(),
101101
MessageDeduplicationId: this.props.messageDeduplicationId,
102102
MessageGroupId: this.props.messageGroupId,
103-
},
103+
}),
104104
};
105-
106-
return taskStateJson(taskConfig);
107105
}
108106
}

packages/@aws-cdk/aws-stepfunctions/lib/states/task-base.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as iam from '@aws-cdk/aws-iam';
33
import { Chain } from '../chain';
44
import { StateGraph } from '../state-graph';
55
import { CatchProps, IChainable, INextable, RetryProps } from '../types';
6-
import { State } from './state';
6+
import { renderJsonPath, State } from './state';
77

88
import * as cdk from '@aws-cdk/core';
99

@@ -89,12 +89,17 @@ export abstract class TaskStateBase extends State implements INextable {
8989

9090
public readonly endStates: INextable[];
9191

92-
protected abstract readonly taskMetrics: TaskMetricsConfig | undefined;
93-
protected abstract readonly taskPolicies: iam.PolicyStatement[] | undefined;
92+
protected abstract readonly taskMetrics?: TaskMetricsConfig;
93+
protected abstract readonly taskPolicies?: iam.PolicyStatement[];
94+
95+
private readonly timeout?: cdk.Duration;
96+
private readonly heartbeat?: cdk.Duration;
9497

9598
constructor(scope: cdk.Construct, id: string, props: TaskStateBaseProps) {
9699
super(scope, id, props);
97100
this.endStates = [this];
101+
this.timeout = props.timeout;
102+
this.heartbeat = props.heartbeat;
98103
}
99104

100105
/**
@@ -134,6 +139,7 @@ export abstract class TaskStateBase extends State implements INextable {
134139
return {
135140
...this.renderNextEnd(),
136141
...this.renderRetryCatch(),
142+
...this.renderTaskBase(),
137143
...this.renderTask(),
138144
};
139145
}
@@ -249,6 +255,18 @@ export abstract class TaskStateBase extends State implements INextable {
249255
}
250256
return this.metric(prefix + suffix, props);
251257
}
258+
259+
private renderTaskBase() {
260+
return {
261+
Type: 'Task',
262+
Comment: this.comment,
263+
TimeoutSeconds: this.timeout?.toSeconds(),
264+
HeartbeatSeconds: this.heartbeat?.toSeconds(),
265+
InputPath: renderJsonPath(this.inputPath),
266+
OutputPath: renderJsonPath(this.outputPath),
267+
ResultPath: renderJsonPath(this.resultPath),
268+
};
269+
}
252270
}
253271

254272
/**

0 commit comments

Comments
 (0)