Skip to content

feat(spanner): Add FGAC database_role query sample - #14961

Open
iennae wants to merge 1 commit into
feat/spanner-create-databasefrom
feat/spanner-read-data-with-database-role
Open

iennae wants to merge 1 commit into
feat/spanner-create-databasefrom
feat/spanner-read-data-with-database-role

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_read_data_with_database_role querying Singers using a Database configured with database_role.

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

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.
@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 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).

Comment on lines +22 to +24
def read_data_with_database_role(
project_id: str, instance_id: str, database_id: str
) -> None:

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

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.

Suggested change
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:

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 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.

Comment on lines +75 to +79
parser.add_argument(
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"),
help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)",
)

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

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).

Suggested change
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
  1. When using argparse for a command-line script, the --project_id argument can be optional, as the project can be inferred from Application Default Credentials (ADC) if not provided.

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 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.

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