Stepfunctions improvements (#3427)

* Implement filtering for stepfunctions:ListExecutions

* Add pagination to Step Functions endpoints

Implements a generalized approach to pagination via a decorator method for the
Step Functions endpoints.  Modeled on the real AWS backend behavior, `nextToken`
is a dictionary of pagination information encoded in an opaque string.

With just a bit of metadata hard-coded (`utils.PAGINATION_MODEL`), backend `list`
methods need only be decorated with `@paginate` and ensure that their returned
entities are sorted to get full pagination support without any duplicated code
polluting the model.

Closes #3137
This commit is contained in:
Brian Pandola 2020-11-01 02:16:41 -08:00 committed by GitHub
commit 68e3d394ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 284 additions and 26 deletions

View file

@ -5,7 +5,7 @@ from datetime import datetime
from boto3 import Session
from moto.core import ACCOUNT_ID, BaseBackend
from moto.core.utils import iso_8601_datetime_without_milliseconds
from moto.core.utils import iso_8601_datetime_with_milliseconds
from uuid import uuid4
from .exceptions import (
ExecutionAlreadyExists,
@ -15,11 +15,12 @@ from .exceptions import (
InvalidName,
StateMachineDoesNotExist,
)
from .utils import paginate
class StateMachine:
def __init__(self, arn, name, definition, roleArn, tags=None):
self.creation_date = iso_8601_datetime_without_milliseconds(datetime.now())
self.creation_date = iso_8601_datetime_with_milliseconds(datetime.now())
self.arn = arn
self.name = name
self.definition = definition
@ -43,7 +44,7 @@ class Execution:
)
self.execution_arn = execution_arn
self.name = execution_name
self.start_date = iso_8601_datetime_without_milliseconds(datetime.now())
self.start_date = iso_8601_datetime_with_milliseconds(datetime.now())
self.state_machine_arn = state_machine_arn
self.execution_input = execution_input
self.status = "RUNNING"
@ -51,7 +52,7 @@ class Execution:
def stop(self):
self.status = "ABORTED"
self.stop_date = iso_8601_datetime_without_milliseconds(datetime.now())
self.stop_date = iso_8601_datetime_with_milliseconds(datetime.now())
class StepFunctionBackend(BaseBackend):
@ -189,8 +190,10 @@ class StepFunctionBackend(BaseBackend):
self.state_machines.append(state_machine)
return state_machine
@paginate
def list_state_machines(self):
return self.state_machines
state_machines = sorted(self.state_machines, key=lambda x: x.creation_date)
return state_machines
def describe_state_machine(self, arn):
self._validate_machine_arn(arn)
@ -233,13 +236,20 @@ class StepFunctionBackend(BaseBackend):
execution.stop()
return execution
def list_executions(self, state_machine_arn):
return [
@paginate
def list_executions(self, state_machine_arn, status_filter=None):
executions = [
execution
for execution in self.executions
if execution.state_machine_arn == state_machine_arn
]
if status_filter:
executions = list(filter(lambda e: e.status == status_filter, executions))
executions = sorted(executions, key=lambda x: x.start_date, reverse=True)
return executions
def describe_execution(self, arn):
self._validate_execution_arn(arn)
exctn = next((x for x in self.executions if x.execution_arn == arn), None)