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 (2) #1513

Merged
merged 1 commit into from
May 4, 2023
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
5 changes: 5 additions & 0 deletions changelogs/fragments/fstring-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 1483 includes a fragment and links to 1513
trivial:
- bulk migration of ``%`` and ``.format()`` to fstrings (https://github.com/ansible-collections/amazon.aws/pull/1513).
minor_changes:
- rds_param_group - drop Python2 import fallbacks (https://github.com/ansible-collections/amazon.aws/pull/1513).
4 changes: 2 additions & 2 deletions plugins/inventory/aws_rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ def describe_wrapper(connection, filters, strict=False):
except is_boto3_error_code("AccessDenied") as e: # pylint: disable=duplicate-except
if not strict:
return []
raise AnsibleError("Failed to query RDS: {0}".format(to_native(e)))
raise AnsibleError(f"Failed to query RDS: {to_native(e)}")
except (
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
) as e: # pylint: disable=duplicate-except
raise AnsibleError("Failed to query RDS: {0}".format(to_native(e)))
raise AnsibleError(f"Failed to query RDS: {to_native(e)}")

return results

Expand Down
50 changes: 21 additions & 29 deletions plugins/module_utils/rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def get_rds_method_attribute(method_name, module):
else:
if module.params.get("wait"):
raise NotImplementedError(
f"method {method_name} hasn't been added to the list of accepted methods to use a waiter in module_utils/rds.py"
f"method {method_name} hasn't been added to the list of accepted methods to use a waiter in module_utils/rds.py",
)

return Boto3ClientMethod(
Expand All @@ -179,7 +179,7 @@ def get_final_identifier(method_name, module):
identifier = module.params["db_cluster_snapshot_identifier"]
else:
raise NotImplementedError(
f"method {method_name} hasn't been added to the list of accepted methods in module_utils/rds.py"
f"method {method_name} hasn't been added to the list of accepted methods in module_utils/rds.py",
)
if not module.check_mode and updated_identifier and apply_immediately:
identifier = updated_identifier
Expand All @@ -188,9 +188,7 @@ def get_final_identifier(method_name, module):

def handle_errors(module, exception, method_name, parameters):
if not isinstance(exception, ClientError):
module.fail_json_aws(
exception, msg="Unexpected failure for method {0} with parameters {1}".format(method_name, parameters)
)
module.fail_json_aws(exception, msg=f"Unexpected failure for method {method_name} with parameters {parameters}")

changed = True
error_code = exception.response["Error"]["Code"]
Expand All @@ -205,38 +203,39 @@ def handle_errors(module, exception, method_name, parameters):
else:
module.fail_json_aws(
exception,
msg="Unable to {0}".format(get_rds_method_attribute(method_name, module).operation_description),
msg=f"Unable to {get_rds_method_attribute(method_name, module).operation_description}",
)
elif method_name == "promote_read_replica" and error_code == "InvalidDBInstanceState":
if "DB Instance is not a read replica" in to_text(exception):
changed = False
else:
module.fail_json_aws(
exception,
msg="Unable to {0}".format(get_rds_method_attribute(method_name, module).operation_description),
msg=f"Unable to {get_rds_method_attribute(method_name, module).operation_description}",
)
elif method_name == "promote_read_replica_db_cluster" and error_code == "InvalidDBClusterStateFault":
if "DB Cluster that is not a read replica" in to_text(exception):
changed = False
else:
module.fail_json_aws(
exception,
msg="Unable to {0}".format(get_rds_method_attribute(method_name, module).operation_description),
msg=f"Unable to {get_rds_method_attribute(method_name, module).operation_description}",
)
elif method_name == "create_db_cluster" and error_code == "InvalidParameterValue":
accepted_engines = ["aurora", "aurora-mysql", "aurora-postgresql", "mysql", "postgres"]
if parameters.get("Engine") not in accepted_engines:
module.fail_json_aws(
exception, msg="DB engine {0} should be one of {1}".format(parameters.get("Engine"), accepted_engines)
exception, msg=f"DB engine {parameters.get('Engine')} should be one of {accepted_engines}"
)
else:
module.fail_json_aws(
exception,
msg="Unable to {0}".format(get_rds_method_attribute(method_name, module).operation_description),
msg=f"Unable to {get_rds_method_attribute(method_name, module).operation_description}",
)
else:
module.fail_json_aws(
exception, msg="Unable to {0}".format(get_rds_method_attribute(method_name, module).operation_description)
exception,
msg=f"Unable to {get_rds_method_attribute(method_name, module).operation_description}",
)

return changed
Expand Down Expand Up @@ -283,15 +282,10 @@ def wait(client, db_instance_id, waiter_name):
if e.last_response.get("Error", {}).get("Code") == "DBInstanceNotFound":
sleep(10)
continue
module.fail_json_aws(
e, msg="Error while waiting for DB instance {0} to be {1}".format(db_instance_id, expected_status)
)
module.fail_json_aws(e, msg=f"Error while waiting for DB instance {db_instance_id} to be {expected_status}")
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(
e,
msg="Unexpected error while waiting for DB instance {0} to be {1}".format(
db_instance_id, expected_status
),
e, msg=f"Unexpected error while waiting for DB instance {db_instance_id} to be {expected_status}"
)


Expand All @@ -300,28 +294,26 @@ def wait_for_cluster_status(client, module, db_cluster_id, waiter_name):
get_waiter(client, waiter_name).wait(DBClusterIdentifier=db_cluster_id)
except WaiterError as e:
if waiter_name == "cluster_deleted":
msg = "Failed to wait for DB cluster {0} to be deleted".format(db_cluster_id)
msg = f"Failed to wait for DB cluster {db_cluster_id} to be deleted"
else:
msg = "Failed to wait for DB cluster {0} to be available".format(db_cluster_id)
msg = f"Failed to wait for DB cluster {db_cluster_id} to be available"
module.fail_json_aws(e, msg=msg)
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(
e, msg="Failed with an unexpected error while waiting for the DB cluster {0}".format(db_cluster_id)
)
module.fail_json_aws(e, msg=f"Failed with an unexpected error while waiting for the DB cluster {db_cluster_id}")


def wait_for_instance_snapshot_status(client, module, db_snapshot_id, waiter_name):
try:
client.get_waiter(waiter_name).wait(DBSnapshotIdentifier=db_snapshot_id)
except WaiterError as e:
if waiter_name == "db_snapshot_deleted":
msg = "Failed to wait for DB snapshot {0} to be deleted".format(db_snapshot_id)
msg = f"Failed to wait for DB snapshot {db_snapshot_id} to be deleted"
else:
msg = "Failed to wait for DB snapshot {0} to be available".format(db_snapshot_id)
msg = f"Failed to wait for DB snapshot {db_snapshot_id} to be available"
module.fail_json_aws(e, msg=msg)
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(
e, msg="Failed with an unexpected error while waiting for the DB snapshot {0}".format(db_snapshot_id)
e, msg=f"Failed with an unexpected error while waiting for the DB snapshot {db_snapshot_id}"
)


Expand All @@ -330,14 +322,14 @@ def wait_for_cluster_snapshot_status(client, module, db_snapshot_id, waiter_name
client.get_waiter(waiter_name).wait(DBClusterSnapshotIdentifier=db_snapshot_id)
except WaiterError as e:
if waiter_name == "db_cluster_snapshot_deleted":
msg = "Failed to wait for DB cluster snapshot {0} to be deleted".format(db_snapshot_id)
msg = f"Failed to wait for DB cluster snapshot {db_snapshot_id} to be deleted"
else:
msg = "Failed to wait for DB cluster snapshot {0} to be available".format(db_snapshot_id)
msg = f"Failed to wait for DB cluster snapshot {db_snapshot_id} to be available"
module.fail_json_aws(e, msg=msg)
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(
e,
msg="Failed with an unexpected error while waiting for the DB cluster snapshot {0}".format(db_snapshot_id),
msg=f"Failed with an unexpected error while waiting for the DB cluster snapshot {db_snapshot_id}",
)


Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/rds_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ def main():
and module.params.get("db_cluster_instance_class")
):
module.fail_json(
f"When engine={module.params['engine']} allocated_storage, iops and db_cluster_instance_class msut be specified"
f"When engine={module.params['engine']} allocated_storage, iops and db_cluster_instance_class must be specified"
)
else:
# Fall to default value
Expand Down
9 changes: 3 additions & 6 deletions plugins/modules/rds_cluster_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def get_snapshot(snapshot_id):
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Couldn't get snapshot {0}".format(snapshot_id))
module.fail_json_aws(e, msg=f"Couldn't get snapshot {snapshot_id}")
return snapshot


Expand All @@ -256,11 +256,8 @@ def get_parameters(parameters, method_name):

required_options = get_boto3_client_method_parameters(client, method_name, required=True)
if any(parameters.get(k) is None for k in required_options):
module.fail_json(
msg="To {0} requires the parameters: {1}".format(
get_rds_method_attribute(method_name, module).operation_description, required_options
)
)
attribute_description = get_rds_method_attribute(method_name, module).operation_description
module.fail_json(msg=f"To {attribute_description} requires the parameters: {required_options}")
options = get_boto3_client_method_parameters(client, method_name)
parameters = dict((k, v) for k, v in parameters.items() if k in options and v is not None)

Expand Down
32 changes: 16 additions & 16 deletions plugins/modules/rds_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,11 +987,8 @@ def get_parameters(client, module, parameters, method_name):

required_options = get_boto3_client_method_parameters(client, method_name, required=True)
if any(parameters.get(k) is None for k in required_options):
module.fail_json(
msg="To {0} requires the parameters: {1}".format(
get_rds_method_attribute(method_name, module).operation_description, required_options
)
)
description = get_rds_method_attribute(method_name, module).operation_description
module.fail_json(msg=f"To {description} requires the parameters: {required_options}")
options = get_boto3_client_method_parameters(client, method_name)
parameters = dict((k, v) for k, v in parameters.items() if k in options and v is not None)

Expand Down Expand Up @@ -1086,7 +1083,10 @@ def get_options_with_changing_values(client, module, parameters):

if new_storage_throughput < 500 and GP3_THROUGHPUT:
module.fail_json(
msg="Storage Throughput must be at least 500 when the allocated storage is larger than or equal to 400 GB."
msg=(
"Storage Throughput must be at least 500 when the allocated storage is larger than or equal"
" to 400 GB."
)
)

if current_iops != new_iops:
Expand Down Expand Up @@ -1245,10 +1245,10 @@ def validate_options(client, module, instance):
modified_instance = {}

if modified_id and instance and modified_instance:
module.fail_json(msg="A new instance ID {0} was provided but it already exists".format(modified_id))
module.fail_json(msg=f"A new instance ID {modified_id} was provided but it already exists")
if modified_id and not instance and modified_instance:
module.fail_json(
msg="A new instance ID {0} was provided but the instance to be renamed does not exist".format(modified_id)
msg=f"A new instance ID {modified_id} was provided but the instance to be renamed does not exist"
)
if state in ("absent", "terminated") and instance and not skip_final_snapshot and snapshot_id is None:
module.fail_json(
Expand All @@ -1257,12 +1257,13 @@ def validate_options(client, module, instance):
if engine is not None and not (engine.startswith("mysql") or engine.startswith("oracle")) and tde_options:
module.fail_json(msg="TDE is available for MySQL and Oracle DB instances")
if read_replica is True and not instance and creation_source not in [None, "instance"]:
module.fail_json(
msg="Cannot create a read replica from {0}. You must use a source DB instance".format(creation_source)
)
module.fail_json(msg=f"Cannot create a read replica from {creation_source}. You must use a source DB instance")
if read_replica is True and not instance and not source_instance:
module.fail_json(
msg="read_replica is true and the instance does not exist yet but all of the following are missing: source_db_instance_identifier"
msg=(
"read_replica is true and the instance does not exist yet but all of the following are missing:"
" source_db_instance_identifier"
)
)


Expand Down Expand Up @@ -1321,9 +1322,7 @@ def ensure_iam_roles(client, module, instance_id):
engine = instance.get("engine")
if engine not in valid_engines_iam_roles:
module.fail_json(
msg="DB engine {0} is not valid for adding IAM roles. Valid engines are {1}".format(
engine, valid_engines_iam_roles
)
msg=f"DB engine {engine} is not valid for adding IAM roles. Valid engines are {valid_engines_iam_roles}"
)

changed = False
Expand Down Expand Up @@ -1509,7 +1508,8 @@ def main():
# see: amazon.aws.module_util.rds.handle_errors.
if module.params.get("allow_major_version_upgrade") and module.check_mode:
module.warn(
"allow_major_version_upgrade is not returned when describing db instances, so changed will always be `True` on check mode runs."
"allow_major_version_upgrade is not returned when describing db instances, so changed will always be `True`"
" on check mode runs."
)

client = module.client("rds")
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/rds_instance_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def get_instance_tags(conn, arn):
try:
return boto3_tag_list_to_ansible_dict(conn.list_tags_for_resource(ResourceName=arn, aws_retry=True)["TagList"])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
raise RdsInstanceInfoFailure(e, "Couldn't get tags for instance %s" % arn)
raise RdsInstanceInfoFailure(e, f"Couldn't get tags for instance {arn}")


def instance_info(conn, instance_name, filters):
Expand Down
9 changes: 3 additions & 6 deletions plugins/modules/rds_instance_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def get_snapshot(snapshot_id):
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Couldn't get snapshot {0}".format(snapshot_id))
module.fail_json_aws(e, msg=f"Couldn't get snapshot {snapshot_id}")
return snapshot


Expand All @@ -267,11 +267,8 @@ def get_parameters(parameters, method_name):

required_options = get_boto3_client_method_parameters(client, method_name, required=True)
if any(parameters.get(k) is None for k in required_options):
module.fail_json(
msg="To {0} requires the parameters: {1}".format(
get_rds_method_attribute(method_name, module).operation_description, required_options
)
)
method_description = get_rds_method_attribute(method_name, module).operation_description
module.fail_json(msg=f"To {method_description} requires the parameters: {*required_options,}")
options = get_boto3_client_method_parameters(client, method_name)
parameters = dict((k, v) for k, v in parameters.items() if k in options and v is not None)

Expand Down
11 changes: 4 additions & 7 deletions plugins/modules/rds_param_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
returned: when state is present
"""

from itertools import zip_longest

try:
import botocore
except ImportError:
Expand Down Expand Up @@ -182,8 +184,7 @@ def update_parameters(module, connection):
for param_key, param_value in desired.items():
if param_key not in lookup:
errors.append(
"Parameter %s is not an available parameter for the %s engine"
% (param_key, module.params.get("engine"))
f"Parameter {param_key} is not an available parameter for the {module.params.get('engine')} engine"
)
else:
converted_value = convert_parameter(lookup[param_key], param_value)
Expand All @@ -194,14 +195,10 @@ def update_parameters(module, connection):
dict(ParameterValue=converted_value, ParameterName=param_key, ApplyMethod=apply_method)
)
else:
errors.append("Parameter %s is not modifiable" % param_key)
errors.append(f"Parameter {param_key} is not modifiable")

# modify_db_parameters takes at most 20 parameters
if modify_list and not module.check_mode:
try:
from itertools import izip_longest as zip_longest # python 2
except ImportError:
from itertools import zip_longest # python 3
for modify_slice in zip_longest(*[iter(modify_list)] * 20, fillvalue=None):
non_empty_slice = [item for item in modify_slice if item]
try:
Expand Down
9 changes: 5 additions & 4 deletions plugins/modules/rds_snapshot_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@
def common_snapshot_info(module, conn, method, prefix, params):
paginator = conn.get_paginator(method)
try:
results = paginator.paginate(**params).build_full_result()["%ss" % prefix]
except is_boto3_error_code("%sNotFound" % prefix):
results = paginator.paginate(**params).build_full_result()[f"{prefix}s"]
except is_boto3_error_code(f"{prefix}NotFound"):
results = []
except (
botocore.exceptions.ClientError,
Expand All @@ -316,10 +316,11 @@ def common_snapshot_info(module, conn, method, prefix, params):
try:
if snapshot["SnapshotType"] != "shared":
snapshot["Tags"] = boto3_tag_list_to_ansible_dict(
conn.list_tags_for_resource(ResourceName=snapshot["%sArn" % prefix], aws_retry=True)["TagList"]
conn.list_tags_for_resource(ResourceName=snapshot[f"{prefix}Arn"], aws_retry=True)["TagList"]
)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, "Couldn't get tags for snapshot %s" % snapshot["%sIdentifier" % prefix])
snapshot_name = snapshot[f"{prefix}Identifier"]
module.fail_json_aws(e, f"Couldn't get tags for snapshot {snapshot_name}")

return [camel_dict_to_snake_dict(snapshot, ignore_list=["Tags"]) for snapshot in results]

Expand Down