Skip to content

Commit

Permalink
RDS: Check if new instance ID already exists (#8475)
Browse files Browse the repository at this point in the history
  • Loading branch information
snordhausen authored Jan 10, 2025
1 parent 92b2c5c commit c60f261
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
5 changes: 5 additions & 0 deletions moto/rds/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def __init__(self, database_identifier: str):
)


class DBInstanceAlreadyExists(RDSClientError):
def __init__(self) -> None:
super().__init__("DBInstanceAlreadyExists", "DB instance already exists")


class DBSnapshotNotFoundError(RDSClientError):
def __init__(self, snapshot_identifier: str):
super().__init__(
Expand Down
7 changes: 7 additions & 0 deletions moto/rds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
DBClusterSnapshotAlreadyExistsError,
DBClusterSnapshotNotFoundError,
DBClusterToBeDeletedHasActiveMembers,
DBInstanceAlreadyExists,
DBInstanceNotFoundError,
DBParameterGroupNotFoundError,
DBProxyAlreadyExistsFault,
Expand Down Expand Up @@ -1822,6 +1823,8 @@ def db_cluster_options(self, engine) -> List[Dict[str, Any]]: # type: ignore

def create_db_instance(self, db_kwargs: Dict[str, Any]) -> DBInstance:
database_id = db_kwargs["db_instance_identifier"]
if database_id in self.databases:
raise DBInstanceAlreadyExists()
self._validate_db_identifier(database_id)
database = DBInstance(self, **db_kwargs)

Expand Down Expand Up @@ -2040,6 +2043,10 @@ def restore_db_instance_from_db_snapshot(
db_instance_identifier=None, db_snapshot_identifier=from_snapshot_id
)[0]
original_database = snapshot.database

if overrides["db_instance_identifier"] in self.databases:
raise DBInstanceAlreadyExists()

new_instance_props = {}
for key, value in original_database.__dict__.items():
if key != "backend":
Expand Down
29 changes: 26 additions & 3 deletions tests/test_rds/test_rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ def test_create_database(client):
assert db_instance["DbInstancePort"] == 1234


@mock_aws
def test_create_database_already_exists():
create_db_instance()
with pytest.raises(ClientError) as exc:
create_db_instance()
err = exc.value.response["Error"]
assert err["Message"] == "DB instance already exists"


@mock_aws
def test_database_with_deletion_protection_cannot_be_deleted():
db_instance = create_db_instance(DeletionProtection=True)
Expand Down Expand Up @@ -985,9 +994,6 @@ def test_restore_db_instance_from_db_snapshot(
db_subnet_group_name = create_db_subnet_group()

# restore
new_instance = client.restore_db_instance_from_db_snapshot(
DBInstanceIdentifier="db-restore-1", DBSnapshotIdentifier=db_snapshot_identifier
)["DBInstance"]
kwargs = {
"DBInstanceIdentifier": "db-restore-1",
"DBSnapshotIdentifier": db_snapshot_identifier,
Expand Down Expand Up @@ -1024,6 +1030,23 @@ def test_restore_db_instance_from_db_snapshot(
)


@mock_aws
def test_restore_db_instance_from_db_snapshot_called_twice(client):
create_db_instance(DBInstanceIdentifier="db-primary-1")
client.create_db_snapshot(
DBInstanceIdentifier="db-primary-1", DBSnapshotIdentifier="snapshot"
)
client.restore_db_instance_from_db_snapshot(
DBInstanceIdentifier="db-restore-1", DBSnapshotIdentifier="snapshot"
)
with pytest.raises(ClientError) as exc:
client.restore_db_instance_from_db_snapshot(
DBInstanceIdentifier="db-restore-1", DBSnapshotIdentifier="snapshot"
)
err = exc.value.response["Error"]
assert err["Message"] == "DB instance already exists"


@pytest.mark.parametrize(
"custom_db_subnet_group", [True, False], ids=("custom_subnet", "default_subnet")
)
Expand Down

0 comments on commit c60f261

Please sign in to comment.