mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-02 14:40:19 -07:00
Add AWS boto3 error code exception function is_boto3_error_code (#41202)
* Add aws/core.py function to check for specific AWS error codes * Use sys.exc_info to get exception object if it isn't passed in * Allow catching exceptions with is_boto3_error_code * Replace from_code with is_boto3_error_code * Return a type that will never be raised to support stricter type comparisons in Python 3+ * Use is_boto3_error_code in aws_eks_cluster * Add duplicate-except to ignores when using is_boto3_error_code * Add is_boto3_error_code to module development guideline docs
This commit is contained in:
parent
269f404121
commit
40d2df0ef3
12 changed files with 84 additions and 41 deletions
|
@ -69,7 +69,7 @@ try:
|
|||
except ImportError:
|
||||
pass # handled by AnsibleAWSModule
|
||||
|
||||
from ansible.module_utils.aws.core import AnsibleAWSModule
|
||||
from ansible.module_utils.aws.core import AnsibleAWSModule, is_boto3_error_code
|
||||
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info, AWSRetry
|
||||
from ansible.module_utils.ec2 import camel_dict_to_snake_dict, boto3_tag_list_to_ansible_dict
|
||||
|
||||
|
@ -88,9 +88,9 @@ def resource_exists(client, module, params):
|
|||
aws_retry=True,
|
||||
)
|
||||
return channel['DeliveryChannels'][0]
|
||||
except client.exceptions.from_code('NoSuchDeliveryChannelException'):
|
||||
except is_boto3_error_code('NoSuchDeliveryChannelException'):
|
||||
return
|
||||
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
|
||||
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
|
||||
module.fail_json_aws(e)
|
||||
|
||||
|
||||
|
@ -104,12 +104,12 @@ def create_resource(client, module, params, result):
|
|||
result['changed'] = True
|
||||
result['channel'] = camel_dict_to_snake_dict(resource_exists(client, module, params))
|
||||
return result
|
||||
except client.exceptions.from_code('InvalidS3KeyPrefixException') as e:
|
||||
except is_boto3_error_code('InvalidS3KeyPrefixException') as e:
|
||||
module.fail_json_aws(e, msg="The `s3_prefix` parameter was invalid. Try '/' for no prefix")
|
||||
except client.exceptions.from_code('InsufficientDeliveryPolicyException') as e:
|
||||
except is_boto3_error_code('InsufficientDeliveryPolicyException') as e: # pylint: disable=duplicate-except
|
||||
module.fail_json_aws(e, msg="The `s3_prefix` or `s3_bucket` parameter is invalid. "
|
||||
"Make sure the bucket exists and is available")
|
||||
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
|
||||
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
|
||||
module.fail_json_aws(e, msg="Couldn't create AWS Config delivery channel")
|
||||
|
||||
|
||||
|
@ -129,12 +129,12 @@ def update_resource(client, module, params, result):
|
|||
result['changed'] = True
|
||||
result['channel'] = camel_dict_to_snake_dict(resource_exists(client, module, params))
|
||||
return result
|
||||
except client.exceptions.from_code('InvalidS3KeyPrefixException') as e:
|
||||
except is_boto3_error_code('InvalidS3KeyPrefixException') as e:
|
||||
module.fail_json_aws(e, msg="The `s3_prefix` parameter was invalid. Try '/' for no prefix")
|
||||
except client.exceptions.from_code('InsufficientDeliveryPolicyException') as e:
|
||||
except is_boto3_error_code('InsufficientDeliveryPolicyException') as e: # pylint: disable=duplicate-except
|
||||
module.fail_json_aws(e, msg="The `s3_prefix` or `s3_bucket` parameter is invalid. "
|
||||
"Make sure the bucket exists and is available")
|
||||
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
|
||||
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
|
||||
module.fail_json_aws(e, msg="Couldn't create AWS Config delivery channel")
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue