Skip to content

feat(spanner): Add single-region CMEK database creation sample - #14945

Open
iennae wants to merge 3 commits into
feat/spanner-create-databasefrom
feat/spanner-create-database-with-encryption-key
Open

iennae wants to merge 3 commits into
feat/spanner-create-databasefrom
feat/spanner-create-database-with-encryption-key

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_create_database_with_encryption_key creating a CMEK-protected Spanner database using EncryptionConfig(kms_key_name=...).

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

Regulated workloads protect Spanner databases at rest with a Cloud KMS CryptoKey passed via EncryptionConfig.kms_key_name. Add spanner_create_database_with_encryption_key.
@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 for creating a Spanner database encrypted with a Cloud KMS Customer-Managed Encryption Key (CMEK). The review feedback suggests adding defensive checks when accessing the database's encryption configuration to prevent potential AttributeError exceptions if the returned database object or its encryption configuration is not fully populated.

Comment on lines +74 to +76
configured_key_id = database.encryption_config.kms_key_name.split("/")[-1]
print(f"Resource ID: {database_id}")
print(f"Encryption Key ID: {configured_key_id}")

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

To prevent potential AttributeError exceptions (e.g., if the database object or its encryption_config is not fully populated or is None), use defensive checks before accessing database.encryption_config.kms_key_name.

Suggested change
configured_key_id = database.encryption_config.kms_key_name.split("/")[-1]
print(f"Resource ID: {database_id}")
print(f"Encryption Key ID: {configured_key_id}")
if database and database.encryption_config and database.encryption_config.kms_key_name:
configured_key_id = database.encryption_config.kms_key_name.split("/")[-1]
print(f"Resource ID: {database_id}")
print(f"Encryption Key ID: {configured_key_id}")
else:
print(f"Resource ID: {database_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! Keeping this as is: the field is populated in the responses this sample handles, and the extra branching would distract from what the sample shows.

…_encryption_key

Problem: multi-line SQL and DDL statements were written as adjacent string
fragments. That style is easy to get wrong (a missing trailing space joins
two SQL tokens) and is harder to read than the statement itself, and the
samples in this set mixed it with triple-quoted SQL.

Solution: write each multi-line statement as a single triple-quoted string,
matching the layout already used by the other samples. Only whitespace
between SQL tokens changes; the statements are otherwise identical.

Files updated:
  - spanner/database_admin/src/spanner_database_admin/create_database_with_encryption_key.py
…tion_key

Problem: review of this sample identified correctness and robustness
improvements in the sample and its tests.

Solution: apply the accepted review suggestions and extend the tests to
cover the updated behavior. Details are in the review thread replies.

Files updated:
  - spanner/database_admin/tests/create_database_with_encryption_key_test.py
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