Skip to content

Commit b5174fe

Browse files
committed
docs: add Multi Region Encryption samples
1 parent f121ca9 commit b5174fe

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2024 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.spanner;
18+
19+
// [START spanner_create_database_with_MR_CMEK]
20+
21+
import com.google.cloud.spanner.Spanner;
22+
import com.google.cloud.spanner.SpannerExceptionFactory;
23+
import com.google.cloud.spanner.SpannerOptions;
24+
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
25+
import com.google.common.collect.ImmutableList;
26+
import com.google.spanner.admin.database.v1.CreateDatabaseRequest;
27+
import com.google.spanner.admin.database.v1.Database;
28+
import com.google.spanner.admin.database.v1.EncryptionConfig;
29+
import com.google.spanner.admin.database.v1.InstanceName;
30+
import java.util.concurrent.ExecutionException;
31+
import java.util.concurrent.TimeUnit;
32+
import java.util.concurrent.TimeoutException;
33+
34+
public class CreateDatabaseWithMultiRegionEncryptionKey {
35+
36+
static void createDatabaseWithEncryptionKey() {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String projectId = "my-project";
39+
String instanceId = "my-instance";
40+
String databaseId = "my-database";
41+
String[] kmsKeyNames =
42+
new String[] {
43+
"projects/" + projectId + "/locations/<location1>/keyRings/<keyRing>/cryptoKeys/<keyId>",
44+
"projects/" + projectId + "/locations/<location2>/keyRings/<keyRing>/cryptoKeys/<keyId>",
45+
"projects/" + projectId + "/locations/<location3>/keyRings/<keyRing>/cryptoKeys/<keyId>"
46+
};
47+
try (Spanner spanner =
48+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
49+
DatabaseAdminClient adminClient = spanner.createDatabaseAdminClient()) {
50+
createDatabaseWithMultiRegionEncryptionKey(
51+
adminClient, projectId, instanceId, databaseId, kmsKeyNames);
52+
}
53+
}
54+
55+
static void createDatabaseWithMultiRegionEncryptionKey(
56+
DatabaseAdminClient adminClient,
57+
String projectId,
58+
String instanceId,
59+
String databaseId,
60+
String[] kmsKeyNames) {
61+
InstanceName instanceName = InstanceName.of(projectId, instanceId);
62+
CreateDatabaseRequest request =
63+
CreateDatabaseRequest.newBuilder()
64+
.setParent(instanceName.toString())
65+
.setCreateStatement("CREATE DATABASE `" + databaseId + "`")
66+
.setEncryptionConfig(
67+
EncryptionConfig.newBuilder()
68+
.addAllKmsKeyNames(ImmutableList.copyOf(kmsKeyNames))
69+
.build())
70+
.addAllExtraStatements(
71+
ImmutableList.of(
72+
"CREATE TABLE Singers ("
73+
+ " SingerId INT64 NOT NULL,"
74+
+ " FirstName STRING(1024),"
75+
+ " LastName STRING(1024),"
76+
+ " SingerInfo BYTES(MAX)"
77+
+ ") PRIMARY KEY (SingerId)",
78+
"CREATE TABLE Albums ("
79+
+ " SingerId INT64 NOT NULL,"
80+
+ " AlbumId INT64 NOT NULL,"
81+
+ " AlbumTitle STRING(MAX)"
82+
+ ") PRIMARY KEY (SingerId, AlbumId),"
83+
+ " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"))
84+
.build();
85+
try {
86+
System.out.println("Waiting for operation to complete...");
87+
Database createdDatabase =
88+
adminClient.createDatabaseAsync(request).get(120, TimeUnit.SECONDS);
89+
90+
System.out.printf(
91+
"Database %s created with encryption keys %s%n",
92+
createdDatabase.getName(), createdDatabase.getEncryptionConfig().getKmsKeyNamesList());
93+
} catch (ExecutionException e) {
94+
// If the operation failed during execution, expose the cause.
95+
throw SpannerExceptionFactory.asSpannerException(e.getCause());
96+
} catch (InterruptedException e) {
97+
// Throw when a thread is waiting, sleeping, or otherwise occupied,
98+
// and the thread is interrupted, either before or during the activity.
99+
throw SpannerExceptionFactory.propagateInterrupt(e);
100+
} catch (TimeoutException e) {
101+
// If the operation timed out propagates the timeout
102+
throw SpannerExceptionFactory.propagateTimeout(e);
103+
}
104+
}
105+
}
106+
// [END spanner_create_database_with_MR_CMEK]

0 commit comments

Comments
 (0)