diff --git a/plugins/modules/iam_user.py b/plugins/modules/iam_user.py index 6b8efcda811..7bd8ebda423 100644 --- a/plugins/modules/iam_user.py +++ b/plugins/modules/iam_user.py @@ -106,17 +106,16 @@ sample: / ''' -from ansible.module_utils._text import to_native -from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule -from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict - -import traceback - try: - from botocore.exceptions import ClientError, ParamValidationError, BotoCoreError + import botocore except ImportError: pass # caught by AnsibleAWSModule +from ansible.module_utils._text import to_native +from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict + +from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule + def compare_attached_policies(current_attached_policies, new_attached_policies): @@ -176,11 +175,8 @@ def create_or_update_user(connection, module): try: connection.create_user(**params) changed = True - except ClientError as e: - module.fail_json(msg="Unable to create user: {0}".format(to_native(e)), exception=traceback.format_exc(), - **camel_dict_to_snake_dict(e.response)) - except ParamValidationError as e: - module.fail_json(msg="Unable to create user: {0}".format(to_native(e)), exception=traceback.format_exc()) + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + module.fail_json_aws(e, msg="Unable to create user") # Manage managed policies current_attached_policies = get_attached_policy_list(connection, module, params['UserName']) @@ -197,14 +193,9 @@ def create_or_update_user(connection, module): if not module.check_mode: try: connection.detach_user_policy(UserName=params['UserName'], PolicyArn=policy_arn) - except ClientError as e: - module.fail_json(msg="Unable to detach policy {0} from user {1}: {2}".format( - policy_arn, params['UserName'], to_native(e)), - exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) - except ParamValidationError as e: - module.fail_json(msg="Unable to detach policy {0} from user {1}: {2}".format( - policy_arn, params['UserName'], to_native(e)), - exception=traceback.format_exc()) + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + module.fail_json_aws(e, msg="Unable to detach policy {0} from user {1}".format( + policy_arn, params['UserName'])) # If there are policies to adjust that aren't in the current list, then things have changed # Otherwise the only changes were in purging above @@ -215,14 +206,9 @@ def create_or_update_user(connection, module): for policy_arn in managed_policies: try: connection.attach_user_policy(UserName=params['UserName'], PolicyArn=policy_arn) - except ClientError as e: - module.fail_json(msg="Unable to attach policy {0} to user {1}: {2}".format( - policy_arn, params['UserName'], to_native(e)), - exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) - except ParamValidationError as e: - module.fail_json(msg="Unable to attach policy {0} to user {1}: {2}".format( - policy_arn, params['UserName'], to_native(e)), - exception=traceback.format_exc()) + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + module.fail_json_aws(e, msg="Unable to attach policy {0} to user {1}".format( + policy_arn, params['UserName'])) if module.check_mode: module.exit_json(changed=changed) @@ -249,7 +235,7 @@ def destroy_user(connection, module): try: for policy in get_attached_policy_list(connection, module, user_name): connection.detach_user_policy(UserName=user_name, PolicyArn=policy['PolicyArn']) - except (ClientError, BotoCoreError) as e: + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Unable to delete user {0}".format(user_name)) try: @@ -298,7 +284,7 @@ def destroy_user(connection, module): connection.remove_user_from_group(UserName=user_name, GroupName=group["GroupName"]) connection.delete_user(UserName=user_name) - except (ClientError, BotoCoreError) as e: + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Unable to delete user {0}".format(user_name)) module.exit_json(changed=True) @@ -311,7 +297,7 @@ def get_user(connection, module, name): try: return connection.get_user(**params) - except ClientError as e: + except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == 'NoSuchEntity': return None else: @@ -323,7 +309,7 @@ def get_attached_policy_list(connection, module, name): try: return connection.list_attached_user_policies(UserName=name)['AttachedPolicies'] - except ClientError as e: + except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == 'NoSuchEntity': return None else: @@ -334,7 +320,7 @@ def delete_user_login_profile(connection, module, user_name): try: return connection.delete_login_profile(UserName=user_name) - except ClientError as e: + except botocore.exceptions.ClientError as e: if e.response["Error"]["Code"] == "NoSuchEntity": return None else: