Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add region to rotate_rds_snapshots DAG (#1099)

sarayourfriend8c0d0e51a410
diff --git a/openverse_catalog/dags/maintenance/rotate_db_snapshots.py b/openverse_catalog/dags/maintenance/rotate_db_snapshots.py
index 764e24fc6..7df6960ac 100644
--- a/openverse_catalog/dags/maintenance/rotate_db_snapshots.py
+++ b/openverse_catalog/dags/maintenance/rotate_db_snapshots.py
@@ -31,8 +31,8 @@ MAX_ACTIVE = 1
@task()
-def delete_previous_snapshots(rds_arn: str, snapshots_to_retain: int):
- rds = boto3.client("rds")
+def delete_previous_snapshots(rds_arn: str, snapshots_to_retain: int, rds_region: str):
+ rds = boto3.client("rds", region_name=rds_region)
# Snapshot object documentation:
# https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_DBSnapshot.html
snapshots = rds.describe_db_snapshots(
@@ -71,18 +71,23 @@ def delete_previous_snapshots(rds_arn: str, snapshots_to_retain: int):
def rotate_db_snapshots():
snapshot_id = "airflow-{{ ds }}"
db_identifier_template = "{{ var.value.AIRFLOW_RDS_ARN }}"
+ hook_params = {"region_name": "{{ var.value.AIRFLOW_RDS_REGION }}"}
+
create_db_snapshot = RdsCreateDbSnapshotOperator(
task_id="create_snapshot",
db_type="instance",
db_identifier=db_identifier_template,
db_snapshot_identifier=snapshot_id,
+ hook_params=hook_params,
)
+
wait_for_snapshot_availability = RdsSnapshotExistenceSensor(
task_id="await_snapshot_availability",
db_type="instance",
db_snapshot_identifier=snapshot_id,
# This is the default for ``target_statuses`` but making it explicit is clearer
target_statuses=["available"],
+ hook_params=hook_params,
)
(
@@ -91,6 +96,7 @@ def rotate_db_snapshots():
>> delete_previous_snapshots(
rds_arn=db_identifier_template,
snapshots_to_retain="{{ var.json.AIRFLOW_RDS_SNAPSHOTS_TO_RETAIN }}",
+ rds_region=hook_params["region_name"],
)
)
diff --git a/tests/dags/maintenance/test_rotate_db_snapshots.py b/tests/dags/maintenance/test_rotate_db_snapshots.py
index 80a1a1610..c00cfba52 100644
--- a/tests/dags/maintenance/test_rotate_db_snapshots.py
+++ b/tests/dags/maintenance/test_rotate_db_snapshots.py
@@ -9,13 +9,18 @@ from maintenance.rotate_db_snapshots import delete_previous_snapshots
@pytest.fixture
-def rds_client(monkeypatch):
- rds = mock.MagicMock()
-
- def get_client(*args, **kwargs):
- return rds
+def boto_client(monkeypatch):
+ get_client = mock.MagicMock()
monkeypatch.setattr(boto3, "client", get_client)
+ return get_client
+
+
+def rds_client(boto_client):
+ rds = mock.MagicMock()
+
+ boto_client.return_value = rds
return rds
@@ -48,7 +53,7 @@ def test_delete_previous_snapshots_no_snapshots_to_delete(
snapshots, snapshots_to_retain, rds_client
):
rds_client.describe_db_snapshots.return_value = snapshots
- delete_previous_snapshots.function("fake_arn", snapshots_to_retain)
+ delete_previous_snapshots.function("fake_arn", snapshots_to_retain, "fake_region")
rds_client.delete_db_snapshot.assert_not_called()
@@ -57,7 +62,8 @@ def test_delete_previous_snapshots(rds_client):
snapshots = _make_snapshots(10)
snapshots_to_delete = snapshots["DBSnapshots"][snapshots_to_retain:]
rds_client.describe_db_snapshots.return_value = snapshots
- delete_previous_snapshots.function("fake_arn", snapshots_to_retain)
+
+ delete_previous_snapshots.function("fake_arn", snapshots_to_retain, "fake_region")
rds_client.delete_db_snapshot.assert_has_calls(
[
mock.call(DBSnapshotIdentifier=snapshot["DBSnapshotIdentifier"])
@@ -79,10 +85,18 @@ def test_sorts_snapshots(rds_client):
random.shuffle(snapshots["DBSnapshots"])
rds_client.describe_db_snapshots.return_value = snapshots
- delete_previous_snapshots.function("fake_arn", snapshots_to_retain)
+ delete_previous_snapshots.function("fake_arn", snapshots_to_retain, "fake_region")
rds_client.delete_db_snapshot.assert_has_calls(
[
mock.call(DBSnapshotIdentifier=snapshot["DBSnapshotIdentifier"])
for snapshot in snapshots_to_delete
]
)
+
+
+def test_instantiates_rds_client_with_region(boto_client, rds_client):
+ rds_client.describe_db_snapshots.return_value = _make_snapshots(0)
+
+ region = "fake_region"
+ delete_previous_snapshots.function("fake_arn", 0, region)
+ boto_client.assert_called_once_with("rds", region_name=region)