| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: Contributors to the Ansible project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
# It would be nice to be able to use autoscaling.XYZ, but we're bound by Ansible's "empty-init" |
| 7 |
# policy: https://docs.ansible.com/ansible-core/devel/dev_guide/testing/sanity/empty-init.html |
| 8 |
|
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
import typing |
| 13 |
|
| 14 |
# Not intended for general re-use / re-import |
| 15 |
from ._autoscaling import common as _common |
| 16 |
from ._autoscaling import groups as _groups |
| 17 |
from ._autoscaling import instances as _instances |
| 18 |
from ._autoscaling import transformations as _transformations |
| 19 |
from ._autoscaling import waiters as _waiters |
| 20 |
from .retries import AWSRetry |
| 21 |
|
| 22 |
if typing.TYPE_CHECKING: |
| 23 |
from typing import Any |
| 24 |
from typing import Dict |
| 25 |
from typing import List |
| 26 |
from typing import Optional |
| 27 |
|
| 28 |
from .retries import RetryingBotoClientWrapper |
| 29 |
from .transformation import AnsibleAWSResourceList |
| 30 |
from .transformation import BotoResourceList |
| 31 |
|
| 32 |
# Intended for general use / re-import |
| 33 |
AnsibleAutoScalingError = _common.AnsibleAutoScalingError |
| 34 |
AutoScalingErrorHandler = _common.AutoScalingErrorHandler |
| 35 |
WAITER_MAP = _waiters.WAITER_MAP |
| 36 |
|
| 37 |
|
| 38 |
def get_autoscaling_groups( |
| 39 |
client: RetryingBotoClientWrapper, group_names: Optional[List[str]] = None |
| 40 |
) -> AnsibleAWSResourceList: |
| 41 |
groups = _groups.describe_auto_scaling_groups(client, group_names) |
| 42 |
return _transformations.normalize_autoscaling_groups(groups) |
| 43 |
|
| 44 |
|
| 45 |
def _get_autoscaling_instances( |
| 46 |
client: RetryingBotoClientWrapper, instance_ids: Optional[List[str]] = None, group_name: Optional[str] = None |
| 47 |
) -> BotoResourceList: |
| 48 |
if group_name: |
| 49 |
try: |
| 50 |
groups = _groups.describe_auto_scaling_groups(client, [group_name]) |
| 51 |
return groups[0]["Instances"] |
| 52 |
except (KeyError, IndexError): |
| 53 |
return None |
| 54 |
return _instances.describe_auto_scaling_instances(client, instance_ids) |
| 55 |
|
| 56 |
|
| 57 |
def get_autoscaling_instances( |
| 58 |
client: RetryingBotoClientWrapper, instance_ids: Optional[List[str]] = None, group_name: Optional[str] = None |
| 59 |
) -> AnsibleAWSResourceList: |
| 60 |
instances = _get_autoscaling_instances(client, instance_ids=instance_ids, group_name=group_name) |
| 61 |
return _transformations.normalize_autoscaling_instances(instances, group_name=group_name) |
| 62 |
|
| 63 |
|
| 64 |
def get_autoscaling_waiter(client: RetryingBotoClientWrapper, waiter_name: str) -> Any: |
| 65 |
return _waiters.waiter_factory.get_waiter(client, waiter_name) |
| 66 |
|
| 67 |
|
| 68 |
# ==================================== |
| 69 |
# TODO Move these about and refactor |
| 70 |
# ==================================== |
| 71 |
|
| 72 |
|
| 73 |
@AutoScalingErrorHandler.list_error_handler("describe InstanceRefreshes", {}) |
| 74 |
@AWSRetry.jittered_backoff() |
| 75 |
def describe_instance_refreshes( |
| 76 |
client, |
| 77 |
auto_scaling_group_name: str, |
| 78 |
instance_refresh_ids: Optional[List[str]] = None, |
| 79 |
next_token: Optional[str] = None, |
| 80 |
max_records: Optional[int] = None, |
| 81 |
) -> Dict[str, Any]: |
| 82 |
params = {"AutoScalingGroupName": auto_scaling_group_name} |
| 83 |
if instance_refresh_ids: |
| 84 |
params["InstanceRefreshIds"] = instance_refresh_ids |
| 85 |
if next_token: |
| 86 |
params["NextToken"] = next_token |
| 87 |
if max_records: |
| 88 |
params["MaxRecords"] = max_records |
| 89 |
return client.describe_instance_refreshes(**params) |
| 90 |
|
| 91 |
|
| 92 |
@AutoScalingErrorHandler.common_error_handler("start InstanceRefresh") |
| 93 |
@AWSRetry.jittered_backoff(catch_extra_error_codes=["InstanceRefreshInProgress"]) |
| 94 |
def start_instance_refresh(client, auto_scaling_group_name: str, **params: Dict[str, Any]) -> str: |
| 95 |
return client.start_instance_refresh(AutoScalingGroupName=auto_scaling_group_name, **params).get( |
| 96 |
"InstanceRefreshId" |
| 97 |
) |
| 98 |
|
| 99 |
|
| 100 |
@AutoScalingErrorHandler.common_error_handler("cancel InstanceRefresh") |
| 101 |
@AWSRetry.jittered_backoff(catch_extra_error_codes=["InstanceRefreshInProgress"]) |
| 102 |
def cancel_instance_refresh(client, auto_scaling_group_name: str) -> str: |
| 103 |
return client.cancel_instance_refresh(AutoScalingGroupName=auto_scaling_group_name).get("InstanceRefreshId") |