Exemple #1
0
def _next_and_end(cls: "StateMirror") -> "StateMirror":
    """Add "Next" and "End" parameters to the class.
    Also adds the "then()" and "end()" helper methods.
    """
    def _validate_next(instance, attribute: attr.Attribute, value: Any):
        if value is not None and instance.End is not None:
            raise ValueError("Only one of 'Next' and 'End' is allowed")

    cls.Next = RHODES_ATTRIB(validator=(optional(instance_of(str)),
                                        _validate_next))
    cls.__doc__ = docstring_with_param(
        cls, "Next", description="The state that will follow this state")

    def _validate_end(instance, attribute: attr.Attribute, value: Any):
        if value is not None and instance.Next is not None:
            raise ValueError("Only one of 'Next' and 'End' is allowed")

        if value is not None and value is not True:
            raise ValueError("If 'End' is set, value must be True")

    cls.End = RHODES_ATTRIB(validator=(optional(instance_of(bool)),
                                       _validate_end))
    cls.__doc__ = docstring_with_param(
        cls, "End", bool, description="This state is a terminal state")

    def _then(instance, next_state):
        """Set the next state in this state machine."""

        if instance.End is not None:
            raise InvalidDefinitionError(
                f"Cannot set state transition. State {instance.title!r} already has an end condition."
            )

        if instance.Next is not None:
            raise InvalidDefinitionError(
                f"Cannot set state transition. State {instance.title!r} already has a state transition."
            )

        instance.member_of.add_state(next_state)
        # TODO: set reference rather than extracting name
        instance.Next = next_state.title
        return next_state

    cls.then = _then

    def _end(instance):
        """Make this state a terminal state."""

        if instance.Next is not None:
            raise InvalidDefinitionError(
                "Cannot set end condition."
                f"State {instance.title!r} already has a state transition.")

        instance.End = True

        return instance

    cls.end = _end

    return cls
Exemple #2
0
class Not(ChoiceRule):
    """Matches only if the provided rule is false.

    :param ChoiceRule Rule: Rule that must evaluate as false
    :param Next: The state to which to continue if this rule evaluates as true
    """

    Rule = RHODES_ATTRIB(validator=instance_of(ChoiceRule))
    Next = RHODES_ATTRIB(validator=optional(instance_of(str)))

    @Rule.validator
    def _validate_rule(self, attribute, value):
        _require_choice_rule_instance(class_name=self.__class__.__name__,
                                      attribute_name=attribute.name,
                                      value=value)
        _require_no_next(class_name=self.__class__.__name__,
                         attribute_name=attribute.name,
                         value=value)

    def to_dict(self, suppress_next=False):
        """Serialize state as a dictionary."""
        if not suppress_next:
            _required_next(self)

        inner_rule = self.Rule.to_dict(suppress_next=True)
        instance_dict = dict(Not=inner_rule)

        if self.Next is not None:
            instance_dict["Next"] = self.Next

        return instance_dict
Exemple #3
0
def task_type(cls: "StateMirror") -> "StateMirror":
    """Add common parameters used by all "Task" types."""

    cls = state(cls)
    cls = _next_and_end(cls)
    cls = _input_output(cls)
    cls = _result_path(cls)
    cls = _catch_retry(cls)

    def _validate_positive_value(instance, attribute: attr.Attribute,
                                 value: int):
        if value is not None and not value > 0:
            raise ValueError(
                f"{instance.__class__.__name__} parameter '{attribute.name}' value must be positive"
            )

    # default=99999999
    cls.TimeoutSeconds = RHODES_ATTRIB(validator=(optional(instance_of(int)),
                                                  _validate_positive_value))
    cls.__doc__ = docstring_with_param(
        cls,
        "TimeoutSeconds",
        int,
        description="Maximum time that this state is allowed to run")

    cls.HeartbeatSeconds = RHODES_ATTRIB(validator=(optional(instance_of(int)),
                                                    _validate_positive_value))
    cls.__doc__ = docstring_with_param(
        cls,
        "HeartbeatSeconds",
        int,
        description=
        "Maximum time allowed between heartbeat responses from state")

    return cls
