Repository navigation
Conversation
Applications enforcing least-privilege SQL access open a Database handle scoped to a specific database_role. Add a standalone spanner_read_data_with_database_role sample querying Singers.
|
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 script read_data_with_database_role.py along with its unit and live tests to query Spanner data using a fine-grained database role. The review feedback suggests making the project_id parameter and CLI argument optional (defaulting to None or os.environ.get("GOOGLE_CLOUD_PROJECT") instead of a hardcoded string) to allow the Spanner client to automatically infer the project ID from Application Default Credentials (ADC).
| def read_data_with_database_role( | ||
| project_id: str, instance_id: str, database_id: str | ||
| ) -> None: |
There was a problem hiding this comment.
Update the project_id type annotation to str | None to allow it to be optional. This allows the client to fall back to Application Default Credentials (ADC) for project detection when no project ID is explicitly provided.
| def read_data_with_database_role( | |
| project_id: str, instance_id: str, database_id: str | |
| ) -> None: | |
| def read_data_with_database_role( | |
| project_id: str | None, instance_id: str, database_id: str | |
| ) -> None: |
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
| parser.add_argument( | ||
| "--project-id", | ||
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | ||
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | ||
| ) |
There was a problem hiding this comment.
Make the --project-id argument truly optional by defaulting to os.environ.get("GOOGLE_CLOUD_PROJECT") instead of a hardcoded dummy string like "example-project-id". If neither the argument nor the environment variable is set, it will default to None, allowing the Spanner client to automatically infer the project ID from Application Default Credentials (ADC).
| 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( | |
| "--project-id", | |
| default=os.environ.get("GOOGLE_CLOUD_PROJECT"), | |
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | |
| ) |
References
- When using
argparsefor a command-line script, the--project_idargument can be optional, as the project can be inferred from Application Default Credentials (ADC) if not provided.
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
Problem: reviewers of this sample set asked for stricter and clearer tests and for a single source of truth for dependencies. Mocks were created without a spec, so a renamed client argument would pass silently; the mocked wait timeout was raised from the API call instead of from the operation's result; live tests did not say which environment variables they need; and some tests carried leftover or repeated lines. Package dependencies were pinned in both pyproject.toml and requirements.txt. Solution: where applicable to the files below, patch client classes with autospec=True, raise the mocked TimeoutError from operation.result(), document the required environment variables above each live test and skip when one is missing, remove unused or duplicated test lines, and join string literals that the formatter had left split on one line. For package scaffolding, read dependencies from requirements.txt and update the README install command to match. Files updated: - spanner/database_admin/tests/read_data_with_database_role_test.py
Description
Adds
spanner_read_data_with_database_rolequeryingSingersusing aDatabaseconfigured withdatabase_role.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions