Skip to content

Commit

Permalink
Bulk migration to Python 3.6 f-strings (ansible-collections#1810)
Browse files Browse the repository at this point in the history
Bulk migration to Python 3.6 f-strings

SUMMARY
We've dropped support for Python <3.6, bulk migrate to fstrings and perform some general string cleanup
A combination of

black --preview
flynt
some manual cleanup

ISSUE TYPE

Feature Pull Request

COMPONENT NAME
plugins/
tests/
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections@de33821
  • Loading branch information
tremble authored and alinabuzachis committed Oct 6, 2023
1 parent f9f21f0 commit 66fe8ff
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions plugins/modules/iam_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def create_or_update_group(connection, module):
try:
connection.detach_group_policy(GroupName=params["GroupName"], PolicyArn=policy_arn)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Couldn't detach policy from group %s" % params["GroupName"])
module.fail_json_aws(e, msg=f"Couldn't detach policy from group {params['GroupName']}")
# 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
if set(managed_policies) - set(current_attached_policies_arn_list):
Expand All @@ -274,13 +274,13 @@ def create_or_update_group(connection, module):
try:
connection.attach_group_policy(GroupName=params["GroupName"], PolicyArn=policy_arn)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Couldn't attach policy to group %s" % params["GroupName"])
module.fail_json_aws(e, msg=f"Couldn't attach policy to group {params['GroupName']}")

# Manage group memberships
try:
current_group_members = get_group(connection, module, params["GroupName"])["Users"]
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, "Couldn't get group %s" % params["GroupName"])
module.fail_json_aws(e, f"Couldn't get group {params['GroupName']}")

current_group_members_list = []
for member in current_group_members:
Expand All @@ -296,9 +296,7 @@ def create_or_update_group(connection, module):
try:
connection.remove_user_from_group(GroupName=params["GroupName"], UserName=user)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(
e, msg="Couldn't remove user %s from group %s" % (user, params["GroupName"])
)
module.fail_json_aws(e, msg=f"Couldn't remove user {user} from group {params['GroupName']}")
# If there are users to adjust that aren't in the current list, then things have changed
# Otherwise the only changes were in purging above
if set(users) - set(current_group_members_list):
Expand All @@ -309,15 +307,15 @@ def create_or_update_group(connection, module):
try:
connection.add_user_to_group(GroupName=params["GroupName"], UserName=user)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Couldn't add user %s to group %s" % (user, params["GroupName"]))
module.fail_json_aws(e, msg=f"Couldn't add user {user} to group {params['GroupName']}")
if module.check_mode:
module.exit_json(changed=changed)

# Get the group again
try:
group = get_group(connection, module, params["GroupName"])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, "Couldn't get group %s" % params["GroupName"])
module.fail_json_aws(e, f"Couldn't get group {params['GroupName']}")

module.exit_json(changed=changed, iam_group=camel_dict_to_snake_dict(group))

Expand All @@ -329,7 +327,7 @@ def destroy_group(connection, module):
try:
group = get_group(connection, module, params["GroupName"])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, "Couldn't get group %s" % params["GroupName"])
module.fail_json_aws(e, f"Couldn't get group {params['GroupName']}")
if group:
# Check mode means we would remove this group
if module.check_mode:
Expand All @@ -340,26 +338,26 @@ def destroy_group(connection, module):
for policy in get_attached_policy_list(connection, module, params["GroupName"]):
connection.detach_group_policy(GroupName=params["GroupName"], PolicyArn=policy["PolicyArn"])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Couldn't remove policy from group %s" % params["GroupName"])
module.fail_json_aws(e, msg=f"Couldn't remove policy from group {params['GroupName']}")

# Remove any users in the group otherwise deletion fails
current_group_members_list = []
try:
current_group_members = get_group(connection, module, params["GroupName"])["Users"]
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, "Couldn't get group %s" % params["GroupName"])
module.fail_json_aws(e, f"Couldn't get group {params['GroupName']}")
for member in current_group_members:
current_group_members_list.append(member["UserName"])
for user in current_group_members_list:
try:
connection.remove_user_from_group(GroupName=params["GroupName"], UserName=user)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, "Couldn't remove user %s from group %s" % (user, params["GroupName"]))
module.fail_json_aws(e, f"Couldn't remove user {user} from group {params['GroupName']}")

try:
connection.delete_group(**params)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, "Couldn't delete group %s" % params["GroupName"])
module.fail_json_aws(e, f"Couldn't delete group {params['GroupName']}")

else:
module.exit_json(changed=False)
Expand Down

0 comments on commit 66fe8ff

Please sign in to comment.