Skip to content

Commit

Permalink
Cleanup - use is_boto3_error_(message|code) (ansible-collections#268)
Browse files Browse the repository at this point in the history
* Reorder imports
* Make use of is_boto3_error_message
* Mass-migration over to is_boto3_error_code
* Remove unused imports
* unused vars in exception
* Improve consistency around catching BotoCoreError and ClientError
* Remove unused imports
* Remove unused 'PolicyError' from iam_policy_info
* Avoid catching botocore.exceptions.ClientError when we only want some error codes
* Import camel_dict_to_snake_dict/snake_dict_to_camel_dict from ansible.module_utils.common.dict_transformations

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections@4cf52ef
  • Loading branch information
tremble authored and goneri committed Sep 21, 2022
1 parent a6cf50a commit 7656d46
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions plugins/modules/iam_policy_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,15 @@
'''

try:
from botocore.exceptions import BotoCoreError, ClientError
import botocore
except ImportError:
pass

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry


class PolicyError(Exception):
pass


class Policy:

def __init__(self, client, name, policy_name):
Expand Down Expand Up @@ -202,12 +199,10 @@ def main():
policy = GroupPolicy(**args)

module.exit_json(**(policy.run()))
except (BotoCoreError, ClientError) as e:
if e.response['Error']['Code'] == 'NoSuchEntity':
module.exit_json(changed=False, msg=e.response['Error']['Message'])
except is_boto3_error_code('NoSuchEntity') as e:
module.exit_json(changed=False, msg=e.response['Error']['Message'])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e)
except PolicyError as e:
module.fail_json(msg=str(e))


if __name__ == '__main__':
Expand Down

0 comments on commit 7656d46

Please sign in to comment.