From 3f948aa5ae3bbac1d0fa5efd82d79bde8b10f820 Mon Sep 17 00:00:00 2001 From: mattBrzezinski Date: Fri, 6 May 2022 12:11:55 -0500 Subject: [PATCH] RDS Filtering on db-cluster-id (#5101) --- moto/rds/models.py | 5 ++++- moto/rds/responses.py | 1 + tests/test_rds/test_filters.py | 16 ++++++++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/moto/rds/models.py b/moto/rds/models.py index f15287337..c6029b4b8 100644 --- a/moto/rds/models.py +++ b/moto/rds/models.py @@ -307,7 +307,7 @@ class ClusterSnapshot(BaseModel): class Database(CloudFormationModel): SUPPORTED_FILTERS = { - "db-cluster-id": FilterDef(None, "DB Cluster Identifiers"), + "db-cluster-id": FilterDef(["db_cluster_identifier"], "DB Cluster Identifiers"), "db-instance-id": FilterDef( ["db_instance_arn", "db_instance_identifier"], "DB Instance Identifiers" ), @@ -357,6 +357,7 @@ class Database(CloudFormationModel): self.allocated_storage = Database.default_allocated_storage( engine=self.engine, storage_type=self.storage_type ) + self.db_cluster_identifier = kwargs.get("db_cluster_identifier") self.db_instance_identifier = kwargs.get("db_instance_identifier") self.source_db_identifier = kwargs.get("source_db_identifier") self.db_instance_class = kwargs.get("db_instance_class") @@ -494,6 +495,7 @@ class Database(CloudFormationModel): {% endfor %} + {{ database.db_cluster_identifier }} {{ database.db_instance_identifier }} {{ database.dbi_resource_id }} {{ database.instance_create_time }} @@ -740,6 +742,7 @@ class Database(CloudFormationModel): "BackupRetentionPeriod": "{{ database.backup_retention_period }}", "CharacterSetName": {%- if database.character_set_name -%}{{ database.character_set_name }}{%- else %} null{%- endif -%}, "DBInstanceClass": "{{ database.db_instance_class }}", + "DBClusterIdentifier": "{{ database.db_cluster_identifier }}", "DBInstanceIdentifier": "{{ database.db_instance_identifier }}", "DBInstanceStatus": "{{ database.status }}", "DBName": {%- if database.db_name -%}"{{ database.db_name }}"{%- else %} null{%- endif -%}, diff --git a/moto/rds/responses.py b/moto/rds/responses.py index 3afa4824e..c2b7b9bac 100644 --- a/moto/rds/responses.py +++ b/moto/rds/responses.py @@ -19,6 +19,7 @@ class RDSResponse(BaseResponse): "backup_retention_period": self._get_param("BackupRetentionPeriod"), "copy_tags_to_snapshot": self._get_param("CopyTagsToSnapshot"), "db_instance_class": self._get_param("DBInstanceClass"), + "db_cluster_identifier": self._get_param("DBClusterIdentifier"), "db_instance_identifier": self._get_param("DBInstanceIdentifier"), "db_name": self._get_param("DBName"), "db_parameter_group_name": self._get_param("DBParameterGroupName"), diff --git a/tests/test_rds/test_filters.py b/tests/test_rds/test_filters.py index 87fac8f40..81e8e46ad 100644 --- a/tests/test_rds/test_filters.py +++ b/tests/test_rds/test_filters.py @@ -15,10 +15,12 @@ class TestDBInstanceFilters(object): cls.mock.start() client = boto3.client("rds", region_name="us-west-2") for i in range(10): - identifier = "db-instance-{}".format(i) + instance_identifier = "db-instance-{}".format(i) + cluster_identifier = "db-cluster-{}".format(i) engine = "postgres" if (i % 3) else "mysql" client.create_db_instance( - DBInstanceIdentifier=identifier, + DBInstanceIdentifier=instance_identifier, + DBClusterIdentifier=cluster_identifier, Engine=engine, DBInstanceClass="db.m1.small", ) @@ -49,6 +51,16 @@ class TestDBInstanceFilters(object): ex.value.response["Error"]["Code"].should.equal("InvalidParameterCombination") ex.value.response["Error"]["Message"].should.contain("must not be empty") + def test_db_cluster_id_filter(self): + resp = self.client.describe_db_instances() + db_cluster_identifier = resp["DBInstances"][0]["DBClusterIdentifier"] + + db_instances = self.client.describe_db_instances( + Filters=[{"Name": "db-cluster-id", "Values": [db_cluster_identifier]}] + ).get("DBInstances") + db_instances.should.have.length_of(1) + db_instances[0]["DBClusterIdentifier"].should.equal(db_cluster_identifier) + def test_db_instance_id_filter(self): resp = self.client.describe_db_instances() db_instance_identifier = resp["DBInstances"][0]["DBInstanceIdentifier"]