Skip to content

feat: Add support for nested Aws StepFunctions service integration #166

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 21 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2d7003b
Add support for nested Aws StepFunctions service integration
ca-nguyen Sep 11, 2021
b8c1987
Merge branch 'main' into add-support-for-step-functions
shivlaks Sep 12, 2021
2953c90
Json response by default when wait_for_completion enabled and raise e…
ca-nguyen Sep 12, 2021
e03be34
Removed string_response arg - wait_for_completion will use :sync:2 re…
ca-nguyen Sep 12, 2021
d1185a9
Add async_call flag to use arn:aws:states:::states:startExecution res…
ca-nguyen Sep 12, 2021
1c7a4ac
Updated flags validation
ca-nguyen Sep 12, 2021
c08140a
Updated test
ca-nguyen Sep 13, 2021
1c52e6c
Use enum to select service integration type
ca-nguyen Sep 14, 2021
e091bc8
Merge branch 'main' into add-support-for-step-functions
ca-nguyen Oct 7, 2021
5d7c99e
Update per PR review
ca-nguyen Oct 7, 2021
bf28366
Merge branch 'main' into add-support-for-step-functions
ca-nguyen Oct 8, 2021
79bc5dd
Update src/stepfunctions/steps/service.py
ca-nguyen Oct 8, 2021
64969ae
Remove ServiceIntegrationType to use existing IntegrationPattern enum
ca-nguyen Oct 8, 2021
f6cb2e4
Merge doc update
ca-nguyen Oct 8, 2021
11c108e
Update src/stepfunctions/steps/integration_resources.py
ca-nguyen Oct 14, 2021
64708c4
Removed unused logger and updated docstring
ca-nguyen Oct 15, 2021
aabdf8e
Merge branch 'main' into add-support-for-step-functions
ca-nguyen Oct 15, 2021
3b40bd3
Fixing error message and documenting args default values
ca-nguyen Oct 15, 2021
a2dba89
Updated arg description doc string for CallAndContinue
ca-nguyen Oct 15, 2021
c882e07
Updated doc string for WaitForTaskToken
ca-nguyen Oct 15, 2021
ecb2e05
Merge branch 'main' into add-support-for-step-functions
ca-nguyen Oct 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ This module provides classes to build steps that integrate with Amazon DynamoDB,

- `Amazon SQS <#amazon-sqs>`__

- `AWS Step Functions <#aws-step-functions>`__


Amazon DynamoDB
----------------
Expand Down Expand Up @@ -82,3 +84,8 @@ Amazon SNS
Amazon SQS
-----------
.. autoclass:: stepfunctions.steps.service.SqsSendMessageStep

AWS Step Functions
------------------
.. autoclass:: stepfunctions.steps.service.StepFunctionsStartExecutionStep

1 change: 1 addition & 0 deletions src/stepfunctions/steps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
from stepfunctions.steps.service import EventBridgePutEventsStep
from stepfunctions.steps.service import GlueDataBrewStartJobRunStep
from stepfunctions.steps.service import SnsPublishStep, SqsSendMessageStep
from stepfunctions.steps.service import StepFunctionsStartExecutionStep
1 change: 1 addition & 0 deletions src/stepfunctions/steps/integration_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class IntegrationPattern(Enum):
WaitForTaskToken = "waitForTaskToken"
WaitForCompletion = "sync"
RequestResponse = ""
WaitForCompletionWithJsonResponse = "sync:2"


def get_service_integration_arn(service, api, integration_pattern=IntegrationPattern.RequestResponse):
Expand Down
61 changes: 61 additions & 0 deletions src/stepfunctions/steps/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
GLUE_DATABREW_SERVICE_NAME = "databrew"
SNS_SERVICE_NAME = "sns"
SQS_SERVICE_NAME = "sqs"
STEP_FUNCTIONS_SERVICE_NAME = "states"


class DynamoDBApi(Enum):
Expand Down Expand Up @@ -70,6 +71,10 @@ class SqsApi(Enum):
SendMessage = "sendMessage"


class StepFunctions(Enum):
StartExecution = "startExecution"


class DynamoDBGetItemStep(Task):
"""
Creates a Task state to get an item from DynamoDB. See `Call DynamoDB APIs with Step Functions <https://docs.aws.amazon.com/step-functions/latest/dg/connect-ddb.html>`_ for more details.
Expand Down Expand Up @@ -887,3 +892,59 @@ def __init__(self, state_id, **kwargs):
ElasticMapReduceApi.ModifyInstanceGroupByName)

super(EmrModifyInstanceGroupByNameStep, self).__init__(state_id, **kwargs)


class StepFunctionsStartExecutionStep(Task):

"""
Creates a Task state that starts an execution of another state machine. See `Manage AWS Step Functions Executions as an Integrated Service <https://docs.aws.amazon.com/step-functions/latest/dg/connect-stepfunctions.html`_ for more details.
"""

def __init__(self, state_id, wait_for_callback=False, wait_for_completion=True, json_ouput=False, **kwargs):
"""
Args:
state_id (str): State name whose length **must be** less than or equal to 128 unicode characters. State names **must be** unique within the scope of the whole state machine.
wait_for_callback(bool, optional): Boolean value set to `True` if the Task state should wait for callback to resume the operation. (default: False)
timeout_seconds (int, optional): Positive integer specifying timeout for the state in seconds. If the state runs longer than the specified timeout, then the interpreter fails the state with a `States.Timeout` Error Name. (default: 60)
timeout_seconds_path (str, optional): Path specifying the state's timeout value in seconds from the state input. When resolved, the path must select a field whose value is a positive integer.
heartbeat_seconds (int, optional): Positive integer specifying heartbeat timeout for the state in seconds. This value should be lower than the one specified for `timeout_seconds`. If more time than the specified heartbeat elapses between heartbeats from the task, then the interpreter fails the state with a `States.Timeout` Error Name.
heartbeat_seconds_path (str, optional): Path specifying the state's heartbeat value in seconds from the state input. When resolved, the path must select a field whose value is a positive integer.
comment (str, optional): Human-readable comment or description. (default: None)
input_path (str, optional): Path applied to the state’s raw input to select some or all of it; that selection is used by the state. (default: '$')
parameters (dict, optional): The value of this field becomes the effective input for the state.
result_path (str, optional): Path specifying the raw input’s combination with or replacement by the state’s result. (default: '$')
output_path (str, optional): Path applied to the state’s output after the application of `result_path`, producing the effective output which serves as the raw input for the next state. (default: '$')
wait_for_completion (bool, optional): Boolean value set to `True` if the Task state should wait to complete before proceeding to the next step in the workflow. (default: True)
json_output(bool, optional): Boolean value set to `True` if the the Task should return a response in JSON format. If set to `False`, the Task should return a str response (default: False)
"""
if wait_for_callback:
"""
Example resource arn: arn:aws:states:::states:startExecution.waitForTaskToken
"""

kwargs[Field.Resource.value] = get_service_integration_arn(STEP_FUNCTIONS_SERVICE_NAME,
StepFunctions.StartExecution,
IntegrationPattern.WaitForTaskToken)
elif wait_for_completion:
if json_ouput:
"""
Example resource arn:aws:states:::states:startExecution.sync:2
"""
kwargs[Field.Resource.value] = get_service_integration_arn(STEP_FUNCTIONS_SERVICE_NAME,
StepFunctions.StartExecution,
IntegrationPattern.WaitForCompletionWithJsonResponse)
else:
"""
Example resource arn:aws:states:::states:startExecution.sync
"""
kwargs[Field.Resource.value] = get_service_integration_arn(STEP_FUNCTIONS_SERVICE_NAME,
StepFunctions.StartExecution,
IntegrationPattern.WaitForCompletion)
else:
"""
Example resource arn:aws:states:::states:startExecution
"""
kwargs[Field.Resource.value] = get_service_integration_arn(STEP_FUNCTIONS_SERVICE_NAME,
StepFunctions.StartExecution)

super(StepFunctionsStartExecutionStep, self).__init__(state_id, **kwargs)
106 changes: 106 additions & 0 deletions tests/unit/test_service_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from stepfunctions.steps.service import EventBridgePutEventsStep
from stepfunctions.steps.service import SnsPublishStep, SqsSendMessageStep
from stepfunctions.steps.service import GlueDataBrewStartJobRunStep
from stepfunctions.steps.service import StepFunctionsStartExecutionStep


@patch.object(boto3.session.Session, 'region_name', 'us-east-1')
Expand Down Expand Up @@ -1158,3 +1159,108 @@ def test_eks_call_step_creation():
},
'End': True
}


@patch.object(boto3.session.Session, 'region_name', 'us-east-1')
def test_step_functions_start_execution_step_creation():
step = StepFunctionsStartExecutionStep(
"SFN Start Execution", wait_for_callback=False, wait_for_completion=False,
parameters={
"Input": {
"Comment": "Hello world!"
},
"StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld",
"Name": "ExecutionName"
})

assert step.to_dict() == {
"Type": "Task",
"Resource": "arn:aws:states:::states:startExecution",
"Parameters": {
"Input": {
"Comment": "Hello world!"
},
"StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld",
"Name": "ExecutionName"
},
"End": True
}


@patch.object(boto3.session.Session, 'region_name', 'us-east-1')
def test_step_functions_start_execution_step_creation_sync_json_response():
step = StepFunctionsStartExecutionStep(
"SFN Start Execution - Sync with json response", wait_for_callback=False, wait_for_completion=True,
json_ouput=True, parameters={
"Input": {
"Comment": "Hello world!"
},
"StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld",
"Name": "ExecutionName"
})

assert step.to_dict() == {
"Type": "Task",
"Resource": "arn:aws:states:::states:startExecution.sync:2",
"Parameters": {
"Input": {
"Comment": "Hello world!"
},
"StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld",
"Name": "ExecutionName"
},
"End": True
}


@patch.object(boto3.session.Session, 'region_name', 'us-east-1')
def test_step_functions_start_execution_step_creation_sync_json_response():
step = StepFunctionsStartExecutionStep(
"SFN Start Execution - Sync with string response", wait_for_callback=False, wait_for_completion=True,
json_ouput=False, parameters={
"Input": {
"Comment": "Hello world!"
},
"StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld",
"Name": "ExecutionName"
})

assert step.to_dict() == {
"Type": "Task",
"Resource": "arn:aws:states:::states:startExecution.sync",
"Parameters": {
"Input": {
"Comment": "Hello world!"
},
"StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld",
"Name": "ExecutionName"
},
"End": True
}


@patch.object(boto3.session.Session, 'region_name', 'us-east-1')
def test_step_functions_start_execution_step_creation_wait_for_callback():
step = StepFunctionsStartExecutionStep(
"SFN Start Execution - Wait for Callback", wait_for_callback=True, parameters={
"Input": {
"Comment": "Hello world!",
"token.$": "$$.Task.Token"
},
"StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld",
"Name": "ExecutionName"
})

assert step.to_dict() == {
"Type": "Task",
"Resource": "arn:aws:states:::states:startExecution.waitForTaskToken",
"Parameters": {
"Input": {
"Comment": "Hello world!",
"token.$": "$$.Task.Token"
},
"StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld",
"Name": "ExecutionName"
},
"End": True
}