diff --git a/spanner/backups/src/spanner_backups/list_copy_backup_operations.py b/spanner/backups/src/spanner_backups/list_copy_backup_operations.py new file mode 100644 index 0000000000..b9d75335d0 --- /dev/null +++ b/spanner/backups/src/spanner_backups/list_copy_backup_operations.py @@ -0,0 +1,86 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START spanner_list_copy_backup_operations] +import sys + +from google.api_core import exceptions +from google.cloud import spanner_admin_database_v1 + + +def list_copy_backup_operations(project_id: str, instance_id: str) -> None: + """Lists copy-backup long-running operations on a destination Spanner instance. + + Args: + project_id: The Google Cloud project ID. + instance_id: The destination Spanner instance ID. + """ + client = spanner_admin_database_v1.DatabaseAdminClient() + parent = client.instance_path(project_id, instance_id) + request = spanner_admin_database_v1.ListBackupOperationsRequest( + parent=parent, + filter="(metadata.@type:type.googleapis.com/google.spanner.admin.database.v1.CopyBackupMetadata)", + ) + + try: + operations = list(client.list_backup_operations(request=request)) + except exceptions.NotFound as err: + print( + f"error: Spanner instance '{instance_id}' was not found: " f"{err.message}", + file=sys.stderr, + ) + except exceptions.GoogleAPICallError as err: + print( + f"error: failed to list copy-backup operations on instance " + f"'{instance_id}': {err.message}", + file=sys.stderr, + ) + else: + print(f"Resource ID: {instance_id}") + print(f"Copy Backup Operation Count: {len(operations)}") + for op in operations: + meta = spanner_admin_database_v1.CopyBackupMetadata.deserialize( + op.metadata.value + ) + print(f"Destination Backup: {meta.name.split('/')[-1]}") + print(f"Operation Done: {op.done}") + print(f"Source Backup: {meta.source_backup.split('/')[-1]}") + + +# [END spanner_list_copy_backup_operations] + + +if __name__ == "__main__": + import argparse + import os + + parser = argparse.ArgumentParser( + description="List copy-backup operations on a Cloud Spanner instance." + ) + parser.add_argument( + "--project-id", + default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), + help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", + ) + parser.add_argument( + "--instance-id", + default=os.environ.get("SPANNER_INSTANCE_ID", "example-instance"), + help="Spanner instance ID (default: $SPANNER_INSTANCE_ID)", + ) + args = parser.parse_args() + + list_copy_backup_operations( + project_id=args.project_id, + instance_id=args.instance_id, + ) diff --git a/spanner/backups/tests/list_copy_backup_operations_test.py b/spanner/backups/tests/list_copy_backup_operations_test.py new file mode 100644 index 0000000000..ba92e4b3ee --- /dev/null +++ b/spanner/backups/tests/list_copy_backup_operations_test.py @@ -0,0 +1,135 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import runpy +from unittest import mock + +from google.api_core import exceptions +from google.cloud import spanner_admin_database_v1 +from google.longrunning import operations_pb2 +from google.protobuf import any_pb2 +import pytest +from spanner_backups import list_copy_backup_operations + +from . import conftest + +PROJECT_ID = "example-project-id" +INSTANCE_ID = "example-instance" +PARENT = f"projects/{PROJECT_ID}/instances/{INSTANCE_ID}" +ADMIN_CLIENT = "google.cloud.spanner_admin_database_v1.DatabaseAdminClient" + + +def _fake_copy_op() -> operations_pb2.Operation: + meta = spanner_admin_database_v1.CopyBackupMetadata( + name=f"{PARENT}/backups/example-copied", + source_backup=f"{PARENT}/backups/example-source", + ) + packed = any_pb2.Any() + packed.Pack(spanner_admin_database_v1.CopyBackupMetadata.pb(meta)) + return operations_pb2.Operation( + name=f"{PARENT}/backups/example-copied/operations/op-1", + metadata=packed, + done=True, + ) + + +@mock.patch(ADMIN_CLIENT) +def test_list_copy_backup_operations_success( + mock_client_class: mock.MagicMock, capsys: pytest.CaptureFixture[str] +) -> None: + client = mock_client_class.return_value + client.instance_path.return_value = PARENT + client.list_backup_operations.return_value = [_fake_copy_op()] + + list_copy_backup_operations.list_copy_backup_operations(PROJECT_ID, INSTANCE_ID) + + req = client.list_backup_operations.call_args.kwargs["request"] + assert "CopyBackupMetadata" in req.filter + out, err = capsys.readouterr() + assert f"Resource ID: {INSTANCE_ID}" in out + assert PROJECT_ID not in out + assert "Copy Backup Operation Count: 1" in out + assert "Destination Backup: example-copied" in out + assert "Operation Done: True" in out + assert "Source Backup: example-source" in out + assert err == "" + + +@pytest.mark.parametrize( + "error, expected_prefix", + [ + ( + exceptions.NotFound("missing"), + "error: Spanner instance 'example-instance'", + ), + ( + exceptions.PermissionDenied("denied"), + "error: failed to list copy-backup operations", + ), + ], +) +@mock.patch(ADMIN_CLIENT) +def test_list_copy_backup_operations_errors( + mock_client_class: mock.MagicMock, + capsys: pytest.CaptureFixture[str], + error: Exception, + expected_prefix: str, +) -> None: + client = mock_client_class.return_value + client.instance_path.return_value = PARENT + client.list_backup_operations.side_effect = error + + list_copy_backup_operations.list_copy_backup_operations(PROJECT_ID, INSTANCE_ID) + + _out, err = capsys.readouterr() + assert err.startswith(expected_prefix) + assert PROJECT_ID not in err + assert PROJECT_ID not in err + + +@mock.patch(ADMIN_CLIENT) +def test_list_copy_backup_operations_cli_entrypoint( + mock_client_class: mock.MagicMock, + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + client = mock_client_class.return_value + client.instance_path.return_value = PARENT + client.list_backup_operations.return_value = [] + monkeypatch.setattr( + "sys.argv", + [ + "list_copy_backup_operations.py", + "--project-id", + PROJECT_ID, + "--instance-id", + INSTANCE_ID, + ], + ) + + runpy.run_module("spanner_backups.list_copy_backup_operations", run_name="__main__") + + assert "Copy Backup Operation Count: 0" in capsys.readouterr().out + + +@conftest.live_only +def test_list_copy_backup_operations_live(capsys: pytest.CaptureFixture[str]) -> None: + list_copy_backup_operations.list_copy_backup_operations( + conftest.LIVE_PROJECT_ID, + conftest.LIVE_INSTANCE_ID, + ) + + out, err = capsys.readouterr() + assert err == "" + assert "Copy Backup Operation Count:" in out