Skip to content

Commit

Permalink
added module_utils for autoscaling (ansible-collections#2301)
Browse files Browse the repository at this point in the history
SUMMARY
This is needed to prepare the promotion of community.aws.autoscaling_instance_refresh and community.aws.autoscaling_instance_refresh_info modules
ISSUE TYPE


Feature Pull Request

COMPONENT NAME

module_utils/autoscaling

Reviewed-by: GomathiselviS
Reviewed-by: Alina Buzachis
  • Loading branch information
abikouo authored Sep 24, 2024
1 parent 14d447b commit 5c636b3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/20240920-module_utils-autoscaling.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- module_utils/autoscaling - create utils to handle AWS call for the ``autoscaling`` client (https://github.com/ansible-collections/amazon.aws/pull/2301).
59 changes: 59 additions & 0 deletions plugins/module_utils/autoscaling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2024 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from typing import Any
from typing import Dict
from typing import List
from typing import Optional

from .botocore import is_boto3_error_code
from .errors import AWSErrorHandler
from .exceptions import AnsibleAWSError
from .retries import AWSRetry


class AnsibleAutoScalingError(AnsibleAWSError):
pass


class AutoScalingErrorHandler(AWSErrorHandler):
_CUSTOM_EXCEPTION = AnsibleAutoScalingError

@classmethod
def _is_missing(cls):
return is_boto3_error_code("")


@AutoScalingErrorHandler.list_error_handler("describe InstanceRefreshes", {})
@AWSRetry.jittered_backoff()
def describe_instance_refreshes(
client,
auto_scaling_group_name: str,
instance_refresh_ids: Optional[List[str]] = None,
next_token: Optional[str] = None,
max_records: Optional[int] = None,
) -> Dict[str, Any]:
params = {"AutoScalingGroupName": auto_scaling_group_name}
if instance_refresh_ids:
params["InstanceRefreshIds"] = instance_refresh_ids
if next_token:
params["NextToken"] = next_token
if max_records:
params["MaxRecords"] = max_records
return client.describe_instance_refreshes(**params)


@AutoScalingErrorHandler.common_error_handler("start InstanceRefresh")
@AWSRetry.jittered_backoff(catch_extra_error_codes=["InstanceRefreshInProgress"])
def start_instance_refresh(client, auto_scaling_group_name: str, **params: Dict[str, Any]) -> str:
return client.start_instance_refresh(AutoScalingGroupName=auto_scaling_group_name, **params).get(
"InstanceRefreshId"
)


@AutoScalingErrorHandler.common_error_handler("cancel InstanceRefresh")
@AWSRetry.jittered_backoff(catch_extra_error_codes=["InstanceRefreshInProgress"])
def cancel_instance_refresh(client, auto_scaling_group_name: str) -> str:
return client.cancel_instance_refresh(AutoScalingGroupName=auto_scaling_group_name).get("InstanceRefreshId")

0 comments on commit 5c636b3

Please sign in to comment.