Run black on moto & test directories.
This commit is contained in:
parent
c820395dbf
commit
96e5b1993d
507 changed files with 52541 additions and 47814 deletions
|
|
@ -2,5 +2,5 @@ from __future__ import unicode_literals
|
|||
from .models import stepfunction_backends
|
||||
from ..core.models import base_decorator
|
||||
|
||||
stepfunction_backend = stepfunction_backends['us-east-1']
|
||||
stepfunction_backend = stepfunction_backends["us-east-1"]
|
||||
mock_stepfunctions = base_decorator(stepfunction_backends)
|
||||
|
|
|
|||
|
|
@ -12,24 +12,27 @@ class AWSError(Exception):
|
|||
self.status = status if status is not None else self.STATUS
|
||||
|
||||
def response(self):
|
||||
return json.dumps({'__type': self.type, 'message': self.message}), dict(status=self.status)
|
||||
return (
|
||||
json.dumps({"__type": self.type, "message": self.message}),
|
||||
dict(status=self.status),
|
||||
)
|
||||
|
||||
|
||||
class ExecutionDoesNotExist(AWSError):
|
||||
TYPE = 'ExecutionDoesNotExist'
|
||||
TYPE = "ExecutionDoesNotExist"
|
||||
STATUS = 400
|
||||
|
||||
|
||||
class InvalidArn(AWSError):
|
||||
TYPE = 'InvalidArn'
|
||||
TYPE = "InvalidArn"
|
||||
STATUS = 400
|
||||
|
||||
|
||||
class InvalidName(AWSError):
|
||||
TYPE = 'InvalidName'
|
||||
TYPE = "InvalidName"
|
||||
STATUS = 400
|
||||
|
||||
|
||||
class StateMachineDoesNotExist(AWSError):
|
||||
TYPE = 'StateMachineDoesNotExist'
|
||||
TYPE = "StateMachineDoesNotExist"
|
||||
STATUS = 400
|
||||
|
|
|
|||
|
|
@ -5,10 +5,15 @@ from moto.core import BaseBackend
|
|||
from moto.core.utils import iso_8601_datetime_without_milliseconds
|
||||
from moto.sts.models import ACCOUNT_ID
|
||||
from uuid import uuid4
|
||||
from .exceptions import ExecutionDoesNotExist, InvalidArn, InvalidName, StateMachineDoesNotExist
|
||||
from .exceptions import (
|
||||
ExecutionDoesNotExist,
|
||||
InvalidArn,
|
||||
InvalidName,
|
||||
StateMachineDoesNotExist,
|
||||
)
|
||||
|
||||
|
||||
class StateMachine():
|
||||
class StateMachine:
|
||||
def __init__(self, arn, name, definition, roleArn, tags=None):
|
||||
self.creation_date = iso_8601_datetime_without_milliseconds(datetime.now())
|
||||
self.arn = arn
|
||||
|
|
@ -18,19 +23,28 @@ class StateMachine():
|
|||
self.tags = tags
|
||||
|
||||
|
||||
class Execution():
|
||||
def __init__(self, region_name, account_id, state_machine_name, execution_name, state_machine_arn):
|
||||
execution_arn = 'arn:aws:states:{}:{}:execution:{}:{}'
|
||||
execution_arn = execution_arn.format(region_name, account_id, state_machine_name, execution_name)
|
||||
class Execution:
|
||||
def __init__(
|
||||
self,
|
||||
region_name,
|
||||
account_id,
|
||||
state_machine_name,
|
||||
execution_name,
|
||||
state_machine_arn,
|
||||
):
|
||||
execution_arn = "arn:aws:states:{}:{}:execution:{}:{}"
|
||||
execution_arn = execution_arn.format(
|
||||
region_name, account_id, state_machine_name, execution_name
|
||||
)
|
||||
self.execution_arn = execution_arn
|
||||
self.name = execution_name
|
||||
self.start_date = iso_8601_datetime_without_milliseconds(datetime.now())
|
||||
self.state_machine_arn = state_machine_arn
|
||||
self.status = 'RUNNING'
|
||||
self.status = "RUNNING"
|
||||
self.stop_date = None
|
||||
|
||||
def stop(self):
|
||||
self.status = 'SUCCEEDED'
|
||||
self.status = "SUCCEEDED"
|
||||
self.stop_date = iso_8601_datetime_without_milliseconds(datetime.now())
|
||||
|
||||
|
||||
|
|
@ -42,26 +56,108 @@ class StepFunctionBackend(BaseBackend):
|
|||
# brackets < > { } [ ]
|
||||
# wildcard characters ? *
|
||||
# special characters " # % \ ^ | ~ ` $ & , ; : /
|
||||
invalid_chars_for_name = [' ', '{', '}', '[', ']', '<', '>',
|
||||
'?', '*',
|
||||
'"', '#', '%', '\\', '^', '|', '~', '`', '$', '&', ',', ';', ':', '/']
|
||||
invalid_chars_for_name = [
|
||||
" ",
|
||||
"{",
|
||||
"}",
|
||||
"[",
|
||||
"]",
|
||||
"<",
|
||||
">",
|
||||
"?",
|
||||
"*",
|
||||
'"',
|
||||
"#",
|
||||
"%",
|
||||
"\\",
|
||||
"^",
|
||||
"|",
|
||||
"~",
|
||||
"`",
|
||||
"$",
|
||||
"&",
|
||||
",",
|
||||
";",
|
||||
":",
|
||||
"/",
|
||||
]
|
||||
# control characters (U+0000-001F , U+007F-009F )
|
||||
invalid_unicodes_for_name = [u'\u0000', u'\u0001', u'\u0002', u'\u0003', u'\u0004',
|
||||
u'\u0005', u'\u0006', u'\u0007', u'\u0008', u'\u0009',
|
||||
u'\u000A', u'\u000B', u'\u000C', u'\u000D', u'\u000E', u'\u000F',
|
||||
u'\u0010', u'\u0011', u'\u0012', u'\u0013', u'\u0014',
|
||||
u'\u0015', u'\u0016', u'\u0017', u'\u0018', u'\u0019',
|
||||
u'\u001A', u'\u001B', u'\u001C', u'\u001D', u'\u001E', u'\u001F',
|
||||
u'\u007F',
|
||||
u'\u0080', u'\u0081', u'\u0082', u'\u0083', u'\u0084', u'\u0085',
|
||||
u'\u0086', u'\u0087', u'\u0088', u'\u0089',
|
||||
u'\u008A', u'\u008B', u'\u008C', u'\u008D', u'\u008E', u'\u008F',
|
||||
u'\u0090', u'\u0091', u'\u0092', u'\u0093', u'\u0094', u'\u0095',
|
||||
u'\u0096', u'\u0097', u'\u0098', u'\u0099',
|
||||
u'\u009A', u'\u009B', u'\u009C', u'\u009D', u'\u009E', u'\u009F']
|
||||
accepted_role_arn_format = re.compile('arn:aws:iam::(?P<account_id>[0-9]{12}):role/.+')
|
||||
accepted_mchn_arn_format = re.compile('arn:aws:states:[-0-9a-zA-Z]+:(?P<account_id>[0-9]{12}):stateMachine:.+')
|
||||
accepted_exec_arn_format = re.compile('arn:aws:states:[-0-9a-zA-Z]+:(?P<account_id>[0-9]{12}):execution:.+')
|
||||
invalid_unicodes_for_name = [
|
||||
u"\u0000",
|
||||
u"\u0001",
|
||||
u"\u0002",
|
||||
u"\u0003",
|
||||
u"\u0004",
|
||||
u"\u0005",
|
||||
u"\u0006",
|
||||
u"\u0007",
|
||||
u"\u0008",
|
||||
u"\u0009",
|
||||
u"\u000A",
|
||||
u"\u000B",
|
||||
u"\u000C",
|
||||
u"\u000D",
|
||||
u"\u000E",
|
||||
u"\u000F",
|
||||
u"\u0010",
|
||||
u"\u0011",
|
||||
u"\u0012",
|
||||
u"\u0013",
|
||||
u"\u0014",
|
||||
u"\u0015",
|
||||
u"\u0016",
|
||||
u"\u0017",
|
||||
u"\u0018",
|
||||
u"\u0019",
|
||||
u"\u001A",
|
||||
u"\u001B",
|
||||
u"\u001C",
|
||||
u"\u001D",
|
||||
u"\u001E",
|
||||
u"\u001F",
|
||||
u"\u007F",
|
||||
u"\u0080",
|
||||
u"\u0081",
|
||||
u"\u0082",
|
||||
u"\u0083",
|
||||
u"\u0084",
|
||||
u"\u0085",
|
||||
u"\u0086",
|
||||
u"\u0087",
|
||||
u"\u0088",
|
||||
u"\u0089",
|
||||
u"\u008A",
|
||||
u"\u008B",
|
||||
u"\u008C",
|
||||
u"\u008D",
|
||||
u"\u008E",
|
||||
u"\u008F",
|
||||
u"\u0090",
|
||||
u"\u0091",
|
||||
u"\u0092",
|
||||
u"\u0093",
|
||||
u"\u0094",
|
||||
u"\u0095",
|
||||
u"\u0096",
|
||||
u"\u0097",
|
||||
u"\u0098",
|
||||
u"\u0099",
|
||||
u"\u009A",
|
||||
u"\u009B",
|
||||
u"\u009C",
|
||||
u"\u009D",
|
||||
u"\u009E",
|
||||
u"\u009F",
|
||||
]
|
||||
accepted_role_arn_format = re.compile(
|
||||
"arn:aws:iam::(?P<account_id>[0-9]{12}):role/.+"
|
||||
)
|
||||
accepted_mchn_arn_format = re.compile(
|
||||
"arn:aws:states:[-0-9a-zA-Z]+:(?P<account_id>[0-9]{12}):stateMachine:.+"
|
||||
)
|
||||
accepted_exec_arn_format = re.compile(
|
||||
"arn:aws:states:[-0-9a-zA-Z]+:(?P<account_id>[0-9]{12}):execution:.+"
|
||||
)
|
||||
|
||||
def __init__(self, region_name):
|
||||
self.state_machines = []
|
||||
|
|
@ -72,7 +168,14 @@ class StepFunctionBackend(BaseBackend):
|
|||
def create_state_machine(self, name, definition, roleArn, tags=None):
|
||||
self._validate_name(name)
|
||||
self._validate_role_arn(roleArn)
|
||||
arn = 'arn:aws:states:' + self.region_name + ':' + str(self._get_account_id()) + ':stateMachine:' + name
|
||||
arn = (
|
||||
"arn:aws:states:"
|
||||
+ self.region_name
|
||||
+ ":"
|
||||
+ str(self._get_account_id())
|
||||
+ ":stateMachine:"
|
||||
+ name
|
||||
)
|
||||
try:
|
||||
return self.describe_state_machine(arn)
|
||||
except StateMachineDoesNotExist:
|
||||
|
|
@ -87,7 +190,9 @@ class StepFunctionBackend(BaseBackend):
|
|||
self._validate_machine_arn(arn)
|
||||
sm = next((x for x in self.state_machines if x.arn == arn), None)
|
||||
if not sm:
|
||||
raise StateMachineDoesNotExist("State Machine Does Not Exist: '" + arn + "'")
|
||||
raise StateMachineDoesNotExist(
|
||||
"State Machine Does Not Exist: '" + arn + "'"
|
||||
)
|
||||
return sm
|
||||
|
||||
def delete_state_machine(self, arn):
|
||||
|
|
@ -98,23 +203,33 @@ class StepFunctionBackend(BaseBackend):
|
|||
|
||||
def start_execution(self, state_machine_arn, name=None):
|
||||
state_machine_name = self.describe_state_machine(state_machine_arn).name
|
||||
execution = Execution(region_name=self.region_name,
|
||||
account_id=self._get_account_id(),
|
||||
state_machine_name=state_machine_name,
|
||||
execution_name=name or str(uuid4()),
|
||||
state_machine_arn=state_machine_arn)
|
||||
execution = Execution(
|
||||
region_name=self.region_name,
|
||||
account_id=self._get_account_id(),
|
||||
state_machine_name=state_machine_name,
|
||||
execution_name=name or str(uuid4()),
|
||||
state_machine_arn=state_machine_arn,
|
||||
)
|
||||
self.executions.append(execution)
|
||||
return execution
|
||||
|
||||
def stop_execution(self, execution_arn):
|
||||
execution = next((x for x in self.executions if x.execution_arn == execution_arn), None)
|
||||
execution = next(
|
||||
(x for x in self.executions if x.execution_arn == execution_arn), None
|
||||
)
|
||||
if not execution:
|
||||
raise ExecutionDoesNotExist("Execution Does Not Exist: '" + execution_arn + "'")
|
||||
raise ExecutionDoesNotExist(
|
||||
"Execution Does Not Exist: '" + execution_arn + "'"
|
||||
)
|
||||
execution.stop()
|
||||
return execution
|
||||
|
||||
def list_executions(self, state_machine_arn):
|
||||
return [execution for execution in self.executions if execution.state_machine_arn == state_machine_arn]
|
||||
return [
|
||||
execution
|
||||
for execution in self.executions
|
||||
if execution.state_machine_arn == state_machine_arn
|
||||
]
|
||||
|
||||
def describe_execution(self, arn):
|
||||
self._validate_execution_arn(arn)
|
||||
|
|
@ -136,19 +251,25 @@ class StepFunctionBackend(BaseBackend):
|
|||
raise InvalidName("Invalid Name: '" + name + "'")
|
||||
|
||||
def _validate_role_arn(self, role_arn):
|
||||
self._validate_arn(arn=role_arn,
|
||||
regex=self.accepted_role_arn_format,
|
||||
invalid_msg="Invalid Role Arn: '" + role_arn + "'")
|
||||
self._validate_arn(
|
||||
arn=role_arn,
|
||||
regex=self.accepted_role_arn_format,
|
||||
invalid_msg="Invalid Role Arn: '" + role_arn + "'",
|
||||
)
|
||||
|
||||
def _validate_machine_arn(self, machine_arn):
|
||||
self._validate_arn(arn=machine_arn,
|
||||
regex=self.accepted_mchn_arn_format,
|
||||
invalid_msg="Invalid State Machine Arn: '" + machine_arn + "'")
|
||||
self._validate_arn(
|
||||
arn=machine_arn,
|
||||
regex=self.accepted_mchn_arn_format,
|
||||
invalid_msg="Invalid State Machine Arn: '" + machine_arn + "'",
|
||||
)
|
||||
|
||||
def _validate_execution_arn(self, execution_arn):
|
||||
self._validate_arn(arn=execution_arn,
|
||||
regex=self.accepted_exec_arn_format,
|
||||
invalid_msg="Execution Does Not Exist: '" + execution_arn + "'")
|
||||
self._validate_arn(
|
||||
arn=execution_arn,
|
||||
regex=self.accepted_exec_arn_format,
|
||||
invalid_msg="Execution Does Not Exist: '" + execution_arn + "'",
|
||||
)
|
||||
|
||||
def _validate_arn(self, arn, regex, invalid_msg):
|
||||
match = regex.match(arn)
|
||||
|
|
@ -159,4 +280,7 @@ class StepFunctionBackend(BaseBackend):
|
|||
return ACCOUNT_ID
|
||||
|
||||
|
||||
stepfunction_backends = {_region.name: StepFunctionBackend(_region.name) for _region in boto.awslambda.regions()}
|
||||
stepfunction_backends = {
|
||||
_region.name: StepFunctionBackend(_region.name)
|
||||
for _region in boto.awslambda.regions()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,24 +9,23 @@ from .models import stepfunction_backends
|
|||
|
||||
|
||||
class StepFunctionResponse(BaseResponse):
|
||||
|
||||
@property
|
||||
def stepfunction_backend(self):
|
||||
return stepfunction_backends[self.region]
|
||||
|
||||
@amzn_request_id
|
||||
def create_state_machine(self):
|
||||
name = self._get_param('name')
|
||||
definition = self._get_param('definition')
|
||||
roleArn = self._get_param('roleArn')
|
||||
tags = self._get_param('tags')
|
||||
name = self._get_param("name")
|
||||
definition = self._get_param("definition")
|
||||
roleArn = self._get_param("roleArn")
|
||||
tags = self._get_param("tags")
|
||||
try:
|
||||
state_machine = self.stepfunction_backend.create_state_machine(name=name, definition=definition,
|
||||
roleArn=roleArn,
|
||||
tags=tags)
|
||||
state_machine = self.stepfunction_backend.create_state_machine(
|
||||
name=name, definition=definition, roleArn=roleArn, tags=tags
|
||||
)
|
||||
response = {
|
||||
'creationDate': state_machine.creation_date,
|
||||
'stateMachineArn': state_machine.arn
|
||||
"creationDate": state_machine.creation_date,
|
||||
"stateMachineArn": state_machine.arn,
|
||||
}
|
||||
return 200, {}, json.dumps(response)
|
||||
except AWSError as err:
|
||||
|
|
@ -35,29 +34,38 @@ class StepFunctionResponse(BaseResponse):
|
|||
@amzn_request_id
|
||||
def list_state_machines(self):
|
||||
list_all = self.stepfunction_backend.list_state_machines()
|
||||
list_all = sorted([{'creationDate': sm.creation_date,
|
||||
'name': sm.name,
|
||||
'stateMachineArn': sm.arn} for sm in list_all],
|
||||
key=lambda x: x['name'])
|
||||
response = {'stateMachines': list_all}
|
||||
list_all = sorted(
|
||||
[
|
||||
{
|
||||
"creationDate": sm.creation_date,
|
||||
"name": sm.name,
|
||||
"stateMachineArn": sm.arn,
|
||||
}
|
||||
for sm in list_all
|
||||
],
|
||||
key=lambda x: x["name"],
|
||||
)
|
||||
response = {"stateMachines": list_all}
|
||||
return 200, {}, json.dumps(response)
|
||||
|
||||
@amzn_request_id
|
||||
def describe_state_machine(self):
|
||||
arn = self._get_param('stateMachineArn')
|
||||
arn = self._get_param("stateMachineArn")
|
||||
return self._describe_state_machine(arn)
|
||||
|
||||
@amzn_request_id
|
||||
def _describe_state_machine(self, state_machine_arn):
|
||||
try:
|
||||
state_machine = self.stepfunction_backend.describe_state_machine(state_machine_arn)
|
||||
state_machine = self.stepfunction_backend.describe_state_machine(
|
||||
state_machine_arn
|
||||
)
|
||||
response = {
|
||||
'creationDate': state_machine.creation_date,
|
||||
'stateMachineArn': state_machine.arn,
|
||||
'definition': state_machine.definition,
|
||||
'name': state_machine.name,
|
||||
'roleArn': state_machine.roleArn,
|
||||
'status': 'ACTIVE'
|
||||
"creationDate": state_machine.creation_date,
|
||||
"stateMachineArn": state_machine.arn,
|
||||
"definition": state_machine.definition,
|
||||
"name": state_machine.name,
|
||||
"roleArn": state_machine.roleArn,
|
||||
"status": "ACTIVE",
|
||||
}
|
||||
return 200, {}, json.dumps(response)
|
||||
except AWSError as err:
|
||||
|
|
@ -65,58 +73,65 @@ class StepFunctionResponse(BaseResponse):
|
|||
|
||||
@amzn_request_id
|
||||
def delete_state_machine(self):
|
||||
arn = self._get_param('stateMachineArn')
|
||||
arn = self._get_param("stateMachineArn")
|
||||
try:
|
||||
self.stepfunction_backend.delete_state_machine(arn)
|
||||
return 200, {}, json.dumps('{}')
|
||||
return 200, {}, json.dumps("{}")
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
@amzn_request_id
|
||||
def list_tags_for_resource(self):
|
||||
arn = self._get_param('resourceArn')
|
||||
arn = self._get_param("resourceArn")
|
||||
try:
|
||||
state_machine = self.stepfunction_backend.describe_state_machine(arn)
|
||||
tags = state_machine.tags or []
|
||||
except AWSError:
|
||||
tags = []
|
||||
response = {'tags': tags}
|
||||
response = {"tags": tags}
|
||||
return 200, {}, json.dumps(response)
|
||||
|
||||
@amzn_request_id
|
||||
def start_execution(self):
|
||||
arn = self._get_param('stateMachineArn')
|
||||
name = self._get_param('name')
|
||||
arn = self._get_param("stateMachineArn")
|
||||
name = self._get_param("name")
|
||||
execution = self.stepfunction_backend.start_execution(arn, name)
|
||||
response = {'executionArn': execution.execution_arn,
|
||||
'startDate': execution.start_date}
|
||||
response = {
|
||||
"executionArn": execution.execution_arn,
|
||||
"startDate": execution.start_date,
|
||||
}
|
||||
return 200, {}, json.dumps(response)
|
||||
|
||||
@amzn_request_id
|
||||
def list_executions(self):
|
||||
arn = self._get_param('stateMachineArn')
|
||||
arn = self._get_param("stateMachineArn")
|
||||
state_machine = self.stepfunction_backend.describe_state_machine(arn)
|
||||
executions = self.stepfunction_backend.list_executions(arn)
|
||||
executions = [{'executionArn': execution.execution_arn,
|
||||
'name': execution.name,
|
||||
'startDate': execution.start_date,
|
||||
'stateMachineArn': state_machine.arn,
|
||||
'status': execution.status} for execution in executions]
|
||||
return 200, {}, json.dumps({'executions': executions})
|
||||
executions = [
|
||||
{
|
||||
"executionArn": execution.execution_arn,
|
||||
"name": execution.name,
|
||||
"startDate": execution.start_date,
|
||||
"stateMachineArn": state_machine.arn,
|
||||
"status": execution.status,
|
||||
}
|
||||
for execution in executions
|
||||
]
|
||||
return 200, {}, json.dumps({"executions": executions})
|
||||
|
||||
@amzn_request_id
|
||||
def describe_execution(self):
|
||||
arn = self._get_param('executionArn')
|
||||
arn = self._get_param("executionArn")
|
||||
try:
|
||||
execution = self.stepfunction_backend.describe_execution(arn)
|
||||
response = {
|
||||
'executionArn': arn,
|
||||
'input': '{}',
|
||||
'name': execution.name,
|
||||
'startDate': execution.start_date,
|
||||
'stateMachineArn': execution.state_machine_arn,
|
||||
'status': execution.status,
|
||||
'stopDate': execution.stop_date
|
||||
"executionArn": arn,
|
||||
"input": "{}",
|
||||
"name": execution.name,
|
||||
"startDate": execution.start_date,
|
||||
"stateMachineArn": execution.state_machine_arn,
|
||||
"status": execution.status,
|
||||
"stopDate": execution.stop_date,
|
||||
}
|
||||
return 200, {}, json.dumps(response)
|
||||
except AWSError as err:
|
||||
|
|
@ -124,7 +139,7 @@ class StepFunctionResponse(BaseResponse):
|
|||
|
||||
@amzn_request_id
|
||||
def describe_state_machine_for_execution(self):
|
||||
arn = self._get_param('executionArn')
|
||||
arn = self._get_param("executionArn")
|
||||
try:
|
||||
execution = self.stepfunction_backend.describe_execution(arn)
|
||||
return self._describe_state_machine(execution.state_machine_arn)
|
||||
|
|
@ -133,7 +148,7 @@ class StepFunctionResponse(BaseResponse):
|
|||
|
||||
@amzn_request_id
|
||||
def stop_execution(self):
|
||||
arn = self._get_param('executionArn')
|
||||
arn = self._get_param("executionArn")
|
||||
execution = self.stepfunction_backend.stop_execution(arn)
|
||||
response = {'stopDate': execution.stop_date}
|
||||
response = {"stopDate": execution.stop_date}
|
||||
return 200, {}, json.dumps(response)
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
from __future__ import unicode_literals
|
||||
from .responses import StepFunctionResponse
|
||||
|
||||
url_bases = [
|
||||
"https?://states.(.+).amazonaws.com",
|
||||
]
|
||||
url_bases = ["https?://states.(.+).amazonaws.com"]
|
||||
|
||||
url_paths = {
|
||||
'{0}/$': StepFunctionResponse.dispatch,
|
||||
}
|
||||
url_paths = {"{0}/$": StepFunctionResponse.dispatch}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue