Skip to content

feat(spanner): Add ListInstanceConfigs regional/multi-region sample - #14969

Open
iennae wants to merge 1 commit into
feat/spanner-create-instance-asymmetric-autoscalingfrom
feat/spanner-list-instance-configs
Open

iennae wants to merge 1 commit into
feat/spanner-create-instance-asymmetric-autoscalingfrom
feat/spanner-list-instance-configs

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_list_instance_configs enumerating available Spanner instance configurations.

Note: Stacked on #14853 (feat/spanner-create-instance-asymmetric-autoscaling) so this PR shows only this sample's diff. It will merge into main after #14853.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. Unit and live integration tests pass on Python 3.13.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

Discovering available regional and multi-region Spanner topologies calls InstanceAdminClient.list_instance_configs. Add a standalone spanner_list_instance_configs sample.
@iennae
iennae requested review from a team as code owners November 18, 2026 22:19
@snippet-bot

snippet-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Nov 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +33 to +51
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)}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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)}")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +45 to +71
@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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Since the sample keeps its explicit exception handling and stderr reporting, this test keeps its capsys stderr assertions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant