Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: add support for CMMR Phase 2 (#2085)
The following samples and corresponding tests have been added: - CreateInstanceConfig - UpdateInstanceConfig - DeleteInstanceConfig - ListInstanceConfigOperations
- Loading branch information
1 parent
cec6cfd
commit 833c572
Showing
8 changed files
with
375 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
samples/snippets/src/main/java/com/example/spanner/CreateInstanceConfigSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_create_instance_config] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.spanner.InstanceAdminClient; | ||
import com.google.cloud.spanner.InstanceConfig; | ||
import com.google.cloud.spanner.InstanceConfigId; | ||
import com.google.cloud.spanner.InstanceConfigInfo; | ||
import com.google.cloud.spanner.ReplicaInfo; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.spanner.admin.instance.v1.CreateInstanceConfigMetadata; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
class CreateInstanceConfigSample { | ||
static void createInstanceConfig() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String baseInstanceConfig = "my-base-instance-config"; | ||
String instanceConfigId = "custom-instance-config4"; | ||
createInstanceConfig(projectId, baseInstanceConfig, instanceConfigId); | ||
} | ||
|
||
static void createInstanceConfig( | ||
String projectId, String baseInstanceConfig, String instanceConfigId) { | ||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
final InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient(); | ||
final InstanceConfig baseConfig = instanceAdminClient.getInstanceConfig(baseInstanceConfig); | ||
List<ReplicaInfo> readOnlyReplicas = | ||
ImmutableList.of(baseConfig.getOptionalReplicas().get(0)); | ||
InstanceConfigInfo instanceConfigInfo = | ||
InstanceConfig.newBuilder(InstanceConfigId.of(projectId, instanceConfigId), baseConfig) | ||
.setDisplayName(instanceConfigId) | ||
.addReadOnlyReplicas(readOnlyReplicas) | ||
.build(); | ||
final OperationFuture<InstanceConfig, CreateInstanceConfigMetadata> operation = | ||
instanceAdminClient.createInstanceConfig(instanceConfigInfo); | ||
try { | ||
System.out.printf("Waiting for create operation for %s to complete...\n", instanceConfigId); | ||
InstanceConfig instanceConfig = operation.get(5, TimeUnit.MINUTES); | ||
System.out.printf("Created instance configuration %s\n", instanceConfig.getId()); | ||
} catch (ExecutionException | TimeoutException e) { | ||
System.out.printf( | ||
"Error: Creating instance configuration %s failed with error message %s\n", | ||
instanceConfigInfo.getId(), e.getMessage()); | ||
} catch (InterruptedException e) { | ||
System.out.println( | ||
"Error: Waiting for createInstanceConfig operation to finish was interrupted"); | ||
} | ||
} | ||
} | ||
} | ||
// [END spanner_create_instance_config] |
49 changes: 49 additions & 0 deletions
49
samples/snippets/src/main/java/com/example/spanner/DeleteInstanceConfigSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_delete_instance_config] | ||
import com.google.cloud.spanner.InstanceAdminClient; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerException; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
|
||
class DeleteInstanceConfigSample { | ||
static void deleteInstanceConfig() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceConfigId = "custom-user-config"; | ||
deleteInstanceConfig(projectId, instanceConfigId); | ||
} | ||
|
||
static void deleteInstanceConfig(String projectId, String instanceConfigId) { | ||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
final InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient(); | ||
try { | ||
System.out.printf("Deleting %s...\n", instanceConfigId); | ||
instanceAdminClient.deleteInstanceConfig(instanceConfigId); | ||
System.out.printf("Deleted instance configuration %s\n", instanceConfigId); | ||
} catch (SpannerException e) { | ||
System.out.printf( | ||
"Error: Deleting instance configuration %s failed with error message: %s\n", | ||
instanceConfigId, e.getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
// [END spanner_delete_instance_config] |
66 changes: 66 additions & 0 deletions
66
samples/snippets/src/main/java/com/example/spanner/ListInstanceConfigOperationsSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_list_instance_config_operations] | ||
import com.google.cloud.spanner.InstanceAdminClient; | ||
import com.google.cloud.spanner.Options; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.longrunning.Operation; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.google.spanner.admin.instance.v1.CreateInstanceConfigMetadata; | ||
|
||
public class ListInstanceConfigOperationsSample { | ||
static void listInstanceConfigOperations() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
listInstanceConfigOperations(projectId); | ||
} | ||
|
||
static void listInstanceConfigOperations(String projectId) { | ||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
final InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient(); | ||
|
||
try { | ||
System.out.printf( | ||
"Getting list of instance config operations for project %s...\n", | ||
projectId); | ||
final Iterable<Operation> instanceConfigOperations = | ||
instanceAdminClient | ||
.listInstanceConfigOperations( | ||
Options.filter( | ||
"(metadata.@type=type.googleapis.com/" | ||
+ "google.spanner.admin.instance.v1.CreateInstanceConfigMetadata)")) | ||
.iterateAll(); | ||
for (Operation operation : instanceConfigOperations) { | ||
CreateInstanceConfigMetadata metadata = | ||
operation.getMetadata().unpack(CreateInstanceConfigMetadata.class); | ||
System.out.printf( | ||
"Create instance config operation for %s is %d%% completed.\n", | ||
metadata.getInstanceConfig().getName(), metadata.getProgress().getProgressPercent()); | ||
} | ||
} catch (InvalidProtocolBufferException e) { | ||
System.out.printf( | ||
"Error: Listing instance config operations failed with error message %s\n", | ||
e.getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
// [END spanner_list_instance_config_operations] |
Oops, something went wrong.