Skip to content

Commit 57740e4

Browse files
Mattix23gcf-owl-bot[bot]parthea
authored
docs: revise create table cmek sample (#1452)
* docs: revise create table cmek sample * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 14ae1f2 commit 57740e4

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

docs/snippets.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_create_table_cmek(client, to_delete):
168168
dataset = bigquery.Dataset(dataset_ref)
169169
client.create_dataset(dataset)
170170
to_delete.append(dataset)
171-
171+
# TODO(Mattix23): When sample is updated in cloud.google.com, delete this one.
172172
# [START bigquery_create_table_cmek]
173173
# from google.cloud import bigquery
174174
# client = bigquery.Client()

samples/snippets/create_table_cmek.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def create_table_cmek(table_id: str, kms_key_name: str) -> None:
17+
orig_table_id = table_id
18+
orig_key_name = kms_key_name
19+
# [START bigquery_create_table_cmek]
20+
from google.cloud import bigquery
21+
22+
client = bigquery.Client()
23+
24+
# TODO(dev): Change table_id to the full name of the table you want to create.
25+
table_id = "your-project.your_dataset.your_table_name"
26+
27+
# Set the encryption key to use for the table.
28+
# TODO: Replace this key with a key you have created in Cloud KMS.
29+
kms_key_name = "projects/your-project/locations/us/keyRings/test/cryptoKeys/test"
30+
31+
# [END bigquery_create_table_cmek]
32+
33+
table_id = orig_table_id
34+
kms_key_name = orig_key_name
35+
36+
# [START bigquery_create_table_cmek]
37+
table = bigquery.Table(table_id)
38+
table.encryption_configuration = bigquery.EncryptionConfiguration(
39+
kms_key_name=kms_key_name
40+
)
41+
table = client.create_table(table) # API request
42+
43+
print(f"Created {table_id}.")
44+
print(f"Key: {table.encryption_configuration.kms_key_name}.")
45+
46+
# [END bigquery_create_table_cmek]
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import typing
16+
17+
import create_table_cmek
18+
19+
if typing.TYPE_CHECKING:
20+
import pytest
21+
22+
23+
def test_create_table(
24+
capsys: "pytest.CaptureFixture[str]",
25+
random_table_id: str,
26+
) -> None:
27+
28+
kms_key_name = (
29+
"projects/cloud-samples-tests/locations/us/keyRings/test/cryptoKeys/test"
30+
)
31+
32+
create_table_cmek.create_table_cmek(random_table_id, kms_key_name)
33+
34+
out, _ = capsys.readouterr()
35+
assert "Created" in out
36+
assert random_table_id in out
37+
assert kms_key_name in out

0 commit comments

Comments
 (0)