Techdebt: MyPy RDS (#6203)

This commit is contained in:
Bert Blommers 2023-04-12 19:27:40 +00:00 committed by GitHub
parent c44f0b7395
commit 7ecc0161af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 537 additions and 417 deletions

View File

@ -3,7 +3,7 @@ from moto.core.exceptions import RESTError
class RDSClientError(RESTError):
def __init__(self, code, message):
def __init__(self, code: str, message: str):
super().__init__(error_type=code, message=message)
template = Template(
"""
@ -20,21 +20,21 @@ class RDSClientError(RESTError):
class DBInstanceNotFoundError(RDSClientError):
def __init__(self, database_identifier):
def __init__(self, database_identifier: str):
super().__init__(
"DBInstanceNotFound", f"DBInstance {database_identifier} not found."
)
class DBSnapshotNotFoundError(RDSClientError):
def __init__(self, snapshot_identifier):
def __init__(self, snapshot_identifier: str):
super().__init__(
"DBSnapshotNotFound", f"DBSnapshot {snapshot_identifier} not found."
)
class DBSecurityGroupNotFoundError(RDSClientError):
def __init__(self, security_group_name):
def __init__(self, security_group_name: str):
super().__init__(
"DBSecurityGroupNotFound",
f"Security Group {security_group_name} not found.",
@ -44,14 +44,14 @@ class DBSecurityGroupNotFoundError(RDSClientError):
class DBSubnetGroupNotFoundError(RDSClientError):
code = 404
def __init__(self, subnet_group_name):
def __init__(self, subnet_group_name: str):
super().__init__(
"DBSubnetGroupNotFoundFault", f"Subnet Group {subnet_group_name} not found."
)
class DBParameterGroupNotFoundError(RDSClientError):
def __init__(self, db_parameter_group_name):
def __init__(self, db_parameter_group_name: str):
super().__init__(
"DBParameterGroupNotFound",
f"DB Parameter Group {db_parameter_group_name} not found.",
@ -59,7 +59,7 @@ class DBParameterGroupNotFoundError(RDSClientError):
class DBClusterParameterGroupNotFoundError(RDSClientError):
def __init__(self, group_name):
def __init__(self, group_name: str):
super().__init__(
"DBParameterGroupNotFound",
f"DBClusterParameterGroup not found: {group_name}",
@ -67,7 +67,7 @@ class DBClusterParameterGroupNotFoundError(RDSClientError):
class OptionGroupNotFoundFaultError(RDSClientError):
def __init__(self, option_group_name):
def __init__(self, option_group_name: str):
super().__init__(
"OptionGroupNotFoundFault",
f"Specified OptionGroupName: {option_group_name} not found.",
@ -75,7 +75,7 @@ class OptionGroupNotFoundFaultError(RDSClientError):
class InvalidDBClusterStateFaultError(RDSClientError):
def __init__(self, database_identifier):
def __init__(self, database_identifier: str):
super().__init__(
"InvalidDBClusterStateFault",
f"Invalid DB type, when trying to perform StopDBInstance on {database_identifier}e. See AWS RDS documentation on rds.stop_db_instance",
@ -83,7 +83,7 @@ class InvalidDBClusterStateFaultError(RDSClientError):
class InvalidDBInstanceStateError(RDSClientError):
def __init__(self, database_identifier, istate):
def __init__(self, database_identifier: str, istate: str):
estate = (
"in available state"
if istate == "stop"
@ -95,7 +95,7 @@ class InvalidDBInstanceStateError(RDSClientError):
class SnapshotQuotaExceededError(RDSClientError):
def __init__(self):
def __init__(self) -> None:
super().__init__(
"SnapshotQuotaExceeded",
"The request cannot be processed because it would exceed the maximum number of snapshots.",
@ -103,7 +103,7 @@ class SnapshotQuotaExceededError(RDSClientError):
class DBSnapshotAlreadyExistsError(RDSClientError):
def __init__(self, database_snapshot_identifier):
def __init__(self, database_snapshot_identifier: str):
super().__init__(
"DBSnapshotAlreadyExists",
f"Cannot create the snapshot because a snapshot with the identifier {database_snapshot_identifier} already exists.",
@ -111,29 +111,29 @@ class DBSnapshotAlreadyExistsError(RDSClientError):
class InvalidParameterValue(RDSClientError):
def __init__(self, message):
def __init__(self, message: str):
super().__init__("InvalidParameterValue", message)
class InvalidParameterCombination(RDSClientError):
def __init__(self, message):
def __init__(self, message: str):
super().__init__("InvalidParameterCombination", message)
class InvalidDBClusterStateFault(RDSClientError):
def __init__(self, message):
def __init__(self, message: str):
super().__init__("InvalidDBClusterStateFault", message)
class DBClusterNotFoundError(RDSClientError):
def __init__(self, cluster_identifier):
def __init__(self, cluster_identifier: str):
super().__init__(
"DBClusterNotFoundFault", f"DBCluster {cluster_identifier} not found."
)
class DBClusterSnapshotNotFoundError(RDSClientError):
def __init__(self, snapshot_identifier):
def __init__(self, snapshot_identifier: str):
super().__init__(
"DBClusterSnapshotNotFoundFault",
f"DBClusterSnapshot {snapshot_identifier} not found.",
@ -141,7 +141,7 @@ class DBClusterSnapshotNotFoundError(RDSClientError):
class DBClusterSnapshotAlreadyExistsError(RDSClientError):
def __init__(self, database_snapshot_identifier):
def __init__(self, database_snapshot_identifier: str):
super().__init__(
"DBClusterSnapshotAlreadyExistsFault",
f"Cannot create the snapshot because a snapshot with the identifier {database_snapshot_identifier} already exists.",
@ -149,7 +149,7 @@ class DBClusterSnapshotAlreadyExistsError(RDSClientError):
class ExportTaskAlreadyExistsError(RDSClientError):
def __init__(self, export_task_identifier):
def __init__(self, export_task_identifier: str):
super().__init__(
"ExportTaskAlreadyExistsFault",
f"Cannot start export task because a task with the identifier {export_task_identifier} already exists.",
@ -157,7 +157,7 @@ class ExportTaskAlreadyExistsError(RDSClientError):
class ExportTaskNotFoundError(RDSClientError):
def __init__(self, export_task_identifier):
def __init__(self, export_task_identifier: str):
super().__init__(
"ExportTaskNotFoundFault",
f"Cannot cancel export task because a task with the identifier {export_task_identifier} is not exist.",
@ -165,7 +165,7 @@ class ExportTaskNotFoundError(RDSClientError):
class InvalidExportSourceStateError(RDSClientError):
def __init__(self, status):
def __init__(self, status: str):
super().__init__(
"InvalidExportSourceStateFault",
f"Export source should be 'available' but current status is {status}.",
@ -173,7 +173,7 @@ class InvalidExportSourceStateError(RDSClientError):
class SubscriptionAlreadyExistError(RDSClientError):
def __init__(self, subscription_name):
def __init__(self, subscription_name: str):
super().__init__(
"SubscriptionAlreadyExistFault",
f"Subscription {subscription_name} already exists.",
@ -181,7 +181,7 @@ class SubscriptionAlreadyExistError(RDSClientError):
class SubscriptionNotFoundError(RDSClientError):
def __init__(self, subscription_name):
def __init__(self, subscription_name: str):
super().__init__(
"SubscriptionNotFoundFault", f"Subscription {subscription_name} not found."
)

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
from collections import defaultdict
from typing import Any
from typing import Any, Dict, List, Iterable
from moto.core.common_types import TYPE_RESPONSE
from moto.core.responses import BaseResponse
@ -16,7 +16,7 @@ from .exceptions import DBParameterGroupNotFoundError
class RDSResponse(BaseResponse):
def __init__(self):
def __init__(self) -> None:
super().__init__(service_name="rds")
# Neptune and RDS share a HTTP endpoint RDS is the lucky guy that catches all requests
# So we have to determine whether we can handle an incoming request here, or whether it needs redirecting to Neptune
@ -31,13 +31,13 @@ class RDSResponse(BaseResponse):
self.neptune.setup_class(request, full_url, headers)
return super()._dispatch(request, full_url, headers)
def __getattribute__(self, name: str):
def __getattribute__(self, name: str) -> Any:
if name in ["create_db_cluster", "create_global_cluster"]:
if self._get_param("Engine") == "neptune":
return object.__getattribute__(self.neptune, name)
return object.__getattribute__(self, name)
def _get_db_kwargs(self):
def _get_db_kwargs(self) -> Dict[str, Any]:
args = {
"auto_minor_version_upgrade": self._get_param("AutoMinorVersionUpgrade"),
"allocated_storage": self._get_int_param("AllocatedStorage"),
@ -90,7 +90,7 @@ class RDSResponse(BaseResponse):
args["tags"] = self.unpack_list_params("Tags", "Tag")
return args
def _get_modify_db_cluster_kwargs(self):
def _get_modify_db_cluster_kwargs(self) -> Dict[str, Any]:
args = {
"auto_minor_version_upgrade": self._get_param("AutoMinorVersionUpgrade"),
"allocated_storage": self._get_int_param("AllocatedStorage"),
@ -146,7 +146,7 @@ class RDSResponse(BaseResponse):
args["tags"] = self.unpack_list_params("Tags", "Tag")
return args
def _get_db_replica_kwargs(self):
def _get_db_replica_kwargs(self) -> Dict[str, Any]:
return {
"auto_minor_version_upgrade": self._get_param("AutoMinorVersionUpgrade"),
"availability_zone": self._get_param("AvailabilityZone"),
@ -161,7 +161,7 @@ class RDSResponse(BaseResponse):
"storage_type": self._get_param("StorageType"),
}
def _get_option_group_kwargs(self):
def _get_option_group_kwargs(self) -> Dict[str, Any]:
return {
"major_engine_version": self._get_param("MajorEngineVersion"),
"description": self._get_param("OptionGroupDescription"),
@ -169,7 +169,7 @@ class RDSResponse(BaseResponse):
"name": self._get_param("OptionGroupName"),
}
def _get_db_parameter_group_kwargs(self):
def _get_db_parameter_group_kwargs(self) -> Dict[str, Any]:
return {
"description": self._get_param("Description"),
"family": self._get_param("DBParameterGroupFamily"),
@ -177,7 +177,7 @@ class RDSResponse(BaseResponse):
"tags": self.unpack_list_params("Tags", "Tag"),
}
def _get_db_cluster_kwargs(self):
def _get_db_cluster_kwargs(self) -> Dict[str, Any]:
return {
"availability_zones": self._get_multi_param(
"AvailabilityZones.AvailabilityZone"
@ -213,7 +213,7 @@ class RDSResponse(BaseResponse):
),
}
def _get_export_task_kwargs(self):
def _get_export_task_kwargs(self) -> Dict[str, Any]:
return {
"export_task_identifier": self._get_param("ExportTaskIdentifier"),
"source_arn": self._get_param("SourceArn"),
@ -224,7 +224,7 @@ class RDSResponse(BaseResponse):
"export_only": self.unpack_list_params("ExportOnly", "member"),
}
def _get_event_subscription_kwargs(self):
def _get_event_subscription_kwargs(self) -> Dict[str, Any]:
return {
"subscription_name": self._get_param("SubscriptionName"),
"sns_topic_arn": self._get_param("SnsTopicArn"),
@ -237,29 +237,31 @@ class RDSResponse(BaseResponse):
"tags": self.unpack_list_params("Tags", "Tag"),
}
def unpack_list_params(self, label, child_label):
def unpack_list_params(self, label: str, child_label: str) -> List[Dict[str, Any]]:
root = self._get_multi_param_dict(label) or {}
return root.get(child_label, [])
def create_db_instance(self):
def create_db_instance(self) -> str:
db_kwargs = self._get_db_kwargs()
database = self.backend.create_db_instance(db_kwargs)
template = self.response_template(CREATE_DATABASE_TEMPLATE)
return template.render(database=database)
def create_db_instance_read_replica(self):
def create_db_instance_read_replica(self) -> str:
db_kwargs = self._get_db_replica_kwargs()
database = self.backend.create_db_instance_read_replica(db_kwargs)
template = self.response_template(CREATE_DATABASE_REPLICA_TEMPLATE)
return template.render(database=database)
def describe_db_instances(self):
def describe_db_instances(self) -> str:
db_instance_identifier = self._get_param("DBInstanceIdentifier")
filters = self._get_multi_param("Filters.Filter.")
filters = {f["Name"]: f["Values"] for f in filters}
filter_dict = {f["Name"]: f["Values"] for f in filters}
all_instances = list(
self.backend.describe_db_instances(db_instance_identifier, filters=filters)
self.backend.describe_db_instances(
db_instance_identifier, filters=filter_dict
)
)
marker = self._get_param("Marker")
all_ids = [instance.db_instance_identifier for instance in all_instances]
@ -278,7 +280,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(DESCRIBE_DATABASES_TEMPLATE)
return template.render(databases=instances_resp, marker=next_marker)
def modify_db_instance(self):
def modify_db_instance(self) -> str:
db_instance_identifier = self._get_param("DBInstanceIdentifier")
db_kwargs = self._get_db_kwargs()
# NOTE modify_db_instance does not support tags
@ -290,7 +292,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(MODIFY_DATABASE_TEMPLATE)
return template.render(database=database)
def delete_db_instance(self):
def delete_db_instance(self) -> str:
db_instance_identifier = self._get_param("DBInstanceIdentifier")
db_snapshot_name = self._get_param("FinalDBSnapshotIdentifier")
database = self.backend.delete_db_instance(
@ -299,13 +301,13 @@ class RDSResponse(BaseResponse):
template = self.response_template(DELETE_DATABASE_TEMPLATE)
return template.render(database=database)
def reboot_db_instance(self):
def reboot_db_instance(self) -> str:
db_instance_identifier = self._get_param("DBInstanceIdentifier")
database = self.backend.reboot_db_instance(db_instance_identifier)
template = self.response_template(REBOOT_DATABASE_TEMPLATE)
return template.render(database=database)
def create_db_snapshot(self):
def create_db_snapshot(self) -> str:
db_instance_identifier = self._get_param("DBInstanceIdentifier")
db_snapshot_identifier = self._get_param("DBSnapshotIdentifier")
tags = self.unpack_list_params("Tags", "Tag")
@ -315,7 +317,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(CREATE_SNAPSHOT_TEMPLATE)
return template.render(snapshot=snapshot)
def copy_db_snapshot(self):
def copy_db_snapshot(self) -> str:
source_snapshot_identifier = self._get_param("SourceDBSnapshotIdentifier")
target_snapshot_identifier = self._get_param("TargetDBSnapshotIdentifier")
tags = self.unpack_list_params("Tags", "Tag")
@ -325,18 +327,18 @@ class RDSResponse(BaseResponse):
template = self.response_template(COPY_SNAPSHOT_TEMPLATE)
return template.render(snapshot=snapshot)
def describe_db_snapshots(self):
def describe_db_snapshots(self) -> str:
db_instance_identifier = self._get_param("DBInstanceIdentifier")
db_snapshot_identifier = self._get_param("DBSnapshotIdentifier")
filters = self._get_multi_param("Filters.Filter.")
filters = {f["Name"]: f["Values"] for f in filters}
filter_dict = {f["Name"]: f["Values"] for f in filters}
snapshots = self.backend.describe_db_snapshots(
db_instance_identifier, db_snapshot_identifier, filters
db_instance_identifier, db_snapshot_identifier, filter_dict
)
template = self.response_template(DESCRIBE_SNAPSHOTS_TEMPLATE)
return template.render(snapshots=snapshots)
def promote_read_replica(self):
def promote_read_replica(self) -> str:
db_instance_identifier = self._get_param("DBInstanceIdentifier")
db_kwargs = self._get_db_kwargs()
database = self.backend.promote_read_replica(db_kwargs)
@ -344,13 +346,13 @@ class RDSResponse(BaseResponse):
template = self.response_template(PROMOTE_REPLICA_TEMPLATE)
return template.render(database=database)
def delete_db_snapshot(self):
def delete_db_snapshot(self) -> str:
db_snapshot_identifier = self._get_param("DBSnapshotIdentifier")
snapshot = self.backend.delete_db_snapshot(db_snapshot_identifier)
template = self.response_template(DELETE_SNAPSHOT_TEMPLATE)
return template.render(snapshot=snapshot)
def restore_db_instance_from_db_snapshot(self):
def restore_db_instance_from_db_snapshot(self) -> str:
db_snapshot_identifier = self._get_param("DBSnapshotIdentifier")
db_kwargs = self._get_db_kwargs()
new_instance = self.backend.restore_db_instance_from_db_snapshot(
@ -359,27 +361,27 @@ class RDSResponse(BaseResponse):
template = self.response_template(RESTORE_INSTANCE_FROM_SNAPSHOT_TEMPLATE)
return template.render(database=new_instance)
def list_tags_for_resource(self):
def list_tags_for_resource(self) -> str:
arn = self._get_param("ResourceName")
template = self.response_template(LIST_TAGS_FOR_RESOURCE_TEMPLATE)
tags = self.backend.list_tags_for_resource(arn)
return template.render(tags=tags)
def add_tags_to_resource(self):
def add_tags_to_resource(self) -> str:
arn = self._get_param("ResourceName")
tags = self.unpack_list_params("Tags", "Tag")
tags = self.backend.add_tags_to_resource(arn, tags)
template = self.response_template(ADD_TAGS_TO_RESOURCE_TEMPLATE)
return template.render(tags=tags)
def remove_tags_from_resource(self):
def remove_tags_from_resource(self) -> str:
arn = self._get_param("ResourceName")
tag_keys = self.unpack_list_params("TagKeys", "member")
self.backend.remove_tags_from_resource(arn, tag_keys)
self.backend.remove_tags_from_resource(arn, tag_keys) # type: ignore
template = self.response_template(REMOVE_TAGS_FROM_RESOURCE_TEMPLATE)
return template.render()
def stop_db_instance(self):
def stop_db_instance(self) -> str:
db_instance_identifier = self._get_param("DBInstanceIdentifier")
db_snapshot_identifier = self._get_param("DBSnapshotIdentifier")
database = self.backend.stop_db_instance(
@ -388,13 +390,13 @@ class RDSResponse(BaseResponse):
template = self.response_template(STOP_DATABASE_TEMPLATE)
return template.render(database=database)
def start_db_instance(self):
def start_db_instance(self) -> str:
db_instance_identifier = self._get_param("DBInstanceIdentifier")
database = self.backend.start_db_instance(db_instance_identifier)
template = self.response_template(START_DATABASE_TEMPLATE)
return template.render(database=database)
def create_db_security_group(self):
def create_db_security_group(self) -> str:
group_name = self._get_param("DBSecurityGroupName")
description = self._get_param("DBSecurityGroupDescription")
tags = self.unpack_list_params("Tags", "Tag")
@ -404,19 +406,19 @@ class RDSResponse(BaseResponse):
template = self.response_template(CREATE_SECURITY_GROUP_TEMPLATE)
return template.render(security_group=security_group)
def describe_db_security_groups(self):
def describe_db_security_groups(self) -> str:
security_group_name = self._get_param("DBSecurityGroupName")
security_groups = self.backend.describe_security_groups(security_group_name)
template = self.response_template(DESCRIBE_SECURITY_GROUPS_TEMPLATE)
return template.render(security_groups=security_groups)
def delete_db_security_group(self):
def delete_db_security_group(self) -> str:
security_group_name = self._get_param("DBSecurityGroupName")
security_group = self.backend.delete_security_group(security_group_name)
template = self.response_template(DELETE_SECURITY_GROUP_TEMPLATE)
return template.render(security_group=security_group)
def authorize_db_security_group_ingress(self):
def authorize_db_security_group_ingress(self) -> str:
security_group_name = self._get_param("DBSecurityGroupName")
cidr_ip = self._get_param("CIDRIP")
security_group = self.backend.authorize_security_group(
@ -425,7 +427,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(AUTHORIZE_SECURITY_GROUP_TEMPLATE)
return template.render(security_group=security_group)
def create_db_subnet_group(self):
def create_db_subnet_group(self) -> str:
subnet_name = self._get_param("DBSubnetGroupName")
description = self._get_param("DBSubnetGroupDescription")
subnet_ids = self._get_multi_param("SubnetIds.SubnetIdentifier")
@ -440,13 +442,13 @@ class RDSResponse(BaseResponse):
template = self.response_template(CREATE_SUBNET_GROUP_TEMPLATE)
return template.render(subnet_group=subnet_group)
def describe_db_subnet_groups(self):
def describe_db_subnet_groups(self) -> str:
subnet_name = self._get_param("DBSubnetGroupName")
subnet_groups = self.backend.describe_db_subnet_groups(subnet_name)
template = self.response_template(DESCRIBE_SUBNET_GROUPS_TEMPLATE)
return template.render(subnet_groups=subnet_groups)
def modify_db_subnet_group(self):
def modify_db_subnet_group(self) -> str:
subnet_name = self._get_param("DBSubnetGroupName")
description = self._get_param("DBSubnetGroupDescription")
subnet_ids = self._get_multi_param("SubnetIds.SubnetIdentifier")
@ -460,25 +462,25 @@ class RDSResponse(BaseResponse):
template = self.response_template(MODIFY_SUBNET_GROUPS_TEMPLATE)
return template.render(subnet_group=subnet_group)
def delete_db_subnet_group(self):
def delete_db_subnet_group(self) -> str:
subnet_name = self._get_param("DBSubnetGroupName")
subnet_group = self.backend.delete_subnet_group(subnet_name)
template = self.response_template(DELETE_SUBNET_GROUP_TEMPLATE)
return template.render(subnet_group=subnet_group)
def create_option_group(self):
def create_option_group(self) -> str:
kwargs = self._get_option_group_kwargs()
option_group = self.backend.create_option_group(kwargs)
template = self.response_template(CREATE_OPTION_GROUP_TEMPLATE)
return template.render(option_group=option_group)
def delete_option_group(self):
def delete_option_group(self) -> str:
kwargs = self._get_option_group_kwargs()
option_group = self.backend.delete_option_group(kwargs["name"])
template = self.response_template(DELETE_OPTION_GROUP_TEMPLATE)
return template.render(option_group=option_group)
def describe_option_groups(self):
def describe_option_groups(self) -> str:
kwargs = self._get_option_group_kwargs()
kwargs["max_records"] = self._get_int_param("MaxRecords")
kwargs["marker"] = self._get_param("Marker")
@ -486,15 +488,14 @@ class RDSResponse(BaseResponse):
template = self.response_template(DESCRIBE_OPTION_GROUP_TEMPLATE)
return template.render(option_groups=option_groups)
def describe_option_group_options(self):
def describe_option_group_options(self) -> str:
engine_name = self._get_param("EngineName")
major_engine_version = self._get_param("MajorEngineVersion")
option_group_options = self.backend.describe_option_group_options(
return self.backend.describe_option_group_options(
engine_name, major_engine_version
)
return option_group_options
def modify_option_group(self):
def modify_option_group(self) -> str:
option_group_name = self._get_param("OptionGroupName")
count = 1
options_to_include = []
@ -530,13 +531,13 @@ class RDSResponse(BaseResponse):
template = self.response_template(MODIFY_OPTION_GROUP_TEMPLATE)
return template.render(option_group=option_group)
def create_db_parameter_group(self):
def create_db_parameter_group(self) -> str:
kwargs = self._get_db_parameter_group_kwargs()
db_parameter_group = self.backend.create_db_parameter_group(kwargs)
template = self.response_template(CREATE_DB_PARAMETER_GROUP_TEMPLATE)
return template.render(db_parameter_group=db_parameter_group)
def describe_db_parameter_groups(self):
def describe_db_parameter_groups(self) -> str:
kwargs = self._get_db_parameter_group_kwargs()
kwargs["max_records"] = self._get_int_param("MaxRecords")
kwargs["marker"] = self._get_param("Marker")
@ -544,7 +545,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(DESCRIBE_DB_PARAMETER_GROUPS_TEMPLATE)
return template.render(db_parameter_groups=db_parameter_groups)
def modify_db_parameter_group(self):
def modify_db_parameter_group(self) -> str:
db_parameter_group_name = self._get_param("DBParameterGroupName")
db_parameter_group_parameters = self._get_db_parameter_group_parameters()
db_parameter_group = self.backend.modify_db_parameter_group(
@ -553,8 +554,8 @@ class RDSResponse(BaseResponse):
template = self.response_template(MODIFY_DB_PARAMETER_GROUP_TEMPLATE)
return template.render(db_parameter_group=db_parameter_group)
def _get_db_parameter_group_parameters(self):
parameter_group_parameters = defaultdict(dict)
def _get_db_parameter_group_parameters(self) -> Iterable[Dict[str, Any]]:
parameter_group_parameters: Dict[str, Any] = defaultdict(dict)
for param_name, value in self.querystring.items():
if not param_name.startswith("Parameters.Parameter"):
continue
@ -567,7 +568,7 @@ class RDSResponse(BaseResponse):
return parameter_group_parameters.values()
def describe_db_parameters(self):
def describe_db_parameters(self) -> str:
db_parameter_group_name = self._get_param("DBParameterGroupName")
db_parameter_groups = self.backend.describe_db_parameter_groups(
{"name": db_parameter_group_name}
@ -578,13 +579,13 @@ class RDSResponse(BaseResponse):
template = self.response_template(DESCRIBE_DB_PARAMETERS_TEMPLATE)
return template.render(db_parameter_group=db_parameter_groups[0])
def delete_db_parameter_group(self):
def delete_db_parameter_group(self) -> str:
kwargs = self._get_db_parameter_group_kwargs()
db_parameter_group = self.backend.delete_db_parameter_group(kwargs["name"])
template = self.response_template(DELETE_DB_PARAMETER_GROUP_TEMPLATE)
return template.render(db_parameter_group=db_parameter_group)
def describe_db_cluster_parameters(self):
def describe_db_cluster_parameters(self) -> str:
db_parameter_group_name = self._get_param("DBParameterGroupName")
db_parameter_groups = self.backend.describe_db_cluster_parameters()
if db_parameter_groups is None:
@ -593,29 +594,29 @@ class RDSResponse(BaseResponse):
template = self.response_template(DESCRIBE_DB_CLUSTER_PARAMETERS_TEMPLATE)
return template.render(db_parameter_group=db_parameter_groups)
def create_db_cluster(self):
def create_db_cluster(self) -> str:
kwargs = self._get_db_cluster_kwargs()
cluster = self.backend.create_db_cluster(kwargs)
template = self.response_template(CREATE_DB_CLUSTER_TEMPLATE)
return template.render(cluster=cluster)
def modify_db_cluster(self):
def modify_db_cluster(self) -> str:
kwargs = self._get_modify_db_cluster_kwargs()
cluster = self.backend.modify_db_cluster(kwargs)
template = self.response_template(MODIFY_DB_CLUSTER_TEMPLATE)
return template.render(cluster=cluster)
def describe_db_clusters(self):
def describe_db_clusters(self) -> str:
_id = self._get_param("DBClusterIdentifier")
filters = self._get_multi_param("Filters.Filter.")
filters = {f["Name"]: f["Values"] for f in filters}
filter_dict = {f["Name"]: f["Values"] for f in filters}
clusters = self.backend.describe_db_clusters(
cluster_identifier=_id, filters=filters
cluster_identifier=_id, filters=filter_dict
)
template = self.response_template(DESCRIBE_CLUSTERS_TEMPLATE)
return template.render(clusters=clusters)
def delete_db_cluster(self):
def delete_db_cluster(self) -> str:
_id = self._get_param("DBClusterIdentifier")
snapshot_name = self._get_param("FinalDBSnapshotIdentifier")
cluster = self.backend.delete_db_cluster(
@ -624,19 +625,19 @@ class RDSResponse(BaseResponse):
template = self.response_template(DELETE_CLUSTER_TEMPLATE)
return template.render(cluster=cluster)
def start_db_cluster(self):
def start_db_cluster(self) -> str:
_id = self._get_param("DBClusterIdentifier")
cluster = self.backend.start_db_cluster(cluster_identifier=_id)
template = self.response_template(START_CLUSTER_TEMPLATE)
return template.render(cluster=cluster)
def stop_db_cluster(self):
def stop_db_cluster(self) -> str:
_id = self._get_param("DBClusterIdentifier")
cluster = self.backend.stop_db_cluster(cluster_identifier=_id)
template = self.response_template(STOP_CLUSTER_TEMPLATE)
return template.render(cluster=cluster)
def create_db_cluster_snapshot(self):
def create_db_cluster_snapshot(self) -> str:
db_cluster_identifier = self._get_param("DBClusterIdentifier")
db_snapshot_identifier = self._get_param("DBClusterSnapshotIdentifier")
tags = self.unpack_list_params("Tags", "Tag")
@ -646,7 +647,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(CREATE_CLUSTER_SNAPSHOT_TEMPLATE)
return template.render(snapshot=snapshot)
def copy_db_cluster_snapshot(self):
def copy_db_cluster_snapshot(self) -> str:
source_snapshot_identifier = self._get_param(
"SourceDBClusterSnapshotIdentifier"
)
@ -660,24 +661,24 @@ class RDSResponse(BaseResponse):
template = self.response_template(COPY_CLUSTER_SNAPSHOT_TEMPLATE)
return template.render(snapshot=snapshot)
def describe_db_cluster_snapshots(self):
def describe_db_cluster_snapshots(self) -> str:
db_cluster_identifier = self._get_param("DBClusterIdentifier")
db_snapshot_identifier = self._get_param("DBClusterSnapshotIdentifier")
filters = self._get_multi_param("Filters.Filter.")
filters = {f["Name"]: f["Values"] for f in filters}
filter_dict = {f["Name"]: f["Values"] for f in filters}
snapshots = self.backend.describe_db_cluster_snapshots(
db_cluster_identifier, db_snapshot_identifier, filters
db_cluster_identifier, db_snapshot_identifier, filter_dict
)
template = self.response_template(DESCRIBE_CLUSTER_SNAPSHOTS_TEMPLATE)
return template.render(snapshots=snapshots)
def delete_db_cluster_snapshot(self):
def delete_db_cluster_snapshot(self) -> str:
db_snapshot_identifier = self._get_param("DBClusterSnapshotIdentifier")
snapshot = self.backend.delete_db_cluster_snapshot(db_snapshot_identifier)
template = self.response_template(DELETE_CLUSTER_SNAPSHOT_TEMPLATE)
return template.render(snapshot=snapshot)
def restore_db_cluster_from_snapshot(self):
def restore_db_cluster_from_snapshot(self) -> str:
db_snapshot_identifier = self._get_param("SnapshotIdentifier")
db_kwargs = self._get_db_cluster_kwargs()
new_cluster = self.backend.restore_db_cluster_from_snapshot(
@ -686,43 +687,43 @@ class RDSResponse(BaseResponse):
template = self.response_template(RESTORE_CLUSTER_FROM_SNAPSHOT_TEMPLATE)
return template.render(cluster=new_cluster)
def start_export_task(self):
def start_export_task(self) -> str:
kwargs = self._get_export_task_kwargs()
export_task = self.backend.start_export_task(kwargs)
template = self.response_template(START_EXPORT_TASK_TEMPLATE)
return template.render(task=export_task)
def cancel_export_task(self):
def cancel_export_task(self) -> str:
export_task_identifier = self._get_param("ExportTaskIdentifier")
export_task = self.backend.cancel_export_task(export_task_identifier)
template = self.response_template(CANCEL_EXPORT_TASK_TEMPLATE)
return template.render(task=export_task)
def describe_export_tasks(self):
def describe_export_tasks(self) -> str:
export_task_identifier = self._get_param("ExportTaskIdentifier")
tasks = self.backend.describe_export_tasks(export_task_identifier)
template = self.response_template(DESCRIBE_EXPORT_TASKS_TEMPLATE)
return template.render(tasks=tasks)
def create_event_subscription(self):
def create_event_subscription(self) -> str:
kwargs = self._get_event_subscription_kwargs()
subscription = self.backend.create_event_subscription(kwargs)
template = self.response_template(CREATE_EVENT_SUBSCRIPTION_TEMPLATE)
return template.render(subscription=subscription)
def delete_event_subscription(self):
def delete_event_subscription(self) -> str:
subscription_name = self._get_param("SubscriptionName")
subscription = self.backend.delete_event_subscription(subscription_name)
template = self.response_template(DELETE_EVENT_SUBSCRIPTION_TEMPLATE)
return template.render(subscription=subscription)
def describe_event_subscriptions(self):
def describe_event_subscriptions(self) -> str:
subscription_name = self._get_param("SubscriptionName")
subscriptions = self.backend.describe_event_subscriptions(subscription_name)
template = self.response_template(DESCRIBE_EVENT_SUBSCRIPTIONS_TEMPLATE)
return template.render(subscriptions=subscriptions)
def describe_orderable_db_instance_options(self):
def describe_orderable_db_instance_options(self) -> str:
engine = self._get_param("Engine")
engine_version = self._get_param("EngineVersion")
options = self.backend.describe_orderable_db_instance_options(
@ -731,12 +732,12 @@ class RDSResponse(BaseResponse):
template = self.response_template(DESCRIBE_ORDERABLE_CLUSTER_OPTIONS)
return template.render(options=options, marker=None)
def describe_global_clusters(self):
def describe_global_clusters(self) -> str:
clusters = self.backend.describe_global_clusters()
template = self.response_template(DESCRIBE_GLOBAL_CLUSTERS_TEMPLATE)
return template.render(clusters=clusters)
def create_global_cluster(self):
def create_global_cluster(self) -> str:
params = self._get_params()
cluster = self.backend.create_global_cluster(
global_cluster_identifier=params["GlobalClusterIdentifier"],
@ -749,7 +750,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(CREATE_GLOBAL_CLUSTER_TEMPLATE)
return template.render(cluster=cluster)
def delete_global_cluster(self):
def delete_global_cluster(self) -> str:
params = self._get_params()
cluster = self.backend.delete_global_cluster(
global_cluster_identifier=params["GlobalClusterIdentifier"],
@ -757,7 +758,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(DELETE_GLOBAL_CLUSTER_TEMPLATE)
return template.render(cluster=cluster)
def remove_from_global_cluster(self):
def remove_from_global_cluster(self) -> str:
params = self._get_params()
global_cluster = self.backend.remove_from_global_cluster(
global_cluster_identifier=params["GlobalClusterIdentifier"],
@ -766,7 +767,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(REMOVE_FROM_GLOBAL_CLUSTER_TEMPLATE)
return template.render(cluster=global_cluster)
def create_db_cluster_parameter_group(self):
def create_db_cluster_parameter_group(self) -> str:
group_name = self._get_param("DBClusterParameterGroupName")
family = self._get_param("DBParameterGroupFamily")
desc = self._get_param("Description")
@ -778,7 +779,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(CREATE_DB_CLUSTER_PARAMETER_GROUP_TEMPLATE)
return template.render(db_cluster_parameter_group=db_cluster_parameter_group)
def describe_db_cluster_parameter_groups(self):
def describe_db_cluster_parameter_groups(self) -> str:
group_name = self._get_param("DBClusterParameterGroupName")
db_parameter_groups = self.backend.describe_db_cluster_parameter_groups(
group_name=group_name,
@ -786,7 +787,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(DESCRIBE_DB_CLUSTER_PARAMETER_GROUPS_TEMPLATE)
return template.render(db_parameter_groups=db_parameter_groups)
def delete_db_cluster_parameter_group(self):
def delete_db_cluster_parameter_group(self) -> str:
group_name = self._get_param("DBClusterParameterGroupName")
self.backend.delete_db_cluster_parameter_group(
group_name=group_name,
@ -794,7 +795,7 @@ class RDSResponse(BaseResponse):
template = self.response_template(DELETE_DB_CLUSTER_PARAMETER_GROUP_TEMPLATE)
return template.render()
def promote_read_replica_db_cluster(self):
def promote_read_replica_db_cluster(self) -> str:
db_cluster_identifier = self._get_param("DBClusterIdentifier")
cluster = self.backend.promote_read_replica_db_cluster(db_cluster_identifier)
template = self.response_template(PROMOTE_READ_REPLICA_DB_CLUSTER_TEMPLATE)

View File

@ -1,6 +1,6 @@
import copy
from collections import namedtuple
from typing import Any, Dict
from typing import Any, Dict, Tuple, Optional
from botocore.utils import merge_dicts
@ -22,7 +22,7 @@ FilterDef = namedtuple(
)
def get_object_value(obj, attr):
def get_object_value(obj: Any, attr: str) -> Any:
"""Retrieves an arbitrary attribute value from an object.
Nested attributes can be specified using dot notation,
@ -47,7 +47,9 @@ def get_object_value(obj, attr):
return val
def merge_filters(filters_to_update, filters_to_merge):
def merge_filters(
filters_to_update: Optional[Dict[str, Any]], filters_to_merge: Dict[str, Any]
) -> Dict[str, Any]:
"""Given two groups of filters, merge the second into the first.
List values are appended instead of overwritten:
@ -76,7 +78,9 @@ def merge_filters(filters_to_update, filters_to_merge):
return filters_to_update
def validate_filters(filters, filter_defs):
def validate_filters(
filters: Dict[str, Any], filter_defs: Dict[str, FilterDef]
) -> None:
"""Validates filters against a set of filter definitions.
Raises standard Python exceptions which should be caught
@ -108,7 +112,7 @@ def validate_filters(filters, filter_defs):
)
def apply_filter(resources, filters, filter_defs):
def apply_filter(resources: Any, filters: Any, filter_defs: Any) -> Any:
"""Apply an arbitrary filter to a group of resources.
:param dict[str, object] resources:
@ -140,7 +144,9 @@ def apply_filter(resources, filters, filter_defs):
return resources_filtered
def get_start_date_end_date(base_date, window):
def get_start_date_end_date(
base_date: str, window: str
) -> Tuple[datetime.datetime, datetime.datetime]:
"""Gets the start date and end date given DDD:HH24:MM-DDD:HH24:MM.
:param base_date:
@ -162,11 +168,11 @@ def get_start_date_end_date(base_date, window):
return start, end
def get_start_date_end_date_from_time(base_date, window):
def get_start_date_end_date_from_time(
base_date: str, window: str
) -> Tuple[datetime.datetime, datetime.datetime, bool]:
"""Gets the start date and end date given HH24:MM-HH24:MM.
:param base_date:
type datetime
:param window:
HH24:MM-HH24:MM
:returns:
@ -187,31 +193,23 @@ def get_start_date_end_date_from_time(base_date, window):
def get_overlap_between_two_date_ranges(
start_time_1, end_time_1, start_time_2, end_time_2
):
"""Determines overlap between 2 date ranges.
:param start_time_1:
type datetime
:param start_time_2:
type datetime
:param end_time_1:
type datetime
:param end_time_2:
type datetime
:returns:
overlap in seconds
:rtype:
int
start_time_1: datetime.datetime,
end_time_1: datetime.datetime,
start_time_2: datetime.datetime,
end_time_2: datetime.datetime,
) -> int:
"""
Determines overlap between 2 date ranges. Returns the overlap in seconds.
"""
latest_start = max(start_time_1, start_time_2)
earliest_end = min(end_time_1, end_time_2)
delta = earliest_end - latest_start
overlap = (delta.days * SECONDS_IN_ONE_DAY) + delta.seconds
return overlap
return (delta.days * SECONDS_IN_ONE_DAY) + delta.seconds
def valid_preferred_maintenance_window(maintenance_window, backup_window):
def valid_preferred_maintenance_window(
maintenance_window: Any, backup_window: Any
) -> Optional[str]:
"""Determines validity of preferred_maintenance_window
:param maintenance_windown:
@ -283,7 +281,7 @@ def valid_preferred_maintenance_window(maintenance_window, backup_window):
delta = maintenance_window_end - maintenance_window_start
delta_seconds = delta.seconds + (delta.days * SECONDS_IN_ONE_DAY)
if delta_seconds >= MINUTES_30 and delta_seconds <= HOURS_24:
return
return None
elif delta_seconds >= 0 and delta_seconds <= MINUTES_30:
return "The maintenance window must be at least 30 minutes."
else:

View File

@ -235,7 +235,7 @@ disable = W,C,R,E
enable = anomalous-backslash-in-string, arguments-renamed, dangerous-default-value, deprecated-module, function-redefined, import-self, redefined-builtin, redefined-outer-name, reimported, pointless-statement, super-with-arguments, unused-argument, unused-import, unused-variable, useless-else-on-loop, wildcard-import
[mypy]
files= moto/a*,moto/b*,moto/c*,moto/d*,moto/e*,moto/f*,moto/g*,moto/i*,moto/k*,moto/l*,moto/m*,moto/n*,moto/o*,moto/p*,moto/q*,moto/ram,moto/rdsdata,moto/scheduler
files= moto/a*,moto/b*,moto/c*,moto/d*,moto/e*,moto/f*,moto/g*,moto/i*,moto/k*,moto/l*,moto/m*,moto/n*,moto/o*,moto/p*,moto/q*,moto/ram,moto/rds,moto/rdsdata,moto/scheduler
show_column_numbers=True
show_error_codes = True
disable_error_code=abstract