Repository navigation
feat(spanner): Add DeleteBackupSchedule cleanup sample - #14987
Conversation
Removing an automated backup schedule from a Spanner database calls DatabaseAdminClient.delete_backup_schedule. Add a standalone spanner_delete_backup_schedule sample.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new module and tests for deleting a Cloud Spanner backup schedule. The review feedback suggests improving error handling by allowing API exceptions to propagate naturally from the delete_backup_schedule function instead of swallowing them, and updating the corresponding unit tests to assert that these exceptions are raised.
| try: | ||
| client.delete_backup_schedule(request=request) | ||
| except exceptions.NotFound as err: | ||
| print( | ||
| f"error: backup schedule '{schedule_id}' on database " | ||
| f"'{database_id}' in instance '{instance_id}' was not found: " | ||
| f"{err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| except exceptions.GoogleAPICallError as err: | ||
| print( | ||
| f"error: failed to delete backup schedule '{schedule_id}' on " | ||
| f"database '{database_id}': {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| else: | ||
| print(f"Resource ID: {schedule_id}") | ||
| print(f"Deleted Schedule: {schedule_id}") |
There was a problem hiding this comment.
Catching and swallowing API exceptions (exceptions.NotFound and exceptions.GoogleAPICallError) inside the sample function makes it difficult for programmatic callers to detect and handle failures. If this function is integrated into a larger application, failures will go unnoticed because the function silently returns None. Additionally, when run as a CLI script, the process will exit with a status code of 0 (success) even if the deletion failed.
It is more idiomatic for sample functions to let exceptions propagate naturally to the caller. If you want to avoid raw tracebacks for CLI users, you can catch the exceptions in the __main__ block and exit with a non-zero status code (e.g., sys.exit(1)).
Note: If you apply this change, you will also need to update test_delete_backup_schedule_errors in delete_backup_schedule_test.py to use pytest.raises instead of asserting on capsys.
client.delete_backup_schedule(request=request)
print(f"Resource ID: {schedule_id}")
print(f"Deleted Schedule: {schedule_id}")References
- For code samples, prioritize consistency with existing samples in the repository over introducing specific library exception classes (like 'google.api_core.exceptions.GoogleAPIError') if they are not already commonly used.
There was a problem hiding this comment.
Thanks for the feedback! Standalone documentation snippets here intentionally catch google.api_core.exceptions inside the sample function and print actionable diagnostic messages to stderr so readers can see concrete error handling directly in context, so we are keeping the try/except block.
| @pytest.mark.parametrize( | ||
| "error, expected_prefix", | ||
| [ | ||
| ( | ||
| exceptions.NotFound("missing"), | ||
| "error: backup schedule 'example-backup-schedule'", | ||
| ), | ||
| ( | ||
| exceptions.PermissionDenied("denied"), | ||
| "error: failed to delete backup schedule", | ||
| ), | ||
| ], | ||
| ) | ||
| @mock.patch(ADMIN_CLIENT) | ||
| def test_delete_backup_schedule_errors( | ||
| mock_client_class: mock.MagicMock, | ||
| capsys: pytest.CaptureFixture[str], | ||
| error: Exception, | ||
| expected_prefix: str, | ||
| ) -> None: | ||
| _setup_admin(mock_client_class) | ||
| mock_client_class.return_value.delete_backup_schedule.side_effect = error | ||
|
|
||
| delete_backup_schedule.delete_backup_schedule( | ||
| PROJECT_ID, INSTANCE_ID, DATABASE_ID, SCHEDULE_ID | ||
| ) | ||
|
|
||
| _out, err = capsys.readouterr() | ||
| assert err.startswith(expected_prefix) | ||
| assert PROJECT_ID not in err |
There was a problem hiding this comment.
If the delete_backup_schedule function is updated to propagate exceptions rather than swallowing them, this test should be updated to assert that the expected exceptions are raised using pytest.raises.
@pytest.mark.parametrize(
"error",
[
exceptions.NotFound("missing"),
exceptions.PermissionDenied("denied"),
],
)
@mock.patch(ADMIN_CLIENT)
def test_delete_backup_schedule_errors(
mock_client_class: mock.MagicMock,
error: Exception,
) -> None:
_setup_admin(mock_client_class)
mock_client_class.return_value.delete_backup_schedule.side_effect = error
with pytest.raises(type(error)):
delete_backup_schedule.delete_backup_schedule(
PROJECT_ID, INSTANCE_ID, DATABASE_ID, SCHEDULE_ID
)There was a problem hiding this comment.
Thanks! Since the sample keeps its explicit exception handling and stderr reporting, this test keeps its capsys stderr assertions.
Description
Adds
spanner_delete_backup_scheduledeleting a backup schedule from a Spanner database.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions