Skip to content

feat(spanner): Add multi-region CMEK database creation sample - #14946

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

iennae wants to merge 3 commits into
feat/spanner-create-databasefrom
feat/spanner-create-database-with-mr-cmek

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_create_database_with_mr_cmek creating a multi-region Spanner database protected by regional Cloud KMS keys.

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

Multi-region CMEK Spanner databases configure per-region Cloud KMS keys via EncryptionConfig.kms_key_names. Add spanner_create_database_with_mr_cmek with unit and live integration 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 to create a multi-region Spanner database protected by regional KMS keys (CMEK), along with its corresponding unit and integration tests. The review feedback suggests improving the live test robustness by gracefully skipping the test if required environment variables are missing, and dynamically asserting the number of configured KMS keys instead of hardcoding the value.

Comment on lines +134 to +135
mr_instance = os.environ["SPANNER_MR_INSTANCE_ID"]
mr_keys = [k for k in os.environ["SPANNER_MR_KMS_KEY_NAMES"].split(",") if k]

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

If SPANNER_MR_INSTANCE_ID or SPANNER_MR_KMS_KEY_NAMES are not set in the environment, this test will fail with a KeyError. Since these are not checked in conftest.py, it is safer to retrieve them using os.environ.get() and skip the test if they are missing.

    mr_instance = os.environ.get("SPANNER_MR_INSTANCE_ID")
    mr_keys_raw = os.environ.get("SPANNER_MR_KMS_KEY_NAMES")
    if not mr_instance or not mr_keys_raw:
        pytest.skip(
            "SPANNER_MR_INSTANCE_ID and SPANNER_MR_KMS_KEY_NAMES environment "
            "variables are required for this test."
        )
    mr_keys = [k for k in mr_keys_raw.split(",") if k]

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.

Good catch, done: the live test now checks for its required environment variables up front and skips with a clear message when one is missing, instead of failing with a KeyError (c4de0f2).

out, err = capsys.readouterr()
assert err == ""
assert conftest.LIVE_PROJECT_ID not in out
assert "Configured KMS Keys: 3" in out

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

The assertion hardcodes 3 KMS keys, but the live test uses the keys from the SPANNER_MR_KMS_KEY_NAMES environment variable, which may contain a different number of keys depending on the test environment. Use len(mr_keys) to make the assertion dynamic and robust.

Suggested change
assert "Configured KMS Keys: 3" in out
assert f"Configured KMS Keys: {len(mr_keys)}" in out

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.

Done: the assertion now uses the number of keys supplied to the test (c4de0f2).

…_mr_cmek

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_mr_cmek.py
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_mr_cmek_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