Exemple #4
0
class AmazonSageMakerCreateHyperParameterTuningJob(State):
    """Start a hyperparameter tuning job.

    `See service docs for more details.
    <https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateHyperParameterTuningJob.html>`_

    :param HyperParameterTuningJobConfig: The HyperParameterTuningJobConfig object that describes the tuning job,
       including the search strategy, the objective metric used to evaluate training jobs,
       ranges of parameters to search, and resource limits for the tuning job.
    :param HyperParameterTuningJobName: The name of the tuning job.
       This name is the prefix for the names of all training jobs that this tuning job launches.
       The name must be unique within the same AWS account and AWS Region. The name must have { } to { } characters.
       Valid characters are a-z, A-Z, 0-9, and : + = @ _ % - (hyphen). The name is not case sensitive.
    :param TrainingJobDefinition:
       The HyperParameterTrainingJobDefinition object that describes the training jobs that this tuning job launches,
       including static hyperparameters, input data configuration, output data configuration, resource configuration,
       and stopping condition.
    :param WarmStartConfig:
       Specifies the configuration for starting the hyperparameter tuning job
       using one or more previous tuning jobs as a starting point.
       The results of previous tuning jobs are used to inform which combinations of hyperparameters to search over
       in the new tuning job.
    """

    _required_fields = ()
    _resource_name = ServiceArn.SAGEMAKER_CREATE_HYPER_PARAMETER_TUNING_JOB

    # TODO: Sort out validation rules
    #  https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateHyperParameterTuningJob.html#API_CreateHyperParameterTuningJob_RequestParameters

    HyperParameterTuningJobConfig = RHODES_ATTRIB()
    HyperParameterTuningJobName = RHODES_ATTRIB()
    TrainingJobDefinition = RHODES_ATTRIB()
    WarmStartConfig = RHODES_ATTRIB()
Exemple #5
0
def _input_output(cls: "StateMirror") -> "StateMirror":
    """Add the "InputPath" and "OutputPath" parameters to the class."""

    cls.InputPath = RHODES_ATTRIB(default=JsonPath("$"),
                                  validator=optional(instance_of(JsonPath)),
                                  converter=convert_to_json_path)
    cls.__doc__ = docstring_with_param(
        cls,
        "InputPath",
        JsonPath,
        description=
        "The portion of the state input data to be used as input for the state",
        default=JsonPath("$"),
    )

    cls.OutputPath = RHODES_ATTRIB(default=JsonPath("$"),
                                   validator=optional(instance_of(JsonPath)),
                                   converter=convert_to_json_path)
    cls.__doc__ = docstring_with_param(
        cls,
        "OutputPath",
        JsonPath,
        description=
        "The portion of the state input data to be passed to the next state",
        default=JsonPath("$"),
    )

    return cls
Exemple #6
0
class AmazonDynamoDbGetItem(State):
    """Return a set of attributes for the item with the given primary key.

    `See service docs for more details.
    <https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html>`_

    :param AttributesToGet: This is a legacy parameter. Use ProjectionExpression instead.
       For more information, see AttributesToGet in the Amazon DynamoDB Developer Guide.
    :param ConsistentRead: Determines the read consistency model
    :param ExpressionAttributeNames: One or more substitution tokens for attribute names in an expression.
    :param ProjectionExpression: A string that identifies one or more attributes to retrieve from the table.
    :param ReturnConsumedCapacity: Determines the level of detail about provisioned throughput consumption
       that is returned in the response
    """

    _required_fields = (
        RequiredValue("Key",
                      "Amazon DynamoDB GetItem Task requires a key value"),
        RequiredValue("TableName",
                      "Amazon DynamoDB GetItem Task requires a table value"),
    )
    _resource_name = ServiceArn.DYNAMODB_GET_ITEM

    # TODO: Sort out validation rules
    #  https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html#DDB-GetItem-request-Key

    AttributesToGet = RHODES_ATTRIB()
    ConsistentRead = RHODES_ATTRIB()
    ExpressionAttributeNames = RHODES_ATTRIB()
    ProjectionExpression = RHODES_ATTRIB()
    ReturnConsumedCapacity = RHODES_ATTRIB()
