| 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 |
import functools |
| 7 |
import inspect |
| 8 |
|
| 9 |
try: |
| 10 |
import botocore |
| 11 |
except ImportError: |
| 12 |
pass # Modules are responsible for handling this. |
| 13 |
|
| 14 |
from .exceptions import AnsibleAWSError |
| 15 |
|
| 16 |
|
| 17 |
class AWSErrorHandler: |
| 18 |
"""_CUSTOM_EXCEPTION can be overridden by subclasses to customize the exception raised""" |
| 19 |
|
| 20 |
_CUSTOM_EXCEPTION = AnsibleAWSError |
| 21 |
|
| 22 |
@classmethod |
| 23 |
def _is_missing(cls): |
| 24 |
"""Should be overridden with a class method that returns the value from is_boto3_error_code (or similar)""" |
| 25 |
return type("NeverEverRaisedException", (Exception,), {}) |
| 26 |
|
| 27 |
@classmethod |
| 28 |
def common_error_handler(cls, description): |
| 29 |
"""A simple error handler that catches the standard Boto3 exceptions and raises |
| 30 |
an AnsibleAWSError exception. |
| 31 |
|
| 32 |
param: description: a description of the action being taken. |
| 33 |
Exception raised will include a message of |
| 34 |
f"Timeout trying to {description}" or |
| 35 |
f"Failed to {description}" |
| 36 |
Supports format string placeholders matching function parameters, |
| 37 |
e.g., "get role {name}" will interpolate the 'name' parameter. |
| 38 |
""" |
| 39 |
|
| 40 |
def wrapper(func): |
| 41 |
# Unwrap nested decorators to find the original function signature |
| 42 |
original_func = func |
| 43 |
while hasattr(original_func, "__wrapped__"): |
| 44 |
original_func = original_func.__wrapped__ |
| 45 |
sig = inspect.signature(original_func) |
| 46 |
|
| 47 |
@functools.wraps(func) |
| 48 |
def handler(*args, **kwargs): |
| 49 |
try: |
| 50 |
bound = sig.bind(*args, **kwargs) |
| 51 |
bound.apply_defaults() |
| 52 |
desc = description.format(**bound.arguments) |
| 53 |
except (KeyError, ValueError, AttributeError) as format_error: |
| 54 |
# Formatting failed - append a note to help debug the issue |
| 55 |
desc = f"{description} (error formatting message: {format_error})" |
| 56 |
|
| 57 |
try: |
| 58 |
return func(*args, **kwargs) |
| 59 |
except botocore.exceptions.WaiterError as e: |
| 60 |
raise cls._CUSTOM_EXCEPTION(message=f"Timeout trying to {desc}", exception=e) from e |
| 61 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 62 |
raise cls._CUSTOM_EXCEPTION(message=f"Failed to {desc}", exception=e) from e |
| 63 |
|
| 64 |
return handler |
| 65 |
|
| 66 |
return wrapper |
| 67 |
|
| 68 |
@classmethod |
| 69 |
def list_error_handler(cls, description, default_value=None): |
| 70 |
"""A simple error handler that catches the standard Boto3 exceptions and raises |
| 71 |
an AnsibleAWSError exception. |
| 72 |
Error codes representing a non-existent entity will result in None being returned |
| 73 |
Generally used for Get/List calls where the exception just means the resource isn't there |
| 74 |
|
| 75 |
param: description: a description of the action being taken. |
| 76 |
Exception raised will include a message of |
| 77 |
f"Timeout trying to {description}" or |
| 78 |
f"Failed to {description}" |
| 79 |
Supports format string placeholders matching function parameters, |
| 80 |
e.g., "get role {name}" will interpolate the 'name' parameter. |
| 81 |
param: default_value: the value to return if no matching |
| 82 |
resources are returned. Defaults to None |
| 83 |
""" |
| 84 |
|
| 85 |
def wrapper(func): |
| 86 |
@cls.common_error_handler(description) |
| 87 |
@functools.wraps(func) |
| 88 |
def handler(*args, **kwargs): |
| 89 |
try: |
| 90 |
return func(*args, **kwargs) |
| 91 |
except cls._is_missing(): |
| 92 |
return default_value |
| 93 |
|
| 94 |
return handler |
| 95 |
|
| 96 |
return wrapper |
| 97 |
|
| 98 |
@classmethod |
| 99 |
def deletion_error_handler(cls, description): |
| 100 |
"""A simple error handler that catches the standard Boto3 exceptions and raises |
| 101 |
an AnsibleAWSError exception. |
| 102 |
Error codes representing a non-existent entity will result in None being returned |
| 103 |
Generally used in deletion calls where NoSuchEntity means it's already gone |
| 104 |
|
| 105 |
param: description: a description of the action being taken. |
| 106 |
Exception raised will include a message of |
| 107 |
f"Timeout trying to {description}" or |
| 108 |
f"Failed to {description}" |
| 109 |
Supports format string placeholders matching function parameters, |
| 110 |
e.g., "delete role {name}" will interpolate the 'name' parameter. |
| 111 |
""" |
| 112 |
|
| 113 |
def wrapper(func): |
| 114 |
@cls.common_error_handler(description) |
| 115 |
@functools.wraps(func) |
| 116 |
def handler(*args, **kwargs): |
| 117 |
try: |
| 118 |
return func(*args, **kwargs) |
| 119 |
except cls._is_missing(): |
| 120 |
return False |
| 121 |
|
| 122 |
return handler |
| 123 |
|
| 124 |
return wrapper |