Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk migration to Python 3.6 f-strings #1810

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/fstring-1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- bulk migration of ``%`` and ``.format()`` to fstrings (https://github.com/ansible-collections/community.aws/pull/1810).
10 changes: 4 additions & 6 deletions plugins/module_utils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _inject_ratelimit_retries(self, model):
def get_waiter(self, waiter_name):
waiters = self._model.waiter_names
if waiter_name not in waiters:
self.module.fail_json("Unable to find waiter {0}. Available_waiters: {1}".format(waiter_name, waiters))
self.module.fail_json(f"Unable to find waiter {waiter_name}. Available_waiters: {waiters}")
return botocore.waiter.create_waiter_with_client(
waiter_name,
self._model,
Expand Down Expand Up @@ -183,11 +183,9 @@ def handler(_self, *args, **kwargs):
try:
return func(_self, *args, **kwargs)
except botocore.exceptions.WaiterError as e:
_self.module.fail_json_aws(
e, msg="Failed waiting for {DESC}".format(DESC=description), **extra_ouput
)
_self.module.fail_json_aws(e, msg=f"Failed waiting for {description}", **extra_ouput)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
_self.module.fail_json_aws(e, msg="Failed to {DESC}".format(DESC=description), **extra_ouput)
_self.module.fail_json_aws(e, msg=f"Failed to {description}", **extra_ouput)

return handler

Expand Down Expand Up @@ -356,7 +354,7 @@ def _set_resource_value(self, key, value, description=None, immutable=False):
if immutable and self.original_resource:
if description is None:
description = key
self.module.fail_json(msg="{0} can not be updated after creation".format(description))
self.module.fail_json(msg=f"{description} can not be updated after creation")
self._resource_updates[key] = value
self.changed = True
return True
Expand Down
4 changes: 2 additions & 2 deletions plugins/module_utils/etag.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ def calculate_multipart_etag(source_path, chunk_size=DEFAULT_CHUNK_SIZE):
md5s.append(md5)

if len(md5s) == 1:
new_etag = '"{0}"'.format(md5s[0].hexdigest())
new_etag = f'"{md5s[0].hexdigest()}"'
else: # > 1
digests = b"".join(m.digest() for m in md5s)

new_md5 = hashlib.md5(digests)
new_etag = '"{0}-{1}"'.format(new_md5.hexdigest(), len(md5s))
new_etag = f'"{new_md5.hexdigest()}-{len(md5s)}"'

return new_etag
15 changes: 7 additions & 8 deletions plugins/module_utils/networkfirewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def _set_metadata_value(self, key, value, description=None, immutable=False):
if immutable and self.original_resource:
if description is None:
description = key
self.module.fail_json(msg="{0} can not be updated after creation".format(description))
self.module.fail_json(msg=f"{description} can not be updated after creation")
self._metadata_updates[key] = value
self.changed = True
return True
Expand Down Expand Up @@ -785,7 +785,7 @@ def _set_rule_option(self, option_name, description, value, immutable=False, def
if value == rule_options.get(option_name, default_value):
return False
if immutable and self.original_resource:
self.module.fail_json(msg="{0} can not be updated after creation".format(description))
self.module.fail_json(msg=f"{description} can not be updated after creation")

rule_options[option_name] = value

Expand Down Expand Up @@ -834,7 +834,7 @@ def _set_rule_source(self, rule_type, rules):
conflicting_rule_type = conflicting_types.intersection(current_keys)
if conflicting_rule_type:
self.module.fail_json(
"Unable to add {0} rules, {1} rules already set".format(rule_type, " and ".join(conflicting_rule_type))
f"Unable to add {rule_type} rules, {' and '.join(conflicting_rule_type)} rules already set"
)

original_rules = rules_source.get(rule_type, None)
Expand Down Expand Up @@ -892,7 +892,7 @@ def set_domain_list(self, options):

def _format_rule_options(self, options, sid):
formatted_options = []
opt = dict(Keyword="sid:{0}".format(sid))
opt = dict(Keyword=f"sid:{sid}")
formatted_options.append(opt)
if options:
for option in sorted(options.keys()):
Expand Down Expand Up @@ -954,8 +954,7 @@ def _flush_create(self):
rule_type = self.RULE_TYPES.intersection(set(rules_source.keys()))
if len(rule_type) != 1:
self.module.fail_json(
"Exactly one of rule strings, domain list or rule list"
" must be provided when creating a new rule group",
"Exactly one of rule strings, domain list or rule list must be provided when creating a new rule group",
rule_type=rule_type,
keys=self._resource_updates.keys(),
types=self.RULE_TYPES,
Expand Down Expand Up @@ -1168,7 +1167,7 @@ def _set_engine_option(self, option_name, description, value, immutable=False, d
if value == engine_options.get(option_name, default_value):
return False
if immutable and self.original_resource:
self.module.fail_json(msg="{0} can not be updated after creation".format(description))
self.module.fail_json(msg=f"{description} can not be updated after creation")

engine_options[option_name] = value
return self._set_resource_value("StatefulEngineOptions", engine_options)
Expand Down Expand Up @@ -1207,7 +1206,7 @@ def set_default_actions(self, key, actions, valid_actions=None):
invalid_actions = list(set(actions) - set(valid_actions or []))
if valid_actions and invalid_actions:
self.module.fail_json(
msg="{0} contains invalid actions".format(key),
msg=f"{key} contains invalid actions",
valid_actions=valid_actions,
invalid_actions=invalid_actions,
actions=actions,
Expand Down
12 changes: 6 additions & 6 deletions plugins/module_utils/opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_domain_status(client, module, domain_name):
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Couldn't get domain {0}".format(domain_name))
module.fail_json_aws(e, msg=f"Couldn't get domain {domain_name}")
return response["DomainStatus"]


Expand All @@ -56,7 +56,7 @@ def get_domain_config(client, module, domain_name):
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Couldn't get domain {0}".format(domain_name))
module.fail_json_aws(e, msg=f"Couldn't get domain {domain_name}")
domain_config = {}
arn = None
if response is not None:
Expand Down Expand Up @@ -96,7 +96,7 @@ def normalize_opensearch(client, module, domain):
try:
domain["Tags"] = boto3_tag_list_to_ansible_dict(client.list_tags(ARN=domain["ARN"], aws_retry=True)["TagList"])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, "Couldn't get tags for domain %s" % domain["domain_name"])
module.fail_json_aws(e, f"Couldn't get tags for domain {domain['domain_name']}")
except KeyError:
module.fail_json(msg=str(domain))

Expand Down Expand Up @@ -203,7 +203,7 @@ def get_target_increment_version(client, module, domain_name, target_version):
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(
e,
msg="Couldn't get compatible versions for domain {0}".format(domain_name),
msg=f"Couldn't get compatible versions for domain {domain_name}",
)
compat = api_compatible_versions.get("CompatibleVersions")
if compat is None:
Expand Down Expand Up @@ -246,7 +246,7 @@ def ensure_tags(client, module, resource_arn, existing_tags, tags, purge_tags):
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
) as e:
module.fail_json_aws(e, "Couldn't add tags to domain {0}".format(resource_arn))
module.fail_json_aws(e, f"Couldn't add tags to domain {resource_arn}")
if tags_to_remove:
if module.check_mode:
module.exit_json(changed=True, msg="Would have removed tags if not in check mode")
Expand All @@ -256,5 +256,5 @@ def ensure_tags(client, module, resource_arn, existing_tags, tags, purge_tags):
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
) as e:
module.fail_json_aws(e, "Couldn't remove tags from domain {0}".format(resource_arn))
module.fail_json_aws(e, f"Couldn't remove tags from domain {resource_arn}")
return changed
6 changes: 3 additions & 3 deletions plugins/module_utils/sns.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def list_topic_subscriptions(client, module, topic_arn):
# potentially AuthorizationError when listing subscriptions for third party topic
return [sub for sub in _list_subscriptions_with_backoff(client) if sub["TopicArn"] == topic_arn]
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Couldn't get subscriptions list for topic %s" % topic_arn)
module.fail_json_aws(e, msg=f"Couldn't get subscriptions list for topic {topic_arn}")
except (
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Couldn't get subscriptions list for topic %s" % topic_arn)
module.fail_json_aws(e, msg=f"Couldn't get subscriptions list for topic {topic_arn}")


def list_topics(client, module):
Expand All @@ -65,7 +65,7 @@ def list_topics(client, module):
def topic_arn_lookup(client, module, name):
# topic names cannot have colons, so this captures the full topic name
all_topics = list_topics(client, module)
lookup_topic = ":%s" % name
lookup_topic = f":{name}"
for topic in all_topics:
if topic.endswith(lookup_topic):
return topic
Expand Down
12 changes: 6 additions & 6 deletions plugins/modules/acm_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def ensure_tags(client, module, resource_arn, existing_tags, tags, purge_tags):
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
) as e:
module.fail_json_aws(e, "Couldn't add tags to certificate {0}".format(resource_arn))
module.fail_json_aws(e, f"Couldn't add tags to certificate {resource_arn}")
if tags_to_remove and not module.check_mode:
# remove_tags_from_certificate wants a list of key, value pairs, not a list of keys.
tags_list = [{"Key": key, "Value": existing_tags.get(key)} for key in tags_to_remove]
Expand All @@ -289,7 +289,7 @@ def ensure_tags(client, module, resource_arn, existing_tags, tags, purge_tags):
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
) as e:
module.fail_json_aws(e, "Couldn't remove tags from certificate {0}".format(resource_arn))
module.fail_json_aws(e, f"Couldn't remove tags from certificate {resource_arn}")
new_tags = deepcopy(existing_tags)
for key, value in tags_to_add.items():
new_tags[key] = value
Expand Down Expand Up @@ -441,7 +441,7 @@ def ensure_certificates_present(client, module, acm, certificates, desired_tags,
cert_arn = None
changed = False
if len(certificates) > 1:
msg = "More than one certificate with Name=%s exists in ACM in this region" % module.params["name_tag"]
msg = f"More than one certificate with Name={module.params['name_tag']} exists in ACM in this region"
module.fail_json(msg=msg, certificates=certificates)
elif len(certificates) == 1:
# Update existing certificate that was previously imported to ACM.
Expand Down Expand Up @@ -496,7 +496,7 @@ def main():
absent_args = ["certificate_arn", "domain_name", "name_tag"]
if sum([(module.params[a] is not None) for a in absent_args]) < 1:
for a in absent_args:
module.debug("%s is %s" % (a, module.params[a]))
module.debug(f"{a} is {module.params[a]}")
module.fail_json(
msg="If 'state' is specified as 'present' then at least one of 'name_tag', 'certificate_arn' or 'domain_name' must be specified"
)
Expand All @@ -505,7 +505,7 @@ def main():
absent_args = ["certificate_arn", "domain_name", "name_tag"]
if sum([(module.params[a] is not None) for a in absent_args]) != 1:
for a in absent_args:
module.debug("%s is %s" % (a, module.params[a]))
module.debug(f"{a} is {module.params[a]}")
module.fail_json(
msg="If 'state' is specified as 'absent' then exactly one of 'name_tag', 'certificate_arn' or 'domain_name' must be specified"
)
Expand Down Expand Up @@ -543,7 +543,7 @@ def main():
only_tags=filter_tags,
)

module.debug("Found %d corresponding certificates in ACM" % len(certificates))
module.debug(f"Found {len(certificates)} corresponding certificates in ACM")
if module.params["state"] == "present":
ensure_certificates_present(client, module, acm, certificates, desired_tags, filter_tags)

Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/acm_certificate_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def main():
)

if module.params["certificate_arn"] and len(certificates) != 1:
module.fail_json(msg="No certificate exists in this region with ARN %s" % module.params["certificate_arn"])
module.fail_json(msg=f"No certificate exists in this region with ARN {module.params['certificate_arn']}")

module.exit_json(certificates=certificates)

Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def get_api_definitions(module, swagger_file=None, swagger_dict=None, swagger_te
with open(swagger_file) as f:
apidata = f.read()
except OSError as e:
msg = "Failed trying to read swagger file {0}: {1}".format(str(swagger_file), str(e))
msg = f"Failed trying to read swagger file {str(swagger_file)}: {str(e)}"
module.fail_json(msg=msg, exception=traceback.format_exc())
if swagger_dict is not None:
apidata = json.dumps(swagger_dict)
Expand Down Expand Up @@ -281,7 +281,7 @@ def delete_rest_api(module, client, api_id):
try:
delete_response = delete_api(client, api_id)
except (botocore.exceptions.ClientError, botocore.exceptions.EndpointConnectionError) as e:
module.fail_json_aws(e, msg="deleting API {0}".format(api_id))
module.fail_json_aws(e, msg=f"deleting API {api_id}")
return delete_response


Expand All @@ -299,7 +299,7 @@ def ensure_api_in_correct_state(module, client, api_id, api_data):
try:
configure_response = configure_api(client, api_id, api_data=api_data)
except (botocore.exceptions.ClientError, botocore.exceptions.EndpointConnectionError) as e:
module.fail_json_aws(e, msg="configuring API {0}".format(api_id))
module.fail_json_aws(e, msg=f"configuring API {api_id}")

deploy_response = None

Expand All @@ -308,7 +308,7 @@ def ensure_api_in_correct_state(module, client, api_id, api_data):
try:
deploy_response = create_deployment(client, api_id, **module.params)
except (botocore.exceptions.ClientError, botocore.exceptions.EndpointConnectionError) as e:
msg = "deploying api {0} to stage {1}".format(api_id, stage)
msg = f"deploying api {api_id} to stage {stage}"
module.fail_json_aws(e, msg)

return configure_response, deploy_response
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/autoscaling_instance_refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def start_or_cancel_instance_refresh(conn, module):
result = dict(instance_refreshes=camel_dict_to_snake_dict(instance_refreshes["InstanceRefreshes"][0]))
return module.exit_json(**result)
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Failed to {0} InstanceRefresh".format(asg_state.replace("ed", "")))
module.fail_json_aws(e, msg=f"Failed to {asg_state.replace('ed', '')} InstanceRefresh")


def main():
Expand Down
13 changes: 6 additions & 7 deletions plugins/modules/autoscaling_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,15 +447,15 @@ def create_scaling_policy(connection, module):
# it's only required if policy is SimpleScaling and state is present
if not module.params["scaling_adjustment"]:
module.fail_json(
msg="scaling_adjustment is required when policy_type is SimpleScaling " "and state is present"
msg="scaling_adjustment is required when policy_type is SimpleScaling and state is present"
)
params["ScalingAdjustment"] = module.params["scaling_adjustment"]
if module.params["cooldown"]:
params["Cooldown"] = module.params["cooldown"]

elif policy_type == "StepScaling":
if not module.params["step_adjustments"]:
module.fail_json(msg="step_adjustments is required when policy_type is StepScaling" "and state is present")
module.fail_json(msg="step_adjustments is required when policy_type is StepScaling and state is present")
params["StepAdjustments"] = []
for step_adjustment in module.params["step_adjustments"]:
step_adjust_params = dict(ScalingAdjustment=step_adjustment["scaling_adjustment"])
Expand All @@ -472,8 +472,7 @@ def create_scaling_policy(connection, module):
elif policy_type == "TargetTrackingScaling":
if not module.params["target_tracking_config"]:
module.fail_json(
msg="target_tracking_config is required when policy_type is "
"TargetTrackingScaling and state is present"
msg="target_tracking_config is required when policy_type is TargetTrackingScaling and state is present"
)
else:
params["TargetTrackingConfiguration"] = build_target_specification(
Expand All @@ -488,7 +487,7 @@ def create_scaling_policy(connection, module):
aws_retry=True, AutoScalingGroupName=asg_name, PolicyNames=[policy_name]
)["ScalingPolicies"]
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to obtain autoscaling policy %s" % policy_name)
module.fail_json_aws(e, msg=f"Failed to obtain autoscaling policy {policy_name}")

before = after = {}
if not policies:
Expand All @@ -512,7 +511,7 @@ def create_scaling_policy(connection, module):
aws_retry=True, AutoScalingGroupName=asg_name, PolicyNames=[policy_name]
)["ScalingPolicies"]
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to obtain autoscaling policy %s" % policy_name)
module.fail_json_aws(e, msg=f"Failed to obtain autoscaling policy {policy_name}")

policy = camel_dict_to_snake_dict(policies[0])
# Backward compatible return values
Expand All @@ -532,7 +531,7 @@ def delete_scaling_policy(connection, module):
try:
policy = connection.describe_policies(aws_retry=True, PolicyNames=[policy_name])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to obtain autoscaling policy %s" % policy_name)
module.fail_json_aws(e, msg=f"Failed to obtain autoscaling policy {policy_name}")

if policy["ScalingPolicies"]:
try:
Expand Down
7 changes: 2 additions & 5 deletions plugins/modules/batch_compute_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,11 @@ def validate_params(module):
# validate compute environment name
if not re.search(r"^[\w\_:]+$", compute_environment_name):
module.fail_json(
msg="Function compute_environment_name {0} is invalid. Names must contain only alphanumeric characters "
"and underscores.".format(compute_environment_name)
msg=f"Function compute_environment_name {compute_environment_name} is invalid. Names must contain only alphanumeric characters and underscores."
)
if not compute_environment_name.startswith("arn:aws:batch:"):
if len(compute_environment_name) > 128:
module.fail_json(
msg='compute_environment_name "{0}" exceeds 128 character limit'.format(compute_environment_name)
)
module.fail_json(msg=f'compute_environment_name "{compute_environment_name}" exceeds 128 character limit')

return

Expand Down
Loading