diff --git a/TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/middleware/clusters.py b/TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/middleware/clusters.py index 3f644d423..81a279926 100644 --- a/TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/middleware/clusters.py +++ b/TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/middleware/clusters.py @@ -60,3 +60,43 @@ def clear_indices(cluster: Cluster): clear_indices_path = "/*,-.*,-searchguard*,-sg7*,.migrations_working_state" r = cluster.call_api(clear_indices_path, method=HttpMethod.DELETE, params={"ignore_unavailable": "true"}) return r.content + + +def clear_cluster(cluster: Cluster): + clear_indices(cluster) + clear_snapshots(cluster, 'migration_assistant_repo') + + +def clear_snapshots(cluster: Cluster, repository: str) -> None: + """ + Clears all snapshots from the specified repository. + + :param cluster: Cluster object to interact with the Elasticsearch cluster. + :param repository: Name of the snapshot repository to clear snapshots from. + :raises Exception: For general errors during snapshot clearing, except when the repository is missing. + """ + try: + # List all snapshots in the repository + snapshots_path = f"/_snapshot/{repository}/_all" + response = call_api(cluster, snapshots_path, raise_error=True) + snapshots = response.json().get("snapshots", []) + + if not snapshots: + logger.info(f"No snapshots found in repository '{repository}'.") + return + + # Delete each snapshot + for snapshot in snapshots: + snapshot_name = snapshot["snapshot"] + delete_path = f"/_snapshot/{repository}/{snapshot_name}" + call_api(cluster, delete_path, method=HttpMethod.DELETE, raise_error=True) + logger.info(f"Deleted snapshot: {snapshot_name} from repository '{repository}'.") + + except Exception as e: + # Handle 404 errors specifically for missing repository + if "repository_missing_exception" in str(e): + logger.info(f"Repository '{repository}' is missing. Skipping snapshot clearing.") + else: + # Re-raise other errors + logger.error(f"Error clearing snapshots from repository '{repository}': {e}") + raise e diff --git a/TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/integ_test/integ_test/backfill_tests.py b/TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/integ_test/integ_test/backfill_tests.py index 70f53b4e1..b4f3f4920 100644 --- a/TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/integ_test/integ_test/backfill_tests.py +++ b/TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/integ_test/integ_test/backfill_tests.py @@ -2,11 +2,10 @@ import pytest import unittest from http import HTTPStatus -from console_link.middleware.clusters import run_test_benchmarks, connection_check, clear_indices, ConnectionResult +from console_link.middleware.clusters import run_test_benchmarks, connection_check, clear_cluster, ConnectionResult from console_link.models.cluster import Cluster from console_link.models.backfill_base import Backfill from console_link.models.command_result import CommandResult -from console_link.models.snapshot import Snapshot from console_link.models.metadata import Metadata from console_link.cli import Context from common_operations import (get_document, create_document, create_index, check_doc_counts_match, @@ -22,9 +21,9 @@ def preload_data(source_cluster: Cluster, target_cluster: Cluster): target_con_result: ConnectionResult = connection_check(target_cluster) assert target_con_result.connection_established is True - # Clear any existing non-system indices - clear_indices(source_cluster) - clear_indices(target_cluster) + # Clear all data from clusters + clear_cluster(source_cluster) + clear_cluster(target_cluster) # Preload data that test cases will verify is migrated # test_backfill_0001 @@ -52,11 +51,7 @@ def setup_backfill(request): assert metadata is not None backfill.create() - snapshot: Snapshot = pytest.console_env.snapshot - status_result: CommandResult = snapshot.status() - if status_result.success: - snapshot.delete() - snapshot_result: CommandResult = snapshot.create(wait=True) + snapshot_result: CommandResult = pytest.console_env.snapshot.create(wait=True) assert snapshot_result.success metadata_result: CommandResult = metadata.migrate() assert metadata_result.success