diff --git a/dnastack/cli/commands/workbench/storage/commands.py b/dnastack/cli/commands/workbench/storage/commands.py index 7d4fcd97..71cbbee9 100644 --- a/dnastack/cli/commands/workbench/storage/commands.py +++ b/dnastack/cli/commands/workbench/storage/commands.py @@ -1,3 +1,4 @@ +from time import monotonic, sleep from typing import Optional, List import click @@ -9,7 +10,36 @@ from dnastack.cli.core.command_spec import ArgumentSpec, ArgumentType, CONTEXT_ARG, SINGLE_ENDPOINT_ID_ARG from dnastack.cli.helpers.exporter import to_json, normalize from dnastack.cli.helpers.iterator_printer import OutputFormat, show_iterator -from dnastack.client.workbench.storage.models import StorageListOptions, Provider +from dnastack.client.workbench.storage.client import StorageClient +from dnastack.client.workbench.storage.models import StorageAccount, StorageListOptions, Provider + +REINDEX_POLL_INTERVAL_SECONDS = 10 +REINDEX_DEFAULT_TIMEOUT_SECONDS = 3600 + + +def _wait_for_indexing(client: StorageClient, + storage_id: str, + account: StorageAccount, + timeout: int, + last_completed_at: Optional[str]) -> StorageAccount: + """Poll until indexing stops, and fail unless it recorded a completed run after `last_completed_at`.""" + deadline = monotonic() + timeout + while account.indexing_status == 'RUNNING': + remaining = deadline - monotonic() + if remaining <= 0: + raise click.ClickException(f'Indexing of storage account {storage_id} is still running after ' + f'{timeout} seconds.') + sleep(min(REINDEX_POLL_INTERVAL_SECONDS, remaining)) + account = client.get_storage_account(storage_id) + + if account.indexing_status != 'IDLE': + raise click.ClickException(f'Storage account {storage_id} reports indexing status ' + f'{account.indexing_status}, which --wait does not recognise.') + # Workbench records last_indexed_at only when a run completes; a failed run returns to IDLE without it. + if account.last_indexed_at == last_completed_at: + raise click.ClickException(f'Indexing of storage account {storage_id} stopped but did not complete. ' + f'The last completed indexing is still {last_completed_at or "none"}.') + return account def init_storage_commands(group: Group): @@ -120,6 +150,63 @@ def get_storage_accounts(context: Optional[str], storage_accounts = [client.get_storage_account(storage_account_id) for storage_account_id in storage_id] click.echo(to_json(normalize(storage_accounts))) + @formatted_command( + group=group, + name='reindex', + specs=[ + ArgumentSpec( + name='storage_id', + arg_type=ArgumentType.POSITIONAL, + help='The storage account id', + required=True, + ), + ArgumentSpec( + name='wait', + arg_names=['--wait'], + help='Wait for indexing to finish, then print the account. Exits non-zero if indexing did not ' + 'complete. Indexing keeps running if the wait ends early.', + type=bool, + ), + ArgumentSpec( + name='timeout', + arg_names=['--timeout'], + help=f'With --wait, how many seconds to wait before giving up ' + f'(default {REINDEX_DEFAULT_TIMEOUT_SECONDS}).', + type=int, + ), + NAMESPACE_ARG, + CONTEXT_ARG, + SINGLE_ENDPOINT_ID_ARG, + ] + ) + def reindex_storage_account(context: Optional[str], + endpoint_id: Optional[str], + namespace: Optional[str], + storage_id: str, + wait: bool = False, + timeout: Optional[int] = None): + """ + Re-scan a storage account for sample files + + Indexing runs in the background: without --wait the command returns once indexing has started. Indexing + also removes sample files whose objects are no longer in the bucket. Requires the workbench.storage.update + permission (namespace admin). + """ + if timeout is not None and not wait: + raise click.BadParameter('only applies with --wait.', param_hint="'--timeout'") + if timeout is not None and timeout < 0: + raise click.BadParameter('must be zero or more seconds.', param_hint="'--timeout'") + client = get_storage_client(context, endpoint_id, namespace) + if not wait: + click.echo(to_json(normalize(client.reindex_storage_account(storage_id)))) + return - - + before = client.get_storage_account(storage_id) + if before.indexing_status is None: + raise click.ClickException('This Workbench does not report indexing status, so --wait cannot ' + 'tell when indexing finishes.') + account = client.reindex_storage_account(storage_id) + account = _wait_for_indexing(client, storage_id, account, + REINDEX_DEFAULT_TIMEOUT_SECONDS if timeout is None else timeout, + before.last_indexed_at) + click.echo(to_json(normalize(account))) diff --git a/dnastack/client/workbench/storage/client.py b/dnastack/client/workbench/storage/client.py index e2f50b3e..afdd096f 100644 --- a/dnastack/client/workbench/storage/client.py +++ b/dnastack/client/workbench/storage/client.py @@ -4,7 +4,7 @@ from dnastack import ServiceEndpoint from dnastack.client.result_iterator import ResultIterator from dnastack.client.service_registry.models import ServiceType -from dnastack.client.workbench.base_client import BaseWorkbenchClient, WorkbenchResultLoader +from dnastack.client.workbench.base_client import BaseWorkbenchClient, WorkbenchResultLoader, path_segment from dnastack.client.workbench.storage.models import StorageAccount, StorageListOptions, StorageListResponse from dnastack.common.tracing import Span from dnastack.http.session import HttpSession @@ -48,6 +48,9 @@ def make(cls, endpoint: ServiceEndpoint, namespace: str): endpoint.type = cls.get_default_service_type() return cls(endpoint, namespace) + def _account_url(self, storage_account_id: str, suffix: str = '') -> str: + return urljoin(self.endpoint.url, f'{self.namespace}/storage/{path_segment(storage_account_id)}{suffix}') + def add_storage_account(self, storage_account: StorageAccount) -> StorageAccount: """Create a new storage account.""" with self.create_http_session() as session: @@ -61,7 +64,7 @@ def update_storage_account(self, storage_account_id: str, storage_account: Stora """Update a storage account.""" with self.create_http_session() as session: response = session.submit("PUT", - urljoin(self.endpoint.url, f'{self.namespace}/storage/{storage_account_id}'), + self._account_url(storage_account_id), json=storage_account.model_dump(), headers={"If-Match": if_match} ) @@ -71,13 +74,20 @@ def update_storage_account(self, storage_account_id: str, storage_account: Stora def delete_storage_account(self, storage_account_id: str) -> None: """Delete a storage account.""" with self.create_http_session() as session: - session.delete(urljoin(self.endpoint.url, f'{self.namespace}/storage/{storage_account_id}')) + session.delete(self._account_url(storage_account_id)) return None def get_storage_account(self, storage_account_id: str) -> StorageAccount: """Get a storage account by its ID.""" with self.create_http_session() as session: - response = session.get(urljoin(self.endpoint.url, f'{self.namespace}/storage/{storage_account_id}')) + response = session.get(self._account_url(storage_account_id)) + return StorageAccount(**response.json()) + + def reindex_storage_account(self, storage_account_id: str) -> StorageAccount: + """Start re-scanning a storage account's bucket. Indexing runs in the background: the returned account + reports RUNNING until it finishes, or IDLE if indexing already finished before the server responded.""" + with self.create_http_session() as session: + response = session.post(self._account_url(storage_account_id, '/reindex')) return StorageAccount(**response.json()) def list_storage_accounts(self, list_options: Optional[StorageListOptions], max_results: int) -> Iterator[StorageAccount]: diff --git a/dnastack/client/workbench/storage/models.py b/dnastack/client/workbench/storage/models.py index d31189e0..f8b078f7 100644 --- a/dnastack/client/workbench/storage/models.py +++ b/dnastack/client/workbench/storage/models.py @@ -58,6 +58,8 @@ class StorageAccount(BaseModel): created_at: Optional[str] = None last_updated_at: Optional[str] = None bucket: Optional[str] = None + indexing_status: Optional[str] = None + last_indexed_at: Optional[str] = None credentials: Optional[Union[AwsStorageAccountCredentials, GcpStorageAccountCredentials, AzureStorageAccountCredentials]] = None diff --git a/tests/unit/cli/commands/workbench/storage/__init__.py b/tests/unit/cli/commands/workbench/storage/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/cli/commands/workbench/storage/test_commands.py b/tests/unit/cli/commands/workbench/storage/test_commands.py new file mode 100644 index 00000000..d3a5884a --- /dev/null +++ b/tests/unit/cli/commands/workbench/storage/test_commands.py @@ -0,0 +1,156 @@ +import json +from unittest.mock import patch + +import pytest +import responses +from click import Group +from click.testing import CliRunner + +from dnastack.cli.commands.workbench.storage.commands import init_storage_commands +from dnastack.client.models import ServiceEndpoint +from dnastack.client.workbench.storage.client import StorageClient + +_SAMPLE_SERVICE_URL = 'https://sample-service.example.com/' +_ACCOUNT_URL = f'{_SAMPLE_SERVICE_URL}ns-1/storage/storage-1' + + +def _account(indexing_status: str, last_indexed_at: str = '2026-11-15T12:00:00Z') -> dict: + return {"id": "storage-1", "name": "instruments", "indexing_status": indexing_status, + "last_indexed_at": last_indexed_at} + + +class TestReindexCommand: + """Tests for the storage reindex CLI command, through the real HTTP session.""" + + def setup_method(self): + self.runner = CliRunner(mix_stderr=False) + self.group = Group() + init_storage_commands(self.group) + self._client_patch = patch( + 'dnastack.cli.commands.workbench.storage.commands.get_storage_client', + return_value=StorageClient.make(ServiceEndpoint(id='test', url=_SAMPLE_SERVICE_URL), 'ns-1')) + self.mock_get_client = self._client_patch.start() + + def teardown_method(self): + self._client_patch.stop() + + @responses.activate + def test_starts_reindex_and_prints_the_running_account(self): + responses.post(f'{_ACCOUNT_URL}/reindex', json=_account('RUNNING')) + + result = self.runner.invoke(self.group, ['reindex', 'storage-1', '--namespace', 'ns-1']) + + assert result.exit_code == 0, result.stderr + self.mock_get_client.assert_called_once_with(None, None, 'ns-1') + assert json.loads(result.stdout)["indexing_status"] == "RUNNING" + assert [c.request.method for c in responses.calls] == ['POST'] + + @pytest.mark.parametrize('status', [400, 403, 404]) + @responses.activate + def test_reports_http_status_and_exits_non_zero(self, status): + responses.post(f'{_ACCOUNT_URL}/reindex', status=status, body='rejected') + + result = self.runner.invoke(self.group, ['reindex', 'storage-1']) + + assert result.exit_code == 1 + assert f"HTTP {status}: rejected" in result.stderr + assert result.stdout == "" + + def test_requires_storage_id(self): + result = self.runner.invoke(self.group, ['reindex']) + + assert result.exit_code == 2 + self.mock_get_client.assert_not_called() + + @responses.activate + @patch('dnastack.cli.commands.workbench.storage.commands.sleep') + def test_wait_polls_until_indexing_finishes_and_prints_the_final_account(self, mock_sleep): + responses.get(_ACCOUNT_URL, json=_account('IDLE')) + responses.post(f'{_ACCOUNT_URL}/reindex', json=_account('RUNNING')) + responses.get(_ACCOUNT_URL, json=_account('RUNNING')) + responses.get(_ACCOUNT_URL, json=_account('IDLE', last_indexed_at='2026-11-16T09:30:00Z')) + + result = self.runner.invoke(self.group, ['reindex', 'storage-1', '--wait']) + + assert result.exit_code == 0, result.stderr + assert [c.request.method for c in responses.calls] == ['GET', 'POST', 'GET', 'GET'] + assert mock_sleep.call_count == 2 + output = json.loads(result.stdout) + assert output["indexing_status"] == "IDLE" + assert output["last_indexed_at"] == "2026-11-16T09:30:00Z" + + @responses.activate + @patch('dnastack.cli.commands.workbench.storage.commands.sleep') + def test_wait_gives_up_after_the_timeout_and_exits_non_zero(self, mock_sleep): + responses.get(_ACCOUNT_URL, json=_account('IDLE')) + responses.post(f'{_ACCOUNT_URL}/reindex', json=_account('RUNNING')) + + result = self.runner.invoke(self.group, ['reindex', 'storage-1', '--wait', '--timeout', '0']) + + assert result.exit_code == 1 + assert "still running" in result.stderr + assert result.stdout == "" + mock_sleep.assert_not_called() + + @responses.activate + def test_wait_fails_before_reindexing_when_workbench_does_not_report_indexing_status(self): + responses.get(_ACCOUNT_URL, json={"id": "storage-1"}) + responses.post(f'{_ACCOUNT_URL}/reindex', json={"id": "storage-1"}) + + result = self.runner.invoke(self.group, ['reindex', 'storage-1', '--wait']) + + assert result.exit_code == 1 + assert "indexing status" in result.stderr + assert result.stdout == "" + assert [c.request.method for c in responses.calls] == ['GET'] + + @responses.activate + def test_wait_succeeds_without_polling_when_indexing_finished_before_the_response(self): + responses.get(_ACCOUNT_URL, json=_account('IDLE')) + responses.post(f'{_ACCOUNT_URL}/reindex', json=_account('IDLE', last_indexed_at='2026-11-16T09:30:00Z')) + + result = self.runner.invoke(self.group, ['reindex', 'storage-1', '--wait']) + + assert result.exit_code == 0, result.stderr + assert [c.request.method for c in responses.calls] == ['GET', 'POST'] + assert json.loads(result.stdout)["last_indexed_at"] == "2026-11-16T09:30:00Z" + + @responses.activate + def test_rejects_timeout_without_wait(self): + result = self.runner.invoke(self.group, ['reindex', 'storage-1', '--timeout', '60']) + + assert result.exit_code == 2 + assert len(responses.calls) == 0 + assert "--wait" in result.stderr + + @responses.activate + @patch('dnastack.cli.commands.workbench.storage.commands.sleep') + def test_wait_fails_when_indexing_stops_without_recording_a_completed_run(self, mock_sleep): + responses.get(_ACCOUNT_URL, json=_account('IDLE')) + responses.post(f'{_ACCOUNT_URL}/reindex', json=_account('RUNNING')) + responses.get(_ACCOUNT_URL, json=_account('IDLE')) + + result = self.runner.invoke(self.group, ['reindex', 'storage-1', '--wait']) + + assert result.exit_code == 1 + assert "did not complete" in result.stderr + assert result.stdout == "" + + @responses.activate + def test_wait_fails_on_an_indexing_status_it_does_not_know(self): + responses.get(_ACCOUNT_URL, json=_account('IDLE')) + responses.post(f'{_ACCOUNT_URL}/reindex', json=_account('QUEUED', last_indexed_at='2026-11-16T09:30:00Z')) + + result = self.runner.invoke(self.group, ['reindex', 'storage-1', '--wait']) + + assert result.exit_code == 1 + assert "QUEUED" in result.stderr + assert result.stdout == "" + + @responses.activate + def test_wait_rejects_a_negative_timeout(self): + result = self.runner.invoke(self.group, ['reindex', 'storage-1', '--wait', '--timeout', '-1']) + + assert result.exit_code == 2 + assert len(responses.calls) == 0 + assert "--timeout" in result.stderr diff --git a/tests/unit/client/workbench/storage/__init__.py b/tests/unit/client/workbench/storage/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/client/workbench/storage/test_storage_client.py b/tests/unit/client/workbench/storage/test_storage_client.py new file mode 100644 index 00000000..7c480acf --- /dev/null +++ b/tests/unit/client/workbench/storage/test_storage_client.py @@ -0,0 +1,64 @@ +import pytest +import responses + +from dnastack.client.models import ServiceEndpoint +from dnastack.client.workbench.storage.client import StorageClient +from dnastack.client.workbench.storage.models import StorageAccount + +_SAMPLE_SERVICE_URL = 'https://sample-service.example.com/' + + +def _make_client(): + return StorageClient.make(ServiceEndpoint(id='test', url=_SAMPLE_SERVICE_URL), 'ns-1') + + +class TestReindexStorageAccount: + """Tests for StorageClient.reindex_storage_account""" + + @responses.activate + def test_posts_reindex_without_a_body_and_returns_the_running_account(self): + responses.post(f'{_SAMPLE_SERVICE_URL}ns-1/storage/storage-1/reindex', + json={"id": "storage-1", "name": "instruments", "indexing_status": "RUNNING", + "last_indexed_at": "2026-11-15T12:00:00Z"}) + + account = _make_client().reindex_storage_account('storage-1') + + assert responses.calls[0].request.body is None + assert account.id == "storage-1" + assert account.indexing_status == "RUNNING" + assert account.last_indexed_at == "2026-11-15T12:00:00Z" + + +_ODD_ID = 'a/../b#c?d' +_ODD_SEGMENT = 'a%2F..%2Fb%23c%3Fd' +_ACCOUNT_JSON = {"id": _ODD_ID, "name": "instruments"} + + +class TestStorageAccountIdIsOnePathSegment: + """Every StorageClient method that addresses one account sends its id as a single escaped path segment.""" + + @pytest.mark.parametrize('method, call, suffix', [ + ('GET', lambda client: client.get_storage_account(_ODD_ID), ''), + ('PUT', lambda client: client.update_storage_account(_ODD_ID, StorageAccount(), 'etag-1'), ''), + ('DELETE', lambda client: client.delete_storage_account(_ODD_ID), ''), + ('POST', lambda client: client.reindex_storage_account(_ODD_ID), '/reindex'), + ]) + @responses.activate + def test_escapes_the_id(self, method, call, suffix): + expected_url = f'{_SAMPLE_SERVICE_URL}ns-1/storage/{_ODD_SEGMENT}{suffix}' + responses.add(method, expected_url, json=_ACCOUNT_JSON) + + call(_make_client()) + + assert responses.calls[0].request.url == expected_url + + @pytest.mark.parametrize('storage_account_id', ['', '.', '..']) + @pytest.mark.parametrize('call', [ + lambda client, account_id: client.get_storage_account(account_id), + lambda client, account_id: client.update_storage_account(account_id, StorageAccount(), 'etag-1'), + lambda client, account_id: client.delete_storage_account(account_id), + lambda client, account_id: client.reindex_storage_account(account_id), + ]) + def test_rejects_ids_that_cannot_be_one_path_segment(self, call, storage_account_id): + with pytest.raises(ValueError): + call(_make_client(), storage_account_id)