Repository navigation
Conversation
Discovering available regional and multi-region Spanner topologies calls InstanceAdminClient.list_instance_configs. Add a standalone spanner_list_instance_configs 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 adds a new script list_instance_configs.py to list Google Cloud Spanner instance configurations, along with corresponding unit and integration tests. The reviewer suggests letting API exceptions propagate naturally instead of catching and printing them to standard error, which ensures correct exit codes and better debugging. Consequently, the unit tests should be updated to assert that these exceptions are raised using pytest.raises.
| try: | ||
| configs = list(client.list_instance_configs(request=request)) | ||
| except exceptions.PermissionDenied as err: | ||
| print( | ||
| f"error: permission denied listing Spanner instance configs: " | ||
| f"{err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| except exceptions.GoogleAPICallError as err: | ||
| print( | ||
| f"error: failed to list Spanner instance configs: {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| else: | ||
| for cfg in configs[:5]: | ||
| cfg_id = cfg.name.split("/")[-1] | ||
| print(f"Resource ID: {cfg_id}") | ||
| print(f"Leader Options: {list(cfg.leader_options)}") | ||
| print(f"Total Instance Configs: {len(configs)}") |
There was a problem hiding this comment.
Swallowing API exceptions and printing them to sys.stderr prevents calling code from detecting failures programmatically, hides the stack trace (making debugging harder), and causes the CLI script to exit with a success status code (0) even when the operation fails.
It is highly recommended to let the exceptions propagate naturally. This simplifies the sample code, ensures correct non-zero exit codes on failure, and provides a full traceback for debugging.
configs = list(client.list_instance_configs(request=request))
for cfg in configs[:5]:
cfg_id = cfg.name.split("/")[-1]
print(f"Resource ID: {cfg_id}")
print(f"Leader Options: {list(cfg.leader_options)}")
print(f"Total Instance Configs: {len(configs)}")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.PermissionDenied("denied"), | ||
| "error: permission denied listing Spanner instance configs", | ||
| ), | ||
| ( | ||
| exceptions.ServiceUnavailable("down"), | ||
| "error: failed to list Spanner instance configs", | ||
| ), | ||
| ], | ||
| ) | ||
| @mock.patch(ADMIN_CLIENT) | ||
| def test_list_instance_configs_errors( | ||
| mock_client_class: mock.MagicMock, | ||
| capsys: pytest.CaptureFixture[str], | ||
| error: Exception, | ||
| expected_prefix: str, | ||
| ) -> None: | ||
| mock_client_class.return_value.list_instance_configs.side_effect = error | ||
|
|
||
| list_instance_configs.list_instance_configs(PROJECT_ID) | ||
|
|
||
| _out, err = capsys.readouterr() | ||
| assert err.startswith(expected_prefix) | ||
| assert PROJECT_ID not in err |
There was a problem hiding this comment.
Since we should let the API exceptions propagate rather than swallowing them, the error tests should be updated to assert that the expected exceptions are raised using pytest.raises.
@pytest.mark.parametrize(
"error",
[
exceptions.PermissionDenied("denied"),
exceptions.ServiceUnavailable("down"),
],
)
@mock.patch(ADMIN_CLIENT)
def test_list_instance_configs_errors(
mock_client_class: mock.MagicMock,
error: Exception,
) -> None:
mock_client_class.return_value.list_instance_configs.side_effect = error
with pytest.raises(type(error)):
list_instance_configs.list_instance_configs(PROJECT_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_list_instance_configsenumerating available Spanner instance configurations.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions