Skip to content

feat(spanner): Add multi-region CMEK RestoreDatabase sample - #14979

Open
iennae wants to merge 2 commits into
feat/spanner-copy-backupfrom
feat/spanner-restore-backup-with-mr-cmek
Open

iennae wants to merge 2 commits into
feat/spanner-copy-backupfrom
feat/spanner-restore-backup-with-mr-cmek

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_restore_backup_with_mr_cmek restoring a backup into a multi-region CMEK Spanner database.

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

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

Restoring a Spanner backup into a multi-region CMEK database passes regional KMS keys via RestoreDatabaseEncryptionConfig.kms_key_names. Add spanner_restore_backup_with_mr_cmek.
@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 and associated tests for restoring a Spanner backup into a multi-region CMEK database. The implementation includes CLI entry points and comprehensive unit and live integration tests. The feedback recommends safely retrieving environment variables in the live integration test using os.environ.get and skipping the test if they are missing, preventing potential KeyError exceptions in environments where these variables are not configured.

Comment on lines +130 to +132
mr_inst = os.environ["SPANNER_MR_INSTANCE_ID"]
mr_bkp = os.environ["SPANNER_MR_BACKUP_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

The live integration test directly accesses os.environ for multi-region CMEK variables (SPANNER_MR_INSTANCE_ID, SPANNER_MR_BACKUP_ID, SPANNER_MR_KMS_KEY_NAMES). If these are not set (which is common in standard/single-region test environments), the test will fail with a KeyError and block the test suite. It is safer to check for their presence and skip the test if they are missing.

    mr_inst = os.environ.get("SPANNER_MR_INSTANCE_ID")
    mr_bkp = os.environ.get("SPANNER_MR_BACKUP_ID")
    mr_keys_env = os.environ.get("SPANNER_MR_KMS_KEY_NAMES")
    if not mr_inst or not mr_bkp or not mr_keys_env:
        pytest.skip(
            "SPANNER_MR_INSTANCE_ID, SPANNER_MR_BACKUP_ID, and "
            "SPANNER_MR_KMS_KEY_NAMES environment variables must be set "
            "to run this live test."
        )
    mr_keys = [k for k in mr_keys_env.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 (99ce713).

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/backups/tests/restore_backup_with_mr_cmek_test.py
@angelcaamal angelcaamal self-assigned this Nov 19, 2026
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.

2 participants