Skip to content

Commit 99367f2

Browse files
committed
rebase branch with main
1 parent 4ec3c25 commit 99367f2

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

samples/samples/admin/backup_sample.py renamed to samples/samples/admin/backup_snippet.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
For more information, see the README.rst under /spanner.
1818
"""
1919

20-
from datetime import datetime, timedelta
2120
import time
21+
from datetime import datetime, timedelta
2222

2323
from google.cloud import spanner
2424
from google.cloud.spanner_admin_database_v1.types import backup as backup_pb
@@ -85,8 +85,8 @@ def create_backup_with_encryption_key(
8585
backup=backup_pb.Backup(
8686
database=database.name,
8787
expire_time=expire_time,
88-
encryption_config=encryption_config,
8988
),
89+
encryption_config=encryption_config,
9090
)
9191
operation = spanner_client.database_admin_api.create_backup(request)
9292

@@ -146,7 +146,10 @@ def restore_database_with_encryption_key(
146146
instance_id, new_database_id, backup_id, kms_key_name
147147
):
148148
"""Restores a database from a backup using a Customer Managed Encryption Key (CMEK)."""
149-
from google.cloud.spanner_admin_database_v1 import RestoreDatabaseEncryptionConfig
149+
from google.cloud.spanner_admin_database_v1 import (
150+
RestoreDatabaseEncryptionConfig,
151+
RestoreDatabaseRequest,
152+
)
150153

151154
spanner_client = spanner.Client()
152155
instance = spanner_client.instance(instance_id)
@@ -168,6 +171,20 @@ def restore_database_with_encryption_key(
168171
# Newly created database has restore information.
169172
new_database.reload()
170173
restore_info = new_database.restore_info
174+
175+
request = RestoreDatabaseRequest(
176+
parent=instance.name,
177+
database_id=new_database_id,
178+
backup="{}/backups/{}".format(instance.name, backup_id),
179+
encryption_config=encryption_config,
180+
)
181+
operation = spanner_client.database_admin_api.restore_database(request)
182+
183+
# Wait for restore operation to complete.
184+
db = operation.result(1600)
185+
186+
# Newly created database has restore information.
187+
restore_info = db.restore_info
171188
print(
172189
"Database {} restored to {} from backup {} with using encryption key {}.".format(
173190
restore_info.backup_info.source_database,
@@ -458,4 +475,4 @@ def copy_backup(instance_id, backup_id, source_backup_path):
458475
)
459476

460477

461-
# [END spanner_copy_backup
478+
# [END spanner_copy_backup]

samples/samples/admin/backup_sample_test.py renamed to samples/samples/admin/backup_snippet_test.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
# limitations under the License.
1414
import uuid
1515

16-
from google.api_core.exceptions import DeadlineExceeded
16+
import backup_snippet
1717
import pytest
18-
19-
import backup_sample
18+
from google.api_core.exceptions import DeadlineExceeded
2019
from test_utils.retry import RetryErrors
2120

2221

@@ -50,7 +49,7 @@ def test_create_backup(capsys, instance_id, sample_database):
5049
results = snapshot.execute_sql("SELECT CURRENT_TIMESTAMP()")
5150
version_time = list(results)[0][0]
5251

53-
backup_sample.create_backup(
52+
backup_snippet.create_backup(
5453
instance_id,
5554
sample_database.database_id,
5655
BACKUP_ID,
@@ -67,7 +66,7 @@ def test_create_backup_with_encryption_key(
6766
sample_database,
6867
kms_key_name,
6968
):
70-
backup_sample.create_backup_with_encryption_key(
69+
backup_snippet.create_backup_with_encryption_key(
7170
instance_id,
7271
sample_database.database_id,
7372
CMEK_BACKUP_ID,
@@ -81,8 +80,26 @@ def test_create_backup_with_encryption_key(
8180
# @pytest.mark.dependency(depends=["create_backup"])
8281
@RetryErrors(exception=DeadlineExceeded, max_tries=2)
8382
def test_restore_database(capsys, instance_id, sample_database):
84-
backup_sample.restore_database(instance_id, RESTORE_DB_ID, BACKUP_ID)
83+
backup_snippet.restore_database(instance_id, RESTORE_DB_ID, BACKUP_ID)
8584
out, _ = capsys.readouterr()
8685
assert (sample_database.database_id + " restored to ") in out
8786
assert (RESTORE_DB_ID + " from backup ") in out
8887
assert BACKUP_ID in out
88+
89+
90+
@pytest.mark.dependency(depends=["create_backup_with_encryption_key"])
91+
@RetryErrors(exception=DeadlineExceeded, max_tries=2)
92+
def test_restore_database_with_encryption_key(
93+
capsys,
94+
instance_id,
95+
sample_database,
96+
kms_key_name,
97+
):
98+
backup_snippet.restore_database_with_encryption_key(
99+
instance_id, CMEK_RESTORE_DB_ID, CMEK_BACKUP_ID, kms_key_name
100+
)
101+
out, _ = capsys.readouterr()
102+
assert (sample_database.database_id + " restored to ") in out
103+
assert (CMEK_RESTORE_DB_ID + " from backup ") in out
104+
assert CMEK_BACKUP_ID in out
105+
assert kms_key_name in out

samples/samples/admin/pg_samples.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
Spanner PostgreSql dialect.
1919
For more information, see the README.rst under /spanner.
2020
"""
21-
import base64
22-
import decimal
23-
2421
from google.cloud import spanner
2522
from google.cloud.spanner_admin_database_v1.types import spanner_database_admin
2623

samples/samples/admin/pg_samples_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414

1515
import uuid
1616

17+
import pg_samples as samples
18+
import pytest
1719
from google.api_core import exceptions
1820
from google.cloud.spanner_admin_database_v1.types.common import DatabaseDialect
19-
import pytest
2021
from test_utils.retry import RetryErrors
2122

22-
import pg_samples as samples
23-
2423
CREATE_TABLE_SINGERS = """\
2524
CREATE TABLE Singers (
2625
SingerId BIGINT NOT NULL,

samples/samples/admin/samples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import time
2323

2424
from google.cloud import spanner
25-
from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin
2625
from google.cloud.spanner_admin_database_v1.types import spanner_database_admin
26+
from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin
2727

2828
OPERATION_TIMEOUT_SECONDS = 240
2929

samples/samples/admin/samples_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
import uuid
2323

24+
import pytest
2425
from google.api_core import exceptions
2526
from google.cloud.spanner_admin_database_v1.types.common import DatabaseDialect
26-
import pytest
2727
from test_utils.retry import RetryErrors
2828

2929
import samples

0 commit comments

Comments
 (0)