From f0875137aaf5926ab069c304faa159a4dbe797dd Mon Sep 17 00:00:00 2001 From: Stefan Nordhausen Date: Fri, 27 Dec 2024 17:18:51 +0100 Subject: [PATCH] Allow restore_db_instance_to_point_in_time in custom subnet Previously, this failed with TypeError: cannot pickle 'generator' object when the code tried to deepcopy the `backend`. Merge it with the existing code that removed "db_subnet_group". --- moto/rds/models.py | 16 ++++++++-------- tests/test_rds/test_rds.py | 12 +++++++++++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/moto/rds/models.py b/moto/rds/models.py index 9b494c10e04b..7ffb42061224 100644 --- a/moto/rds/models.py +++ b/moto/rds/models.py @@ -2065,14 +2065,14 @@ def restore_db_instance_to_point_in_time( db_instance_identifier=source_db_identifier )[0] - # remove the db subnet group as it cannot be copied - # and is not used in the restored instance - source_dict = db_instance.__dict__ - if "db_subnet_group" in source_dict: - del source_dict["db_subnet_group"] - - new_instance_props = copy.deepcopy(source_dict) - new_instance_props.pop("backend") + new_instance_props = {} + for key, value in db_instance.__dict__.items(): + # Remove backend / db subnet group as they cannot be copied + # and are not used in the restored instance. + if key in ("backend", "db_subnet_group"): + continue + new_instance_props[key] = copy.deepcopy(value) + if not db_instance.option_group_supplied: # If the option group is not supplied originally, the 'option_group_name' will receive a default value # Force this reconstruction, and prevent any validation on the default value diff --git a/tests/test_rds/test_rds.py b/tests/test_rds/test_rds.py index 00bf451867df..9003cccc7b10 100644 --- a/tests/test_rds/test_rds.py +++ b/tests/test_rds/test_rds.py @@ -1424,9 +1424,18 @@ def test_restore_db_instance_from_db_snapshot( ) +@pytest.mark.parametrize( + "custom_db_subnet_group", [True, False], ids=("custom_subnet", "default_subnet") +) @mock_aws -def test_restore_db_instance_to_point_in_time(): +def test_restore_db_instance_to_point_in_time(custom_db_subnet_group: bool): conn = boto3.client("rds", region_name=DEFAULT_REGION) + + if custom_db_subnet_group: + extra_kwargs = {"DBSubnetGroupName": create_db_subnet_group()} + else: + extra_kwargs = {} + conn.create_db_instance( DBInstanceIdentifier="db-primary-1", AllocatedStorage=10, @@ -1436,6 +1445,7 @@ def test_restore_db_instance_to_point_in_time(): MasterUsername="root", MasterUserPassword="hunter2", DBSecurityGroups=["my_sg"], + **extra_kwargs, ) assert len(conn.describe_db_instances()["DBInstances"]) == 1