Exemple #7
0
class AmazonDynamoDbUpdateItem(State):
    """Edit an existing item's attributes or add a new item to the table if it does not already exist.

    `See service docs for more details.
    <https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html>`_

    :param AttributeUpdates: This is a legacy parameter. Use UpdateExpression instead.
       For more information, see AttributeUpdates in the Amazon DynamoDB Developer Guide.
    :param UpdateExpression: An expression that defines one or more attributes to be updated,
       the action to be performed on them, and new values for them.
    """

    _required_fields = (
        RequiredValue("Key",
                      "Amazon DynamoDB UpdateItem Task requires a key value"),
        RequiredValue(
            "TableName",
            "Amazon DynamoDB UpdateItem Task requires a table value"),
    )
    _resource_name = ServiceArn.DYNAMODB_UPDATE_ITEM

    # TODO: Sort out validation rules
    #  https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html#API_UpdateItem_RequestParameters

    AttributeUpdates = RHODES_ATTRIB()
    UpdateExpression = RHODES_ATTRIB()
Exemple #8
0
def _catch_retry(cls: "StateMirror") -> "StateMirror":
    """Add the "Catch" and "Retry" parameters to the class."""
    cls.Retry = RHODES_ATTRIB()
    cls.__doc__ = docstring_with_param(cls, "Retry")

    cls.Catch = RHODES_ATTRIB()
    cls.__doc__ = docstring_with_param(cls, "Catch")

    return cls
Exemple #9
0
class Fail(State):
    """A Fail state stops the execution of the state machine and marks it as a failure.

    `See Step Functions docs for more details.
    <https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-fail-state.html>`_

    """

    Error: Optional[str] = RHODES_ATTRIB(validator=optional(instance_of(str)))
    Cause: Optional[str] = RHODES_ATTRIB(validator=optional(instance_of(str)))
Exemple #10
0
def _number(cls):
    cls = _single(cls)

    def _numeric_converter(value) -> Decimal:
        if isinstance(value, Decimal):
            return value

        return Decimal(str(value))

    def _value_serializer(instance) -> float:
        return float(instance.Value)

    # TODO: Note that for interoperability,
    #  numeric comparisons should not be assumed to work
    #  with values outside the magnitude or precision
    #  representable using the IEEE 754-2008 “binary64” data type.
    #  In particular,
    #  integers outside of the range [-(253)+1, (253)-1]
    #  might fail to compare in the expected way.
    cls.Value = RHODES_ATTRIB(validator=instance_of(Decimal),
                              converter=_numeric_converter)
    cls.__doc__ = docstring_with_param(
        cls, "Value", description="The value to which to compare ``Variable``")

    cls._serialized_value = _value_serializer

    return cls
Exemple #11
0
class Parallel(State):
    """The Parallel state can be used to create parallel branches of execution in your state machine.

    `See Step Functions docs for more details.
    <https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-parallel-state.html>`_

    """

    # TODO: Each branch MUST be a self-contained state machine.
    Branches: List[StateMachine] = RHODES_ATTRIB(default=attr.Factory(list))

    def to_dict(self) -> Dict:
        """Serialize state as a dictionary."""
        self_dict = super(Parallel, self).to_dict()

        for pos, branch in enumerate(self_dict["Branches"]):
            self_dict["Branches"][pos] = branch.to_dict()

        return self_dict

    def add_branch(
            self,
            state_machine: Optional[StateMachine] = None) -> StateMachine:
        """Add a parallel branch to this state.
        If ``state_machine`` is not provided, we generate an empty state machine and add that.

        :param state_machine: State machine to add (optional)
        :return: ``state_machine`` if provided or a new empty state machine if not
        """
        if state_machine is None:
            state_machine = StateMachine()

        self.Branches.append(state_machine)
        return state_machine
Exemple #12
0
def _endpoint_name(cls: StateMirror) -> StateMirror:
    cls.EndpointName = RHODES_ATTRIB()
    cls.__doc__ = docstring_with_param(cls,
                                       "EndpointName",
                                       description="The name of the endpoint.")

    return cls
Exemple #13
0
def _endpoint_config_name(cls: StateMirror) -> StateMirror:
    cls.EndpointConfigName = RHODES_ATTRIB()
    cls.__doc__ = docstring_with_param(
        cls,
        "EndpointConfigName",
        description="The name of an endpoint configuration.")

    return cls
Exemple #14
0
class AwsGlue(State):
    """Start a job run using a job definition.

    `See service docs for more details.
    <https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-jobs-runs.html#aws-glue-api-jobs-runs-StartJobRun>`_

    :param JobName: The name of the job definition to use.
    :param JobRunId: The ID of a previous JobRun to retry.
    :param Arguments: The job arguments specifically for this run.
    :param AllocatedCapacity: This field is deprecated. Use MaxCapacity instead.
       The number of AWS Glue data processing units (DPUs) to allocate to this JobRun.
    :param Timeout: The JobRun timeout in minutes.
    :param SecurityConfiguration: The name of the SecurityConfiguration structure to be used with this job run.
    :param NotificationProperty: Specifies configuration properties of a job run notification.
    """

    _required_fields = (RequiredValue("JobName",
                                      "AWS Glue requires a job name."), )
    _resource_name = ServiceArn.GLUE

    # TODO: Sort out validation rules
    #  https://docs.aws.amazon.com/step-functions/latest/dg/connect-glue.html

    JobName = RHODES_ATTRIB()
    JobRunId = RHODES_ATTRIB()
    Arguments = RHODES_ATTRIB()
    # TODO: AllocatedCapacity is deprecated; Will SFn's integration change?
    AllocatedCapacity = RHODES_ATTRIB()
    Timeout = RHODES_ATTRIB()
    SecurityConfiguration = RHODES_ATTRIB()
    NotificationProperty = RHODES_ATTRIB()
Exemple #15
0
def _multi(cls):
    cls.Rules = RHODES_ATTRIB(validator=_validate_multi_subrules)
    cls.__doc__ = docstring_with_param(
        cls,
        "Rules",
        description="One or more :class:`ChoiceRule` to evaluate for this rule"
    )

    cls.Next = RHODES_ATTRIB(validator=optional(instance_of(str)))
    cls.__doc__ = docstring_with_param(
        cls,
        "Next",
        description=
        "The state to which to continue if this rule evaluates as true")

    cls.to_dict = _multi_to_dict

    return cls
Exemple #16
0
def _single(cls):
    cls.Variable = RHODES_ATTRIB(validator=instance_of(VariablePath),
                                 converter=_convert_to_variable_path)
    cls.__doc__ = docstring_with_param(
        cls,
        "Variable",
        VariablePath,
        description="Path to value in state input that will be evaluated")

    cls.Next = RHODES_ATTRIB(validator=optional(instance_of(str)))
    cls.__doc__ = docstring_with_param(
        cls,
        "Next",
        description=
        "The state to which to continue if this rule evaluates as true")

    cls.to_dict = _single_to_dict

    return cls
Exemple #17
0
class Pass(State):
    """A Pass state passes its input to its output without performing work.
    Pass states are useful when constructing and debugging state machines.

    `See Step Functions docs for more details.
    <https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-pass-state.html>`_

    """

    Result = RHODES_ATTRIB()
Exemple #18
0
def _ddb_key(cls: StateMirror) -> StateMirror:
    cls.Key = RHODES_ATTRIB()
    cls.__doc__ = docstring_with_param(
        cls,
        "Key",
        description=
        ("A map of attribute names to AttributeValue objects, representing the primary key of the item to retrieve."
         ),
    )

    return cls
