Skip to content

Commit 833c572

Browse files
authored
samples: add support for CMMR Phase 2 (#2085)
The following samples and corresponding tests have been added: - CreateInstanceConfig - UpdateInstanceConfig - DeleteInstanceConfig - ListInstanceConfigOperations
1 parent cec6cfd commit 833c572

File tree

8 files changed

+375
-23
lines changed

8 files changed

+375
-23
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,16 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/
260260
| Create Database With Default Leader Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateDatabaseWithDefaultLeaderSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateDatabaseWithDefaultLeaderSample.java) |
261261
| Create Database With Encryption Key | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateDatabaseWithEncryptionKey.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateDatabaseWithEncryptionKey.java) |
262262
| Create Database With Version Retention Period Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateDatabaseWithVersionRetentionPeriodSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateDatabaseWithVersionRetentionPeriodSample.java) |
263+
| Create Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateInstanceConfigSample.java) |
263264
| Create Instance Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateInstanceExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateInstanceExample.java) |
264265
| Create Instance With Processing Units Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateInstanceWithProcessingUnitsExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateInstanceWithProcessingUnitsExample.java) |
265266
| Custom Timeout And Retry Settings Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CustomTimeoutAndRetrySettingsExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CustomTimeoutAndRetrySettingsExample.java) |
267+
| Delete Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DeleteInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DeleteInstanceConfigSample.java) |
266268
| Get Commit Stats Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/GetCommitStatsSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/GetCommitStatsSample.java) |
267269
| Get Database Ddl Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/GetDatabaseDdlSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/GetDatabaseDdlSample.java) |
268270
| Get Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/GetInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/GetInstanceConfigSample.java) |
269271
| List Databases Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/ListDatabasesSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/ListDatabasesSample.java) |
272+
| List Instance Config Operations Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/ListInstanceConfigOperationsSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/ListInstanceConfigOperationsSample.java) |
270273
| List Instance Configs Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/ListInstanceConfigsSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/ListInstanceConfigsSample.java) |
271274
| Pg Async Query To List Async Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/PgAsyncQueryToListAsyncExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/PgAsyncQueryToListAsyncExample.java) |
272275
| Pg Async Runner Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/PgAsyncRunnerExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/PgAsyncRunnerExample.java) |
@@ -287,6 +290,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/
287290
| Tag Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/TagSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/TagSample.java) |
288291
| Tracing Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/TracingSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/TracingSample.java) |
289292
| Update Database With Default Leader Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/UpdateDatabaseWithDefaultLeaderSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/UpdateDatabaseWithDefaultLeaderSample.java) |
293+
| Update Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/UpdateInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/UpdateInstanceConfigSample.java) |
290294
| Update Json Data Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/UpdateJsonDataSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/UpdateJsonDataSample.java) |
291295
| Update Numeric Data Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/UpdateNumericDataSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/UpdateNumericDataSample.java) |
292296

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2022 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_instance_config]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.spanner.InstanceAdminClient;
22+
import com.google.cloud.spanner.InstanceConfig;
23+
import com.google.cloud.spanner.InstanceConfigId;
24+
import com.google.cloud.spanner.InstanceConfigInfo;
25+
import com.google.cloud.spanner.ReplicaInfo;
26+
import com.google.cloud.spanner.Spanner;
27+
import com.google.cloud.spanner.SpannerOptions;
28+
import com.google.common.collect.ImmutableList;
29+
import com.google.spanner.admin.instance.v1.CreateInstanceConfigMetadata;
30+
import java.util.List;
31+
import java.util.concurrent.ExecutionException;
32+
import java.util.concurrent.TimeUnit;
33+
import java.util.concurrent.TimeoutException;
34+
35+
class CreateInstanceConfigSample {
36+
static void createInstanceConfig() {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String projectId = "my-project";
39+
String baseInstanceConfig = "my-base-instance-config";
40+
String instanceConfigId = "custom-instance-config4";
41+
createInstanceConfig(projectId, baseInstanceConfig, instanceConfigId);
42+
}
43+
44+
static void createInstanceConfig(
45+
String projectId, String baseInstanceConfig, String instanceConfigId) {
46+
try (Spanner spanner =
47+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
48+
final InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
49+
final InstanceConfig baseConfig = instanceAdminClient.getInstanceConfig(baseInstanceConfig);
50+
List<ReplicaInfo> readOnlyReplicas =
51+
ImmutableList.of(baseConfig.getOptionalReplicas().get(0));
52+
InstanceConfigInfo instanceConfigInfo =
53+
InstanceConfig.newBuilder(InstanceConfigId.of(projectId, instanceConfigId), baseConfig)
54+
.setDisplayName(instanceConfigId)
55+
.addReadOnlyReplicas(readOnlyReplicas)
56+
.build();
57+
final OperationFuture<InstanceConfig, CreateInstanceConfigMetadata> operation =
58+
instanceAdminClient.createInstanceConfig(instanceConfigInfo);
59+
try {
60+
System.out.printf("Waiting for create operation for %s to complete...\n", instanceConfigId);
61+
InstanceConfig instanceConfig = operation.get(5, TimeUnit.MINUTES);
62+
System.out.printf("Created instance configuration %s\n", instanceConfig.getId());
63+
} catch (ExecutionException | TimeoutException e) {
64+
System.out.printf(
65+
"Error: Creating instance configuration %s failed with error message %s\n",
66+
instanceConfigInfo.getId(), e.getMessage());
67+
} catch (InterruptedException e) {
68+
System.out.println(
69+
"Error: Waiting for createInstanceConfig operation to finish was interrupted");
70+
}
71+
}
72+
}
73+
}
74+
// [END spanner_create_instance_config]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2022 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_delete_instance_config]
20+
import com.google.cloud.spanner.InstanceAdminClient;
21+
import com.google.cloud.spanner.Spanner;
22+
import com.google.cloud.spanner.SpannerException;
23+
import com.google.cloud.spanner.SpannerOptions;
24+
25+
class DeleteInstanceConfigSample {
26+
static void deleteInstanceConfig() {
27+
// TODO(developer): Replace these variables before running the sample.
28+
String projectId = "my-project";
29+
String instanceConfigId = "custom-user-config";
30+
deleteInstanceConfig(projectId, instanceConfigId);
31+
}
32+
33+
static void deleteInstanceConfig(String projectId, String instanceConfigId) {
34+
try (Spanner spanner =
35+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
36+
final InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
37+
try {
38+
System.out.printf("Deleting %s...\n", instanceConfigId);
39+
instanceAdminClient.deleteInstanceConfig(instanceConfigId);
40+
System.out.printf("Deleted instance configuration %s\n", instanceConfigId);
41+
} catch (SpannerException e) {
42+
System.out.printf(
43+
"Error: Deleting instance configuration %s failed with error message: %s\n",
44+
instanceConfigId, e.getMessage());
45+
}
46+
}
47+
}
48+
}
49+
// [END spanner_delete_instance_config]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2022 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_list_instance_config_operations]
20+
import com.google.cloud.spanner.InstanceAdminClient;
21+
import com.google.cloud.spanner.Options;
22+
import com.google.cloud.spanner.Spanner;
23+
import com.google.cloud.spanner.SpannerOptions;
24+
import com.google.longrunning.Operation;
25+
import com.google.protobuf.InvalidProtocolBufferException;
26+
import com.google.spanner.admin.instance.v1.CreateInstanceConfigMetadata;
27+
28+
public class ListInstanceConfigOperationsSample {
29+
static void listInstanceConfigOperations() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "my-project";
32+
listInstanceConfigOperations(projectId);
33+
}
34+
35+
static void listInstanceConfigOperations(String projectId) {
36+
try (Spanner spanner =
37+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
38+
final InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
39+
40+
try {
41+
System.out.printf(
42+
"Getting list of instance config operations for project %s...\n",
43+
projectId);
44+
final Iterable<Operation> instanceConfigOperations =
45+
instanceAdminClient
46+
.listInstanceConfigOperations(
47+
Options.filter(
48+
"(metadata.@type=type.googleapis.com/"
49+
+ "google.spanner.admin.instance.v1.CreateInstanceConfigMetadata)"))
50+
.iterateAll();
51+
for (Operation operation : instanceConfigOperations) {
52+
CreateInstanceConfigMetadata metadata =
53+
operation.getMetadata().unpack(CreateInstanceConfigMetadata.class);
54+
System.out.printf(
55+
"Create instance config operation for %s is %d%% completed.\n",
56+
metadata.getInstanceConfig().getName(), metadata.getProgress().getProgressPercent());
57+
}
58+
} catch (InvalidProtocolBufferException e) {
59+
System.out.printf(
60+
"Error: Listing instance config operations failed with error message %s\n",
61+
e.getMessage());
62+
}
63+
}
64+
}
65+
}
66+
// [END spanner_list_instance_config_operations]

0 commit comments

Comments
 (0)