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/community.aws@de33821
  • Loading branch information
tremble authored and abikouo committed Oct 18, 2024
1 parent 1d059f0 commit a14f58c
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions plugins/modules/ec2_launch_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,11 @@ def determine_iam_role(module, name_or_arn):
role = iam.get_instance_profile(InstanceProfileName=name_or_arn, aws_retry=True)
return {"arn": role["InstanceProfile"]["Arn"]}
except is_boto3_error_code("NoSuchEntity") as e:
module.fail_json_aws(e, msg="Could not find instance_role {0}".format(name_or_arn))
module.fail_json_aws(e, msg=f"Could not find instance_role {name_or_arn}")
except (BotoCoreError, ClientError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(
e,
msg="An error occurred while searching for instance_role {0}. Please try supplying the full ARN.".format(
name_or_arn
),
msg=f"An error occurred while searching for instance_role {name_or_arn}. Please try supplying the full ARN.",
)


Expand All @@ -481,15 +479,18 @@ def existing_templates(module):
except is_boto3_error_code("InvalidLaunchTemplateId.Malformed") as e: # pylint: disable=duplicate-except
module.fail_json_aws(
e,
msg="Launch template with ID {0} is not a valid ID. It should start with `lt-....`".format(
module.params.get("launch_template_id")
msg=(
f"Launch template with ID {module.params.get('launch_template_id')} is not a valid ID. It should start"
" with `lt-....`"
),
)
except is_boto3_error_code("InvalidLaunchTemplateId.NotFoundException") as e: # pylint: disable=duplicate-except
module.fail_json_aws(
e,
msg="Launch template with ID {0} could not be found, please supply a name "
"instead so that a new template can be created".format(module.params.get("launch_template_id")),
msg=(
f"Launch template with ID {module.params.get('launch_template_id')} could not be found, please supply a"
" name instead so that a new template can be created"
),
)
except (ClientError, BotoCoreError, WaiterError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Could not check existing launch templates. This may be an IAM permission problem.")
Expand All @@ -510,9 +511,7 @@ def existing_templates(module):
except (ClientError, BotoCoreError, WaiterError) as e:
module.fail_json_aws(
e,
msg="Could not find launch template versions for {0} (ID: {1}).".format(
template["LaunchTemplateName"], template_id
),
msg=f"Could not find launch template versions for {template['LaunchTemplateName']} (ID: {template_id}).",
)


Expand Down Expand Up @@ -547,28 +546,24 @@ def delete_template(module):
)
if v_resp["UnsuccessfullyDeletedLaunchTemplateVersions"]:
module.warn(
"Failed to delete template versions {0} on launch template {1}".format(
v_resp["UnsuccessfullyDeletedLaunchTemplateVersions"],
template["LaunchTemplateId"],
)
f"Failed to delete template versions {v_resp['UnsuccessfullyDeletedLaunchTemplateVersions']} on"
f" launch template {template['LaunchTemplateId']}"
)
deleted_versions = [
camel_dict_to_snake_dict(v) for v in v_resp["SuccessfullyDeletedLaunchTemplateVersions"]
]
except (ClientError, BotoCoreError) as e:
module.fail_json_aws(
e,
msg="Could not delete existing versions of the launch template {0}".format(
template["LaunchTemplateId"]
),
msg=f"Could not delete existing versions of the launch template {template['LaunchTemplateId']}",
)
try:
resp = ec2.delete_launch_template(
LaunchTemplateId=template["LaunchTemplateId"],
aws_retry=True,
)
except (ClientError, BotoCoreError) as e:
module.fail_json_aws(e, msg="Could not delete launch template {0}".format(template["LaunchTemplateId"]))
module.fail_json_aws(e, msg=f"Could not delete launch template {template['LaunchTemplateId']}")
return {
"deleted_versions": deleted_versions,
"deleted_template": camel_dict_to_snake_dict(resp["LaunchTemplate"]),
Expand Down Expand Up @@ -647,9 +642,7 @@ def create_or_update(module, template_options):
int(module.params.get("source_version"))
except ValueError:
module.fail_json(
msg='source_version param was not a valid integer, got "{0}"'.format(
module.params.get("source_version")
)
msg=f"source_version param was not a valid integer, got \"{module.params.get('source_version')}\""
)
# get source template version
source_version = next(
Expand All @@ -658,7 +651,7 @@ def create_or_update(module, template_options):
)
if source_version is None:
module.fail_json(
msg='source_version does not exist, got "{0}"'.format(module.params.get("source_version"))
msg=f"source_version does not exist, got \"{module.params.get('source_version')}\""
)
resp = ec2.create_launch_template_version(
LaunchTemplateId=template["LaunchTemplateId"],
Expand All @@ -684,9 +677,7 @@ def create_or_update(module, template_options):
int(module.params.get("default_version"))
except ValueError:
module.fail_json(
msg='default_version param was not a valid integer, got "{0}"'.format(
module.params.get("default_version")
)
msg=f"default_version param was not a valid integer, got \"{module.params.get('default_version')}\""
)
set_default = ec2.modify_launch_template(
LaunchTemplateId=template["LaunchTemplateId"],
Expand Down Expand Up @@ -863,7 +854,7 @@ def main():
elif module.params.get("state") == "absent":
out = delete_template(module)
else:
module.fail_json(msg='Unsupported value "{0}" for `state` parameter'.format(module.params.get("state")))
module.fail_json(msg=f"Unsupported value \"{module.params.get('state')}\" for `state` parameter")

module.exit_json(**out)

Expand Down

0 comments on commit a14f58c

Please sign in to comment.