Skip to content

Commit

Permalink
Bulk migration to fail_json_aws (ansible-collections#361)
Browse files Browse the repository at this point in the history
* Split imports and sort
* Move camel_dict_to_snake_dict imports to ansible.module_utils.common.dict_transformations
* Cleanup unused imports
* Bulk migration to fail_json_aws
* Changelog

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections/community.aws@6c88315
  • Loading branch information
tremble authored and GomathiselviS committed Sep 22, 2022
1 parent 58c724d commit e959e26
Showing 1 changed file with 19 additions and 33 deletions.
52 changes: 19 additions & 33 deletions plugins/modules/iam_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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'])
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit e959e26

Please sign in to comment.