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 all commits
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
20 changes: 16 additions & 4 deletions src/stepfunctions/steps/integration_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,35 @@ class IntegrationPattern(Enum):

WaitForTaskToken = "waitForTaskToken"
WaitForCompletion = "sync"
RequestResponse = ""
CallAndContinue = ""


def get_service_integration_arn(service, api, integration_pattern=IntegrationPattern.RequestResponse):
def get_service_integration_arn(service, api, integration_pattern=IntegrationPattern.CallAndContinue, version=None):

"""
ARN builder for task integration
Args:
service (str): The service name for the service integration
api (str): The api of the service integration
integration_pattern (IntegrationPattern, optional): The integration pattern for the task. (Default: IntegrationPattern.RequestResponse)
integration_pattern (IntegrationPattern, optional): The integration pattern for the task. (Default: IntegrationPattern.CallAndContinue)
version (int, optional): The version of the resource to use. (Default: None)
"""
arn = ""
if integration_pattern == IntegrationPattern.RequestResponse:
if integration_pattern == IntegrationPattern.CallAndContinue:
arn = f"arn:{get_aws_partition()}:states:::{service}:{api.value}"
else:
arn = f"arn:{get_aws_partition()}:states:::{service}:{api.value}.{integration_pattern.value}"

if version:
arn = f"{arn}:{str(version)}"

return arn


def is_integration_pattern_valid(integration_pattern, supported_integration_patterns):
if not isinstance(integration_pattern, IntegrationPattern):
raise TypeError(f"Integration pattern must be of type {IntegrationPattern}")
elif integration_pattern not in supported_integration_patterns:
raise ValueError(f"Integration Pattern ({integration_pattern.name}) is not supported for this step - "
f"Please use one of the following: "
f"{[integ_type.name for integ_type in supported_integration_patterns]}")
59 changes: 58 additions & 1 deletion src/stepfunctions/steps/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from enum import Enum
from stepfunctions.steps.states import Task
from stepfunctions.steps.fields import Field
from stepfunctions.steps.integration_resources import IntegrationPattern, get_service_integration_arn
from stepfunctions.steps.integration_resources import IntegrationPattern, get_service_integration_arn,\
is_integration_pattern_valid

DYNAMODB_SERVICE_NAME = "dynamodb"
EKS_SERVICES_NAME = "eks"
Expand All @@ -24,6 +25,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 +72,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 +893,54 @@ 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 a 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, integration_pattern=IntegrationPattern.WaitForCompletion, **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.
integration_pattern (stepfunctions.steps.integration_resources.IntegrationPattern, optional): Service integration pattern used to call the integrated service. (default: WaitForCompletion)
Supported integration patterns:
WaitForCompletion: Wait for the state machine execution to complete before going to the next state. (See `Run A Job <https://docs.aws.amazon.com/step-functions/latest/dg/connect-to-resource.html#connect-sync`_ for more details.)
WaitForTaskToken: Wait for the state machine execution to return a task token before progressing to the next state (See `Wait for a Callback with the Task Token <https://docs.aws.amazon.com/step-functions/latest/dg/connect-to-resource.html#connect-wait-token`_ for more details.)
CallAndContinue: Call StartExecution and progress to the next state (See `Request Response <https://docs.aws.amazon.com/step-functions/latest/dg/connect-to-resource.html#connect-default`_ for more details.)
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. (default: None)
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: '$')
"""
supported_integ_patterns = [IntegrationPattern.WaitForCompletion, IntegrationPattern.WaitForTaskToken,
IntegrationPattern.CallAndContinue]

is_integration_pattern_valid(integration_pattern, supported_integ_patterns)

if integration_pattern == IntegrationPattern.WaitForCompletion:
"""
Example resource arn:aws:states:::states:startExecution.sync:2
"""
kwargs[Field.Resource.value] = get_service_integration_arn(STEP_FUNCTIONS_SERVICE_NAME,
StepFunctions.StartExecution,
integration_pattern,
2)
else:
"""
Example resource arn:
- arn:aws:states:::states:startExecution.waitForTaskToken
- arn:aws:states:::states:startExecution
"""
kwargs[Field.Resource.value] = get_service_integration_arn(STEP_FUNCTIONS_SERVICE_NAME,
StepFunctions.StartExecution,
integration_pattern)

super(StepFunctionsStartExecutionStep, self).__init__(state_id, **kwargs)
97 changes: 97 additions & 0 deletions tests/unit/test_service_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from __future__ import absolute_import

import boto3
import pytest

from unittest.mock import patch
from stepfunctions.steps.service import DynamoDBGetItemStep, DynamoDBPutItemStep, DynamoDBUpdateItemStep, DynamoDBDeleteItemStep
Expand All @@ -30,6 +31,8 @@
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
from stepfunctions.steps.integration_resources import IntegrationPattern


@patch.object(boto3.session.Session, 'region_name', 'us-east-1')
Expand Down Expand Up @@ -1158,3 +1161,97 @@ 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_default():
step = StepFunctionsStartExecutionStep(
"SFN Start Execution", parameters={
"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": {
"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_call_and_continue():
step = StepFunctionsStartExecutionStep(
"SFN Start Execution", integration_pattern=IntegrationPattern.CallAndContinue, parameters={
"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": {
"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_completion():
step = StepFunctionsStartExecutionStep(
"SFN Start Execution - Sync", integration_pattern=IntegrationPattern.WaitForCompletion, parameters={
"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": {
"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_task_token():
step = StepFunctionsStartExecutionStep(
"SFN Start Execution - Wait for Callback", integration_pattern=IntegrationPattern.WaitForTaskToken,
parameters={
"Input": {
"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": {
"token.$": "$$.Task.Token"
},
"StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld",
"Name": "ExecutionName"
},
"End": True
}


@pytest.mark.parametrize("integration_pattern", [
None,
"ServiceIntegrationTypeStr",
0
])
@patch.object(boto3.session.Session, 'region_name', 'us-east-1')
def test_step_functions_start_execution_step_creation_invalid_integration_pattern_raises_type_error(integration_pattern):
with pytest.raises(TypeError):
StepFunctionsStartExecutionStep("SFN Start Execution - invalid ServiceType", integration_pattern=integration_pattern)
26 changes: 23 additions & 3 deletions tests/unit/test_steps_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@

# Test if boto3 session can fetch correct aws partition info from test environment

from stepfunctions.steps.utils import get_aws_partition, merge_dicts
from stepfunctions.steps.integration_resources import IntegrationPattern, get_service_integration_arn
import boto3
from unittest.mock import patch
import logging
import pytest

from enum import Enum
from unittest.mock import patch

from stepfunctions.steps.utils import get_aws_partition, merge_dicts
from stepfunctions.steps.integration_resources import IntegrationPattern,\
get_service_integration_arn, is_integration_pattern_valid


testService = "sagemaker"
Expand Down Expand Up @@ -86,3 +91,18 @@ def test_merge_dicts():
'b': 2,
'c': 3
}


@pytest.mark.parametrize("service_integration_type", [
None,
"IntegrationPatternStr",
0
])
def test_is_integration_pattern_valid_with_invalid_type_raises_type_error(service_integration_type):
with pytest.raises(TypeError):
is_integration_pattern_valid(service_integration_type, [IntegrationPattern.WaitForTaskToken])


def test_is_integration_pattern_valid_with_non_supported_type_raises_value_error():
with pytest.raises(ValueError):
is_integration_pattern_valid(IntegrationPattern.WaitForTaskToken, [IntegrationPattern.WaitForCompletion])