Exemple #19
0
def _bool(cls):
    cls = _single(cls)

    cls.Value = RHODES_ATTRIB(validator=instance_of(bool))
    cls.__doc__ = docstring_with_param(
        cls,
        "Value",
        bool,
        description="The value to which to compare ``Variable``")

    return cls
Exemple #20
0
class AmazonEcs(State):
    """Start a new task using the specified task definition.

    `See service docs for more details.
    <https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html>`_

    :param Cluster: The short name or full Amazon Resource Name (ARN) of the cluster on which to run your task.
       If you do not specify a cluster, the default cluster is assumed.
    :param Group: The name of the task group to associate with the task.
       The default value is the family name of the task definition (for example, family:my-family-name).
    :param LaunchType: The launch type on which to run your task.
       For more information, see Amazon ECS Launch Types in the Amazon Elastic Container Service Developer Guide.
    :param NetworkConfiguration: The network configuration for the task.
       This parameter is required for task definitions that use the awsvpc network mode
       to receive their own elastic network interface, and it is not supported for other network modes.
       For more information, see Task Networking in the Amazon Elastic Container Service Developer Guide.
    :param Overrides: A list of container overrides in JSON format that specify the name of a container
       in the specified task definition and the overrides it should receive.
    :param PlacementConstraints: An array of placement constraint objects to use for the task.
       You can specify up to 10 constraints per task
       (including constraints in the task definition and those specified at runtime).
    :param PlacementStrategy: The placement strategy objects to use for the task.
       You can specify a maximum of five strategy rules per task.
    :param PlatformVersion: The platform version the task should run.
       A platform version is only specified for tasks using the Fargate launch type.
       If one is not specified, the LATEST platform version is used by default.
       For more information, see AWS Fargate Platform Versions in the Amazon Elastic Container Service Developer Guide.
    :param TaskDefinition: The family and revision (family:revision) or full ARN of the task definition to run.
       If a revision is not specified, the latest ACTIVE revision is used.
    """

    _required_fields = (RequiredValue("TaskDefinition", "Amazon ECS Task requires a task definition"),)
    _resource_name = ServiceArn.ECS

    # TODO: Sort out validation rules
    #  https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html#ECS-RunTask-request-cluster

    Cluster = RHODES_ATTRIB()
    Group = RHODES_ATTRIB()
    LaunchType = RHODES_ATTRIB()
    NetworkConfiguration = RHODES_ATTRIB()
    Overrides = RHODES_ATTRIB()
    PlacementConstraints = RHODES_ATTRIB()
    PlacementStrategy = RHODES_ATTRIB()
    PlatformVersion = RHODES_ATTRIB()
    TaskDefinition = RHODES_ATTRIB()
Exemple #21
0
class AmazonSageMakerCreateEndpointConfig(State):
    """Create an endpoint configuration that Amazon SageMaker hosting services uses to deploy models.

    `See service docs for more details.
    <https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateEndpointConfig.html>`_

    :param KmsKeyId: The Amazon Resource Name (ARN) of a AWS Key Management Service key
       that Amazon SageMaker uses to encrypt data on the storage volume
       attached to the ML compute instance that hosts the endpoint.
    :param ProductionVariants: An list of ProductionVariant objects,
       one for each model that you want to host at this endpoint.
    """

    _required_fields = ()
    _resource_name = ServiceArn.SAGEMAKER_CREATE_ENDPOINT_CONFIG

    # TODO: Sort out validation rules
    #  https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateEndpointConfig.html#API_CreateEndpointConfig_RequestParameters

    KmsKeyId = RHODES_ATTRIB()
    ProductionVariants = RHODES_ATTRIB()
Exemple #22
0
def _tags(cls: StateMirror) -> StateMirror:
    cls.Tags = RHODES_ATTRIB()
    cls.__doc__ = docstring_with_param(
        cls,
        "Tags",
        description=
        ("An array of key-value pairs. "
         "For more information, see Using Cost Allocation Tagsin the AWS Billing and Cost Management User Guide."
         ),
    )

    return cls
Exemple #23
0
def _parameters(cls: "StateMirror") -> "StateMirror":
    """Add the "Parameters" parameter to the class."""
    cls.Parameters = RHODES_ATTRIB(validator=optional(instance_of(Parameters)))
    cls.__doc__ = docstring_with_param(
        cls,
        "Parameters",
        Parameters,
        description=
        "Additional parameters for Step Functions to provide to connected resource",
    )

    return cls
Exemple #24
0
class AmazonSqs(State):
    """Deliver a message to the specified queue.

    `See service docs for more details.
    <https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html>`_

    :param DelaySeconds: The length of time, in seconds, for which to delay a specific message.
       Valid values: 0 to 900. Maximum: 15 minutes.
    :param MessageAttribute: Each message attribute consists of a Name, Type, and Value.
    :param MessageBody: The message to send. The maximum string size is 256 KB.
    :param MessageDeduplicationId: The token used for deduplication of sent messages.
    :param MessageGroupId: The tag that specifies that a message belongs to a specific message group.
    :param QueueUrl: The URL of the Amazon SQS queue to which a message is sent.
    """

    _required_fields = (
        RequiredValue("MessageBody",
                      "Amazon SQS Task requires a message body"),
        RequiredValue("QueueUrl", "Amazon SQS Task requires a queue url"),
    )
    _resource_name = ServiceArn.SQS

    # TODO: Sort out validation rules
    #  https://docs.aws.amazon.com/step-functions/latest/dg/connect-sqs.html

    DelaySeconds = RHODES_ATTRIB()
    MessageAttribute = RHODES_ATTRIB()
    MessageBody = RHODES_ATTRIB()
    MessageDeduplicationId = RHODES_ATTRIB()
    MessageGroupId = RHODES_ATTRIB()
    QueueUrl = RHODES_ATTRIB()
Exemple #25
0
class AmazonSageMakerCreateModel(State):
    """Create a model in Amazon SageMaker.

    `See service docs for more details.
    <https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateModel.html>`_

    :param Containers: Specifies the containers in the inference pipeline.
    :param EnableNetworkIsolation: Isolates the model container.
       No inbound or outbound network calls can be made to or from the model container.
    :param ExecutionRoleArn: The Amazon Resource Name (ARN) of the IAM role that Amazon SageMaker can assume
       to access model artifacts and docker image for deployment on ML compute instances or for batch transform jobs.
       Deploying on ML compute instances is part of model hosting. For more information, see Amazon SageMaker Roles.
    :param ModelName: The name of the new model.
    :param PrimaryContainer: The location of the primary docker image containing inference code, associated artifacts,
       and custom environment map that the inference code uses when the model is deployed for predictions.
    :param VpcConfig: A VpcConfig object that specifies the VPC that you want your model to connect to.
    """

    _required_fields = ()
    _resource_name = ServiceArn.SAGEMAKER_CREATE_MODEL

    # TODO: Sort out validation rules
    #  https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateModel.html#API_CreateModel_RequestParameters

    Containers = RHODES_ATTRIB()
    EnableNetworkIsolation = RHODES_ATTRIB()
    ExecutionRoleArn = RHODES_ATTRIB()
    ModelName = RHODES_ATTRIB()
    PrimaryContainer = RHODES_ATTRIB()
    VpcConfig = RHODES_ATTRIB()
Exemple #26
0
class Task(State):
    """A Task state represents a single unit of work performed by a state machine.

    `See Step Functions docs for more details.
    <https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-task-state.html>`_

    """

    _required_fields = [RequiredValue("Resource", "Task resource is not set.")]

    # TODO: Additional validation for strings?
    Resource = RHODES_ATTRIB(
        validator=optional(instance_of(TASK_RESOURCE_TYPES)))
Exemple #27
0
    def _decorate(cls: StateMirror) -> StateMirror:
        cls = task_type(cls)

        cls.Pattern = RHODES_ATTRIB(default=options[0], validator=in_(options))
        cls.__doc__ = docstring_with_param(
            cls,
            "Pattern",
            IntegrationPattern,
            description="Step Functions integration pattern",
            default=options[0])

        def to_dict(instance) -> Dict:
            """Serialize state as a dictionary."""
            for required in instance._required_fields:
                require_field(instance=instance, required_value=required)

            task = instance._build_task()
            return task.to_dict()

        cls.to_dict = to_dict

        def _build_task(instance) -> Task:
            task_fields = [field.name for field in attr.fields(Task)]
            field_name_blacklist = ("Pattern", )
            resource_name = instance._resource_name.value + instance.Pattern.value

            task_kwargs = {}
            parameters_kwargs = {}

            for field in attr.fields(type(instance)):
                if field.name in field_name_blacklist or field.name.startswith(
                        "_"):
                    continue

                value = getattr(instance, field.name)
                if value is None:
                    continue

                if field.name in task_fields and field.name != "Parameters":
                    task_kwargs[field.name] = value
                else:
                    parameters_kwargs[field.name] = value

            params = Parameters(**parameters_kwargs)
            return Task(Parameters=params,
                        Resource=resource_name,
                        **task_kwargs)

        cls._build_task = _build_task

        return cls
Exemple #28
0
class Map(State):
    """The Map state can be used to run a set of steps for each element of an input array.
    While the Parallel state executes multiple branches of steps using the same input,
    a Map state will execute the same steps for multiple entries of an array in the state input.

    `See Step Functions docs for more details.
    <https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-map-state.html>`_

    """

    _required_fields = [
        RequiredValue("Iterator", "Map iterator must be set."),
        RequiredValue("ItemsPath", "Map items path must be set."),
    ]

    # TODO: Iterator MUST be a self-contained state machine.
    Iterator: StateMachine = RHODES_ATTRIB(validator=instance_of(StateMachine))
    # TODO: ItemsPath MUST be a valid JSON-path
    ItemsPath: JsonPath = RHODES_ATTRIB(validator=optional(
        instance_of(JsonPath)),
                                        converter=convert_to_json_path)
    # TODO: MaxConcurrency MUST be non-negative
    MaxConcurrency: Optional[int] = RHODES_ATTRIB(
        validator=optional(instance_of(int)))
Exemple #29
0
def _result_path(cls: "StateMirror") -> "StateMirror":
    """Add the "ResultPath" parameter to the class."""
    cls.ResultPath = RHODES_ATTRIB(default=JsonPath("$"),
                                   validator=optional(instance_of(JsonPath)),
                                   converter=convert_to_json_path)
    cls.__doc__ = docstring_with_param(
        cls,
        "ResultPath",
        JsonPath,
        description=
        "Where in the state input data to place the results of this state",
        default=JsonPath("$"),
    )

    return cls
Exemple #30
0
class AwsStepFunctions(State):
    """Start a state machine execution.

    `See service docs for more details.
    <https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html>`_

    :param StateMachineArn: The AWS Step Functions state machine to invoke
    :type StateMachineArn: :class:`JsonPath`, :class:`AWSHelperFn`, str, or :class:`Enum`
    :param Input: Data to provide to the state machine as input
    :type Input: :class:`Parameters`, :class:`JsonPath`, :class:`AWSHelperFn`, dict, str, or :class:`Enum`
    """

    _required_fields = (RequiredValue(
        "StateMachineArn",
        "AWS Step Functions Task requires a state machine target"), )
    _resource_name = ServiceArn.STEP_FUNCTIONS

    StateMachineArn = RHODES_ATTRIB(validator=optional(
        instance_of(SERVICE_INTEGRATION_SIMPLE_VALUE_TYPES)))
    # SFn docs say that this needs to be a string,
    #  but in practice JSON can be provided inline in the state machine
    Input = RHODES_ATTRIB(validator=optional(
        instance_of(SERVICE_INTEGRATION_COMPLEX_VALUE_TYPES +
                    SERVICE_INTEGRATION_SIMPLE_VALUE_TYPES)))