Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 31cc937

Browse files
author
Praful Makani
authoredJul 30, 2020
docs(samples): add update table cmek (#619)
1 parent ae97b5f commit 31cc937

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
// [START bigquery_update_table_cmek]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.EncryptionConfiguration;
24+
import com.google.cloud.bigquery.Table;
25+
import com.google.cloud.bigquery.TableId;
26+
27+
// Sample to update a cmek table
28+
public class UpdateTableCMEK {
29+
30+
public static void runUpdateTableCMEK() {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String datasetName = "MY_DATASET_NAME";
33+
String tableName = "MY_TABLE_NAME";
34+
String kmsKeyName = "MY_KEY_NAME";
35+
// Set a new encryption key to use for the destination.
36+
// i.e. projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{cryptoKey}
37+
EncryptionConfiguration encryption =
38+
EncryptionConfiguration.newBuilder().setKmsKeyName(kmsKeyName).build();
39+
updateTableCMEK(datasetName, tableName, encryption);
40+
}
41+
42+
public static void updateTableCMEK(
43+
String datasetName, String tableName, EncryptionConfiguration encryption) {
44+
try {
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests.
47+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
48+
49+
Table table = bigquery.getTable(TableId.of(datasetName, tableName));
50+
bigquery.update(table.toBuilder().setEncryptionConfiguration(encryption).build());
51+
System.out.println("Table cmek updated successfully");
52+
} catch (BigQueryException e) {
53+
System.out.println("Table cmek was not updated. \n" + e.toString());
54+
}
55+
}
56+
}
57+
// [END bigquery_update_table_cmek]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.bigquery.EncryptionConfiguration;
23+
import com.google.cloud.bigquery.Field;
24+
import com.google.cloud.bigquery.Schema;
25+
import com.google.cloud.bigquery.StandardSQLTypeName;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.PrintStream;
28+
import java.util.UUID;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
34+
public class UpdateTableCMEKIT {
35+
36+
private String tableName;
37+
private ByteArrayOutputStream bout;
38+
private PrintStream out;
39+
40+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
41+
private static final String BIGQUERY_KMS_KEY_NAME = requireEnvVar("BIGQUERY_KMS_KEY_NAME");
42+
43+
private static String requireEnvVar(String varName) {
44+
String value = System.getenv(varName);
45+
assertNotNull(
46+
"Environment variable " + varName + " is required to perform these tests.",
47+
System.getenv(varName));
48+
return value;
49+
}
50+
51+
@BeforeClass
52+
public static void checkRequirements() {
53+
requireEnvVar("BIGQUERY_DATASET_NAME");
54+
requireEnvVar("BIGQUERY_KMS_KEY_NAME");
55+
}
56+
57+
@Before
58+
public void setUp() {
59+
bout = new ByteArrayOutputStream();
60+
out = new PrintStream(bout);
61+
System.setOut(out);
62+
// create a test table.
63+
tableName = "MY_UPDATE_TABLE_CMEK_TEST" + UUID.randomUUID().toString().substring(0, 8);
64+
Schema schema =
65+
Schema.of(
66+
Field.of("stringField", StandardSQLTypeName.STRING),
67+
Field.of("booleanField", StandardSQLTypeName.BOOL));
68+
EncryptionConfiguration encryption =
69+
EncryptionConfiguration.newBuilder().setKmsKeyName(BIGQUERY_KMS_KEY_NAME).build();
70+
CreateTableCMEK.createTableCMEK(BIGQUERY_DATASET_NAME, tableName, schema, encryption);
71+
72+
bout = new ByteArrayOutputStream();
73+
out = new PrintStream(bout);
74+
System.setOut(out);
75+
}
76+
77+
@After
78+
public void tearDown() {
79+
// Clean up
80+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
81+
System.setOut(null);
82+
}
83+
84+
@Test
85+
public void testUpdateTableCMEK() {
86+
EncryptionConfiguration encryption =
87+
EncryptionConfiguration.newBuilder().setKmsKeyName(BIGQUERY_KMS_KEY_NAME).build();
88+
UpdateTableCMEK.updateTableCMEK(BIGQUERY_DATASET_NAME, tableName, encryption);
89+
assertThat(bout.toString()).contains("Table cmek updated successfully");
90+
}
91+
}

0 commit comments

Comments
 (0)
Failed to load comments.