Skip to content

feat(spanner): Add ListDatabases instance inventory sample - #14950

Open
iennae wants to merge 1 commit into
feat/spanner-create-databasefrom
feat/spanner-list-databases
Open

iennae wants to merge 1 commit into
feat/spanner-create-databasefrom
feat/spanner-list-databases

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_list_databases listing databases and leader options on a Spanner instance.

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

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

Enumerating databases and their leader options on an instance uses DatabaseAdminClient.list_databases. Add a standalone spanner_list_databases sample with unit and live tests.
@iennae
iennae requested review from a team as code owners November 18, 2026 22:18
@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 introduces a new script and associated tests to list Google Cloud Spanner databases and their default leader options on a given instance. The feedback suggests improving the code by letting API exceptions propagate naturally instead of catching and printing them, which enhances reusability. Additionally, it recommends using the client's built-in parse_database_path method for more robust resource name parsing instead of manual string splitting.

Comment on lines +33 to +52
try:
databases = list(client.list_databases(request=request))
except exceptions.NotFound as err:
print(
f"error: instance '{instance_id}' was not found: {err.message}",
file=sys.stderr,
)
except exceptions.GoogleAPICallError as err:
print(
f"error: failed to list databases on instance '{instance_id}': "
f"{err.message}",
file=sys.stderr,
)
else:
print(f"Resource ID: {instance_id}")
print(f"Database Count: {len(databases)}")
for db in databases:
db_id = db.name.split("/")[-1]
leader = db.default_leader or "regional-default"
print(f"Database ID: {db_id}, Default Leader: {leader}")

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

Improvement: Simplify Exception Handling and Use Robust Path Parsing

  1. Exception Propagation: Swallowing exceptions and printing to sys.stderr makes this function less reusable. In Python, helper/library functions should let exceptions propagate so that calling applications can handle them programmatically (e.g., for retries or custom error handling). Removing the try-except block also significantly reduces boilerplate, keeping the sample focused on the Spanner API.
  2. Robust Path Parsing: Instead of manually splitting the database resource name with db.name.split("/")[-1], use the client's built-in parse_database_path helper, which is more robust and idiomatic.

Note: If you apply this change, remember to update test_list_databases_errors in tests/list_databases_test.py to use pytest.raises to assert that the expected exceptions are raised.

    databases = list(client.list_databases(request=request))
    print(f"Resource ID: {instance_id}")
    print(f"Database Count: {len(databases)}")
    for db in databases:
        db_id = client.parse_database_path(db.name)["database"]
        leader = db.default_leader or "regional-default"
        print(f"Database ID: {db_id}, Default Leader: {leader}")

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. The resource ID is taken from the last path segment, as in the other samples in this set.

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