|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import copy |
| 4 | +import datetime |
| 5 | +import random |
| 6 | +import threading |
| 7 | +from typing import Final, Optional |
| 8 | + |
| 9 | +from localstack.aws.api.stepfunctions import ( |
| 10 | + AliasDescription, |
| 11 | + Arn, |
| 12 | + CharacterRestrictedName, |
| 13 | + DescribeStateMachineAliasOutput, |
| 14 | + RoutingConfigurationList, |
| 15 | + StateMachineAliasListItem, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class Alias: |
| 20 | + _mutex: Final[threading.Lock] |
| 21 | + update_date: Optional[datetime.datetime] |
| 22 | + name: Final[CharacterRestrictedName] |
| 23 | + _description: Optional[AliasDescription] |
| 24 | + _routing_configuration_list: RoutingConfigurationList |
| 25 | + _state_machine_version_arns: list[Arn] |
| 26 | + _execution_probability_distribution: list[int] |
| 27 | + state_machine_alias_arn: Final[Arn] |
| 28 | + create_date: datetime.datetime |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + state_machine_arn: Arn, |
| 33 | + name: CharacterRestrictedName, |
| 34 | + description: Optional[AliasDescription], |
| 35 | + routing_configuration_list: RoutingConfigurationList, |
| 36 | + ): |
| 37 | + self._mutex = threading.Lock() |
| 38 | + self.update_date = None |
| 39 | + self.name = name |
| 40 | + self._description = None |
| 41 | + self.state_machine_alias_arn = f"{state_machine_arn}:{name}" |
| 42 | + self.update(description=description, routing_configuration_list=routing_configuration_list) |
| 43 | + self.create_date = self._get_mutex_date() |
| 44 | + |
| 45 | + def __hash__(self): |
| 46 | + return hash(self.state_machine_alias_arn) |
| 47 | + |
| 48 | + def __eq__(self, other): |
| 49 | + if isinstance(other, Alias): |
| 50 | + return self.is_idempotent(other=other) |
| 51 | + return False |
| 52 | + |
| 53 | + def is_idempotent(self, other: Alias) -> bool: |
| 54 | + return all( |
| 55 | + [ |
| 56 | + self.state_machine_alias_arn == other.state_machine_alias_arn, |
| 57 | + self.name == other.name, |
| 58 | + self._description == other._description, |
| 59 | + self._routing_configuration_list == other._routing_configuration_list, |
| 60 | + ] |
| 61 | + ) |
| 62 | + |
| 63 | + @staticmethod |
| 64 | + def _get_mutex_date() -> datetime.datetime: |
| 65 | + return datetime.datetime.now(tz=datetime.timezone.utc) |
| 66 | + |
| 67 | + def get_routing_configuration_list(self) -> RoutingConfigurationList: |
| 68 | + return copy.deepcopy(self._routing_configuration_list) |
| 69 | + |
| 70 | + def is_router_for(self, state_machine_version_arn: Arn) -> bool: |
| 71 | + with self._mutex: |
| 72 | + return state_machine_version_arn in self._state_machine_version_arns |
| 73 | + |
| 74 | + def update( |
| 75 | + self, |
| 76 | + description: Optional[AliasDescription], |
| 77 | + routing_configuration_list: RoutingConfigurationList, |
| 78 | + ) -> None
341A
span>: |
| 79 | + with self._mutex: |
| 80 | + self.update_date = self._get_mutex_date() |
| 81 | + |
| 82 | + if description is not None: |
| 83 | + self._description = description |
| 84 | + |
| 85 | + if routing_configuration_list: |
| 86 | + self._routing_configuration_list = routing_configuration_list |
| 87 | + self._state_machine_version_arns = list() |
| 88 | + self._execution_probability_distribution = list() |
| 89 | + for routing_configuration in routing_configuration_list: |
| 90 | + self._state_machine_version_arns.append( |
| 91 | + routing_configuration["stateMachineVersionArn"] |
| 92 | + ) |
| 93 | + self._execution_probability_distribution.append(routing_configuration["weight"]) |
| 94 | + |
| 95 | + def sample(self): |
| 96 | + with self._mutex: |
| 97 | + samples = random.choices( |
| 98 | + self._state_machine_version_arns, |
| 99 | + weights=self._execution_probability_distribution, |
| 100 | + k=1, |
| 101 | + ) |
| 102 | + state_machine_version_arn = samples[0] |
| 103 | + return state_machine_version_arn |
| 104 | + |
| 105 | + def to_description(self) -> DescribeStateMachineAliasOutput: |
| 106 | + with self._mutex: |
| 107 | + description = DescribeStateMachineAliasOutput( |
| 108 | + creationDate=self.create_date, |
| 109 | + name=self.name, |
| 110 | + description=self._description, |
| 111 | + routingConfiguration=self._routing_configuration_list, |
| 112 | + stateMachineAliasArn=self.state_machine_alias_arn, |
| 113 | + ) |
| 114 | + if self.update_date is not None: |
| 115 | + description["updateDate"] = self.update_date |
| 116 | + return description |
| 117 | + |
| 118 | + def to_item(self) -> StateMachineAliasListItem: |
| 119 | + return StateMachineAliasListItem( |
| 120 | + stateMachineAliasArn=self.state_machine_alias_arn, creationDate=self.create_date |
| 121 | + ) |
0 commit comments