From 88e24c7a7d046056605a2a824450e0153b339c86 Mon Sep 17 00:00:00 2001 From: Arpan Mishra Date: Fri, 12 Jan 2024 20:50:41 +0530 Subject: [PATCH] docs: samples and tests for instance APIs. (#2768) Adds samples and tests for auto-generated Instance Admin APIs. --- README.md | 14 ++- samples/install-without-bom/pom.xml | 3 +- samples/native-image/pom.xml | 1 + samples/snapshot/pom.xml | 3 +- samples/snippets/pom.xml | 2 +- ...CreateDatabaseWithDefaultLeaderSample.java | 4 +- .../generated/CreateInstanceConfigSample.java | 88 +++++++++++++++++++ .../generated/CreateInstanceExample.java | 57 ++++++------ ...eInstanceWithAutoscalingConfigExample.java | 87 ++++++++++++++++++ ...ateInstanceWithProcessingUnitsExample.java | 73 +++++++++++++++ .../generated/DeleteInstanceConfigSample.java | 55 ++++++++++++ .../generated/GetInstanceConfigSample.java | 51 +++++++++++ .../ListInstanceConfigOperationsSample.java | 67 ++++++++++++++ .../generated/ListInstanceConfigsSample.java | 48 ++++++++++ .../generated/UpdateInstanceConfigSample.java | 82 +++++++++++++++++ ...InstanceWithAutoscalingConfigSampleIT.java | 37 ++++++++ ...teInstanceWithProcessingUnitsSampleIT.java | 37 ++++++++ .../CustomInstanceConfigSampleIT.java | 74 ++++++++++++++++ .../admin/generated/SampleTestBaseV2.java | 11 ++- 19 files changed, 755 insertions(+), 39 deletions(-) create mode 100644 samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigExample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsExample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/admin/generated/DeleteInstanceConfigSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/admin/generated/GetInstanceConfigSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigOperationsSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigsSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateInstanceConfigSample.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigSampleIT.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsSampleIT.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/admin/generated/CustomInstanceConfigSampleIT.java diff --git a/README.md b/README.md index 417e48553d8..5b94cdadb35 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,13 @@ implementation 'com.google.cloud:google-cloud-spanner' If you are using Gradle without BOM, add this to your dependencies: ```Groovy -implementation 'com.google.cloud:google-cloud-spanner:6.55.0' +implementation 'com.google.cloud:google-cloud-spanner:6.56.0' ``` If you are using SBT, add this to your dependencies: ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.55.0" +libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.56.0" ``` @@ -331,7 +331,15 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/ | 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) | | Update Using Dml Returning Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/UpdateUsingDmlReturningSample.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/UpdateUsingDmlReturningSample.java) | | Create Database With Default Leader Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/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/admin/generated/CreateDatabaseWithDefaultLeaderSample.java) | +| Create Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/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/admin/generated/CreateInstanceConfigSample.java) | | Create Instance Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/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/admin/generated/CreateInstanceExample.java) | +| Create Instance With Autoscaling Config Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigExample.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/admin/generated/CreateInstanceWithAutoscalingConfigExample.java) | +| Create Instance With Processing Units Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/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/admin/generated/CreateInstanceWithProcessingUnitsExample.java) | +| Delete Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/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/admin/generated/DeleteInstanceConfigSample.java) | +| Get Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/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/admin/generated/GetInstanceConfigSample.java) | +| List Instance Config Operations Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/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/admin/generated/ListInstanceConfigOperationsSample.java) | +| List Instance Configs Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/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/admin/generated/ListInstanceConfigsSample.java) | +| Update Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/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/admin/generated/UpdateInstanceConfigSample.java) | @@ -435,7 +443,7 @@ Java is a registered trademark of Oracle and/or its affiliates. [kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-spanner/java11.html [stability-image]: https://img.shields.io/badge/stability-stable-green [maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-spanner.svg -[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.55.0 +[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.56.0 [authentication]: https://github.com/googleapis/google-cloud-java#authentication [auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes [predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index c30d1f02fd8..5db018897a9 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -15,6 +15,7 @@ com.google.cloud.samples shared-configuration 1.2.0 + @@ -144,7 +145,7 @@ java-client-integration-test java-client-mr-integration-test - nam15 + nam11 us-east1 cmek-test-key-ring cmek-test-key diff --git a/samples/native-image/pom.xml b/samples/native-image/pom.xml index 7beb606063b..d4a8d10d74c 100644 --- a/samples/native-image/pom.xml +++ b/samples/native-image/pom.xml @@ -14,6 +14,7 @@ com.google.cloud.samples shared-configuration 1.2.0 + diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index e58390df897..5e343b36dcc 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -15,6 +15,7 @@ com.google.cloud.samples shared-configuration 1.2.0 + @@ -143,7 +144,7 @@ java-client-integration-test java-client-mr-integration-test - nam15 + nam11 us-east1 cmek-test-key-ring cmek-test-key diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 0c55fa59f4a..406225e85f0 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -180,7 +180,7 @@ java-client-integration-test java-client-mr-integration-test - nam15 + nam11 us-east1 cmek-test-key-ring cmek-test-key diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithDefaultLeaderSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithDefaultLeaderSample.java index eedbca8095a..853ec557b94 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithDefaultLeaderSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithDefaultLeaderSample.java @@ -39,9 +39,7 @@ static void createDatabaseWithDefaultLeader() throws IOException { static void createDatabaseWithDefaultLeader(String instanceName, String databaseId, String defaultLeader) throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - - try { + try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { Database createdDatabase = databaseAdminClient.createDatabaseAsync( CreateDatabaseRequest.newBuilder() diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java new file mode 100644 index 00000000000..49ce023b24a --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java @@ -0,0 +1,88 @@ +/* + * Copyright 2024 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.admin.generated; + +// [START spanner_create_instance_config] + +import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; +import com.google.spanner.admin.instance.v1.CreateInstanceConfigRequest; +import com.google.spanner.admin.instance.v1.InstanceConfig; +import com.google.spanner.admin.instance.v1.InstanceConfigName; +import com.google.spanner.admin.instance.v1.ProjectName; +import com.google.spanner.admin.instance.v1.ReplicaInfo; +import java.io.IOException; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +class CreateInstanceConfigSample { + + static void createInstanceConfig() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String baseInstanceConfigId = "nam11"; + String instanceConfigId = "custom-instance-config4"; + + createInstanceConfig(projectId, baseInstanceConfigId, instanceConfigId); + } + + static void createInstanceConfig( + String projectId, String baseInstanceConfigId, String instanceConfigId) throws IOException { + try (InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + final InstanceConfigName baseInstanceConfigName = InstanceConfigName.of(projectId, + baseInstanceConfigId); + final InstanceConfig baseConfig = + instanceAdminClient.getInstanceConfig(baseInstanceConfigName.toString()); + final InstanceConfigName instanceConfigName = InstanceConfigName.of(projectId, + instanceConfigId); + /** + * The replicas for the custom instance configuration must include all the replicas of the + * base configuration, in addition to at least one from the list of optional replicas of the + * base configuration. + */ + final List replicas = + Stream.concat(baseConfig.getReplicasList().stream(), + baseConfig.getOptionalReplicasList().stream().limit(1)).collect(Collectors.toList()); + final InstanceConfig instanceConfig = + InstanceConfig.newBuilder().setName(instanceConfigName.toString()) + .setBaseConfig(baseInstanceConfigName.toString()) + .setDisplayName("Instance Configuration").addAllReplicas(replicas).build(); + final CreateInstanceConfigRequest createInstanceConfigRequest = + CreateInstanceConfigRequest.newBuilder().setParent(ProjectName.of(projectId).toString()) + .setInstanceConfigId(instanceConfigId).setInstanceConfig(instanceConfig).build(); + try { + System.out.printf("Waiting for create operation for %s to complete...\n", + instanceConfigName); + InstanceConfig instanceConfigResult = + instanceAdminClient.createInstanceConfigAsync( + createInstanceConfigRequest).get(5, TimeUnit.MINUTES); + System.out.printf("Created instance configuration %s\n", instanceConfigResult.getName()); + } catch (ExecutionException | TimeoutException e) { + System.out.printf( + "Error: Creating instance configuration %s failed with error message %s\n", + instanceConfig.getName(), e.getMessage()); + } catch (InterruptedException e) { + System.out.println( + "Error: Waiting for createInstanceConfig operation to finish was interrupted"); + } + } + } +} +// [END spanner_create_instance_config] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceExample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceExample.java index 88675c414cf..8664c85b444 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceExample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceExample.java @@ -17,6 +17,7 @@ package com.example.spanner.admin.generated; //[START spanner_create_instance] + import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; import com.google.spanner.admin.instance.v1.CreateInstanceRequest; import com.google.spanner.admin.instance.v1.Instance; @@ -35,35 +36,35 @@ static void createInstance() throws IOException { } static void createInstance(String projectId, String instanceId) throws IOException { - InstanceAdminClient instanceAdminClient = InstanceAdminClient.create(); - - // Set Instance configuration. - int nodeCount = 2; - String displayName = "Descriptive name"; + try (InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + // Set Instance configuration. + int nodeCount = 2; + String displayName = "Descriptive name"; - // Create an Instance object that will be used to create the instance. - Instance instance = - Instance.newBuilder() - .setDisplayName(displayName) - .setNodeCount(nodeCount) - .setConfig( - InstanceConfigName.of(projectId, "regional-us-central1").toString()) - .build(); - try { - // Wait for the createInstance operation to finish. - Instance createdInstance = instanceAdminClient.createInstanceAsync( - CreateInstanceRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) - .setInstanceId(instanceId) - .setInstance(instance) - .build()).get(); - System.out.printf("Instance %s was successfully created%n", createdInstance.getName()); - } catch (ExecutionException e) { - System.out.printf( - "Error: Creating instance %s failed with error message %s%n", - instance.getName(), e.getMessage()); - } catch (InterruptedException e) { - System.out.println("Error: Waiting for createInstance operation to finish was interrupted"); + // Create an Instance object that will be used to create the instance. + Instance instance = + Instance.newBuilder() + .setDisplayName(displayName) + .setNodeCount(nodeCount) + .setConfig( + InstanceConfigName.of(projectId, "regional-us-central1").toString()) + .build(); + try { + // Wait for the createInstance operation to finish. + Instance createdInstance = instanceAdminClient.createInstanceAsync( + CreateInstanceRequest.newBuilder() + .setParent(ProjectName.of(projectId).toString()) + .setInstanceId(instanceId) + .setInstance(instance) + .build()).get(); + System.out.printf("Instance %s was successfully created%n", createdInstance.getName()); + } catch (ExecutionException e) { + System.out.printf( + "Error: Creating instance %s failed with error message %s%n", + instance.getName(), e.getMessage()); + } catch (InterruptedException e) { + System.out.println("Error: Waiting for createInstance operation to finish was interrupted"); + } } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigExample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigExample.java new file mode 100644 index 00000000000..aa785fe57d0 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigExample.java @@ -0,0 +1,87 @@ +/* + * Copyright 2024 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.admin.generated; + +// [START spanner_create_instance_with_autoscaling_config] + +import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; +import com.google.spanner.admin.instance.v1.AutoscalingConfig; +import com.google.spanner.admin.instance.v1.CreateInstanceRequest; +import com.google.spanner.admin.instance.v1.Instance; +import com.google.spanner.admin.instance.v1.InstanceConfigName; +import com.google.spanner.admin.instance.v1.ProjectName; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class CreateInstanceWithAutoscalingConfigExample { + + static void createInstance() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + createInstance(projectId, instanceId); + } + + static void createInstance(String projectId, String instanceId) throws IOException { + try (InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + // Set Instance configuration. + String configId = "regional-us-central1"; + String displayName = "Descriptive name"; + + // Create an autoscaling config. + // When autoscaling_config is enabled, node_count and processing_units fields + // need not be specified. + AutoscalingConfig autoscalingConfig = + AutoscalingConfig.newBuilder() + .setAutoscalingLimits( + AutoscalingConfig.AutoscalingLimits.newBuilder().setMinNodes(1).setMaxNodes(2)) + .setAutoscalingTargets( + AutoscalingConfig.AutoscalingTargets.newBuilder() + .setHighPriorityCpuUtilizationPercent(65) + .setStorageUtilizationPercent(95)) + .build(); + Instance instance = + Instance.newBuilder() + .setAutoscalingConfig(autoscalingConfig) + .setDisplayName(displayName) + .setConfig( + InstanceConfigName.of(projectId, configId).toString()) + .build(); + + // Creates a new instance + System.out.printf("Creating instance %s.%n", instanceId); + try { + // Wait for the createInstance operation to finish. + Instance instanceResult = instanceAdminClient.createInstanceAsync( + CreateInstanceRequest.newBuilder() + .setParent(ProjectName.of(projectId).toString()) + .setInstanceId(instanceId) + .setInstance(instance) + .build()).get(); + System.out.printf("Autoscaler instance %s was successfully created%n", + instanceResult.getName()); + } catch (ExecutionException e) { + System.out.printf( + "Error: Creating instance %s failed with error message %s%n", + instance.getName(), e.getMessage()); + } catch (InterruptedException e) { + System.out.println("Error: Waiting for createInstance operation to finish was interrupted"); + } + } + } +} +// [END spanner_create_instance_with_autoscaling_config] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsExample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsExample.java new file mode 100644 index 00000000000..9aa0487098b --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsExample.java @@ -0,0 +1,73 @@ +/* + * Copyright 2024 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.admin.generated; + +//[START spanner_create_instance_with_processing_units] + +import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; +import com.google.spanner.admin.instance.v1.CreateInstanceRequest; +import com.google.spanner.admin.instance.v1.Instance; +import com.google.spanner.admin.instance.v1.InstanceConfigName; +import com.google.spanner.admin.instance.v1.ProjectName; +import java.io.IOException; + +class CreateInstanceWithProcessingUnitsExample { + + static void createInstance() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + createInstance(projectId, instanceId); + } + + static void createInstance(String projectId, String instanceId) throws IOException { + try (InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + // Set Instance configuration. + String configId = "regional-us-central1"; + // This will create an instance with the processing power of 0.2 nodes. + int processingUnits = 500; + String displayName = "Descriptive name"; + + try { + // Creates a new instance + System.out.printf("Creating instance %s.%n", instanceId); + Instance instance = + Instance.newBuilder() + .setDisplayName(displayName) + .setProcessingUnits(processingUnits) + .setConfig( + InstanceConfigName.of(projectId, configId).toString()) + .build(); + // Wait for the createInstance operation to finish. + System.out.printf("Waiting for operation on %s to complete...%n", instanceId); + Instance createdInstance = instanceAdminClient.createInstanceAsync( + CreateInstanceRequest.newBuilder() + .setParent(ProjectName.of(projectId).toString()) + .setInstanceId(instanceId) + .setInstance(instance) + .build()).get(); + + System.out.printf("Created instance %s.%n", createdInstance.getName()); + System.out.printf("Instance %s has %d processing units.%n", createdInstance.getName(), + createdInstance.getProcessingUnits()); + } catch (Exception e) { + System.out.printf("Error: %s.%n", e.getMessage()); + } + } + } +} +//[END spanner_create_instance_with_processing_units] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/DeleteInstanceConfigSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/DeleteInstanceConfigSample.java new file mode 100644 index 00000000000..1fa9aee586c --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/DeleteInstanceConfigSample.java @@ -0,0 +1,55 @@ +/* + * Copyright 2024 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.admin.generated; + +// [START spanner_delete_instance_config] + +import com.google.cloud.spanner.SpannerException; +import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; +import com.google.spanner.admin.instance.v1.DeleteInstanceConfigRequest; +import com.google.spanner.admin.instance.v1.InstanceConfigName; +import java.io.IOException; + +class DeleteInstanceConfigSample { + + static void deleteInstanceConfig() throws IOException { + // 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) throws IOException { + try (final InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + final InstanceConfigName instanceConfigName = InstanceConfigName.of(projectId, + instanceConfigId); + final DeleteInstanceConfigRequest request = + DeleteInstanceConfigRequest.newBuilder().setName(instanceConfigName.toString()).build(); + + try { + System.out.printf("Deleting %s...\n", instanceConfigName); + instanceAdminClient.deleteInstanceConfig(request); + System.out.printf("Deleted instance configuration %s\n", instanceConfigName); + } catch (SpannerException e) { + System.out.printf( + "Error: Deleting instance configuration %s failed with error message: %s\n", + instanceConfigName, e.getMessage()); + } + } + } +} +// [END spanner_delete_instance_config] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/GetInstanceConfigSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/GetInstanceConfigSample.java new file mode 100644 index 00000000000..5fc1b907479 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/GetInstanceConfigSample.java @@ -0,0 +1,51 @@ +/* + * Copyright 2024 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.admin.generated; + +//[START spanner_get_instance_config] + +import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; +import com.google.spanner.admin.instance.v1.InstanceConfig; +import com.google.spanner.admin.instance.v1.InstanceConfigName; +import java.io.IOException; + +public class GetInstanceConfigSample { + + static void getInstanceConfig() throws IOException { + // TODO(developer): Replace these variables before running the sample. + final String projectId = "my-project"; + final String instanceConfigId = "nam6"; + getInstanceConfig(projectId, instanceConfigId); + } + + static void getInstanceConfig(String projectId, String instanceConfigId) throws IOException { + try (final InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + final InstanceConfigName instanceConfigName = InstanceConfigName.of(projectId, + instanceConfigId); + + final InstanceConfig instanceConfig = + instanceAdminClient.getInstanceConfig(instanceConfigName.toString()); + + System.out.printf( + "Available leader options for instance config %s: %s%n", + instanceConfig.getName(), + instanceConfig.getLeaderOptionsList() + ); + } + } +} +//[END spanner_get_instance_config] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigOperationsSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigOperationsSample.java new file mode 100644 index 00000000000..6970c500694 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigOperationsSample.java @@ -0,0 +1,67 @@ +/* + * Copyright 2024 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.admin.generated; + +// [START spanner_list_instance_config_operations] + +import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; +import com.google.longrunning.Operation; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.spanner.admin.instance.v1.CreateInstanceConfigMetadata; +import com.google.spanner.admin.instance.v1.ListInstanceConfigOperationsRequest; +import com.google.spanner.admin.instance.v1.ProjectName; +import java.io.IOException; + +public class ListInstanceConfigOperationsSample { + + static void listInstanceConfigOperations() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + listInstanceConfigOperations(projectId); + } + + static void listInstanceConfigOperations(String projectId) throws IOException { + try (final InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + final ProjectName projectName = ProjectName.of(projectId); + System.out.printf( + "Getting list of instance config operations for project %s...\n", + projectId); + final ListInstanceConfigOperationsRequest request = + ListInstanceConfigOperationsRequest.newBuilder() + .setParent(projectName.toString()) + .setFilter("(metadata.@type=type.googleapis.com/" + + "google.spanner.admin.instance.v1.CreateInstanceConfigMetadata)").build(); + final Iterable instanceConfigOperations = + instanceAdminClient.listInstanceConfigOperations(request).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()); + } + System.out.printf( + "Obtained list of instance config operations for project %s...\n", + projectName); + } 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] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigsSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigsSample.java new file mode 100644 index 00000000000..92a0c7014e9 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigsSample.java @@ -0,0 +1,48 @@ +/* + * Copyright 2024 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.admin.generated; + +//[START spanner_list_instance_configs] + +import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; +import com.google.spanner.admin.instance.v1.InstanceConfig; +import com.google.spanner.admin.instance.v1.ProjectName; +import java.io.IOException; + +public class ListInstanceConfigsSample { + + static void listInstanceConfigs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + listInstanceConfigs(projectId); + } + + static void listInstanceConfigs(String projectId) throws IOException { + try (final InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + final ProjectName projectName = ProjectName.of(projectId); + for (InstanceConfig instanceConfig : + instanceAdminClient.listInstanceConfigs(projectName).iterateAll()) { + System.out.printf( + "Available leader options for instance config %s: %s%n", + instanceConfig.getName(), + instanceConfig.getLeaderOptionsList() + ); + } + } + } +} +//[END spanner_list_instance_configs] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateInstanceConfigSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateInstanceConfigSample.java new file mode 100644 index 00000000000..1fb5cd0e6fb --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateInstanceConfigSample.java @@ -0,0 +1,82 @@ +/* + * Copyright 2024 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.admin.generated; + +// [START spanner_update_instance_config] + +import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; +import com.google.common.collect.ImmutableList; +import com.google.protobuf.FieldMask; +import com.google.spanner.admin.instance.v1.InstanceConfig; +import com.google.spanner.admin.instance.v1.InstanceConfigName; +import com.google.spanner.admin.instance.v1.UpdateInstanceConfigRequest; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +class UpdateInstanceConfigSample { + + static void updateInstanceConfig() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceConfigId = "custom-instance-config"; + updateInstanceConfig(projectId, instanceConfigId); + } + + static void updateInstanceConfig(String projectId, String instanceConfigId) throws IOException { + try (final InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + final InstanceConfigName instanceConfigName = + InstanceConfigName.of(projectId, instanceConfigId); + final InstanceConfig instanceConfig = + InstanceConfig.newBuilder() + .setName(instanceConfigName.toString()) + .setDisplayName("updated custom instance config") + .putLabels("updated", "true").build(); + /** + * The field mask must always be specified; this prevents any future + * fields in [InstanceConfig][google.spanner.admin.instance.v1.InstanceConfig] + * from being erased accidentally by clients that do not know about them. + */ + final UpdateInstanceConfigRequest updateInstanceConfigRequest = + UpdateInstanceConfigRequest.newBuilder() + .setInstanceConfig(instanceConfig) + .setUpdateMask( + FieldMask.newBuilder().addAllPaths(ImmutableList.of("display_name", "labels")) + .build()).build(); + try { + System.out.printf("Waiting for update operation on %s to complete...\n", + instanceConfigName); + InstanceConfig instanceConfigResult = + instanceAdminClient.updateInstanceConfigAsync( + updateInstanceConfigRequest).get(5, TimeUnit.MINUTES); + System.out.printf( + "Updated instance configuration %s with new display name %s\n", + instanceConfigResult.getName(), instanceConfig.getDisplayName()); + } catch (ExecutionException | TimeoutException e) { + System.out.printf( + "Error: Updating instance config %s failed with error message %s\n", + instanceConfig.getName(), e.getMessage()); + e.printStackTrace(); + } catch (InterruptedException e) { + System.out.println( + "Error: Waiting for updateInstanceConfig operation to finish was interrupted"); + } + } + } +} +// [END spanner_update_instance_config] diff --git a/samples/snippets/src/test/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigSampleIT.java new file mode 100644 index 00000000000..86224cfd305 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigSampleIT.java @@ -0,0 +1,37 @@ +/* + * Copyright 2024 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.admin.generated; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.spanner.SampleRunner; +import com.google.spanner.admin.database.v1.InstanceName; +import org.junit.Test; + +public class CreateInstanceWithAutoscalingConfigSampleIT extends SampleTestBaseV2 { + + @Test + public void testCreateInstanceWithAutoscalingConfig() throws Exception { + String instanceId = idGenerator.generateInstanceId(); + String out = + SampleRunner.runSample( + () -> CreateInstanceWithAutoscalingConfigExample.createInstance(projectId, instanceId)); + assertThat(out) + .contains(String.format("Autoscaler instance %s", + InstanceName.of(projectId, instanceId).toString())); + } +} diff --git a/samples/snippets/src/test/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsSampleIT.java new file mode 100644 index 00000000000..13778dff825 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsSampleIT.java @@ -0,0 +1,37 @@ +/* + * Copyright 2024 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.admin.generated; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.spanner.SampleRunner; +import com.google.spanner.admin.database.v1.InstanceName; +import org.junit.Test; + +public class CreateInstanceWithProcessingUnitsSampleIT extends SampleTestBaseV2 { + + @Test + public void testCreateInstanceWithProcessingUnits() throws Exception { + String instanceId = idGenerator.generateInstanceId(); + String out = + SampleRunner.runSample( + () -> CreateInstanceWithProcessingUnitsExample.createInstance(projectId, instanceId)); + assertThat(out) + .contains(String.format("Instance %s has %d processing units", + InstanceName.of(projectId, instanceId).toString(), 500)); + } +} diff --git a/samples/snippets/src/test/java/com/example/spanner/admin/generated/CustomInstanceConfigSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/admin/generated/CustomInstanceConfigSampleIT.java new file mode 100644 index 00000000000..8a01d2a7a6b --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/admin/generated/CustomInstanceConfigSampleIT.java @@ -0,0 +1,74 @@ +/* + * Copyright 2024 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.admin.generated; + +import static org.junit.Assert.assertTrue; + +import com.example.spanner.SampleRunner; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CustomInstanceConfigSampleIT extends SampleTestBaseV2 { + + @Test + public void testCustomInstanceConfigOperations() throws Exception { + String customInstanceConfigId = idGenerator.generateInstanceConfigId(); + + // Create a random instance config. Display name is set to the instance config id in sample. + final String out1 = + SampleRunner.runSample( + () -> + CreateInstanceConfigSample.createInstanceConfig( + projectId, instanceConfigName, customInstanceConfigId)); + assertTrue(out1.contains("Created instance configuration")); + + // Fetch the instance config that was created above. + final String out2 = + SampleRunner.runSample( + () -> GetInstanceConfigSample.getInstanceConfig(projectId, instanceConfigName)); + assertTrue(out2.contains("Available leader options for instance config")); + + // Fetch the instance config that was created above. + final String out3 = + SampleRunner.runSample( + () -> ListInstanceConfigsSample.listInstanceConfigs(projectId)); + assertTrue(out3.contains("Available leader options for instance config")); + + // List the instance config operations. + final String out4 = + SampleRunner.runSample( + () -> + ListInstanceConfigOperationsSample.listInstanceConfigOperations(projectId)); + assertTrue(out4.contains("Obtained list of instance config operations")); + + // Update display name to a randomly generated instance config id. + final String out5 = + SampleRunner.runSample( + () -> + UpdateInstanceConfigSample.updateInstanceConfig(projectId, customInstanceConfigId)); + assertTrue(out5.contains("Updated instance configuration")); + + // Delete the created instance config. + final String out6 = + SampleRunner.runSample( + () -> + DeleteInstanceConfigSample.deleteInstanceConfig(projectId, customInstanceConfigId)); + assertTrue(out6.contains("Deleted instance configuration")); + } +} diff --git a/samples/snippets/src/test/java/com/example/spanner/admin/generated/SampleTestBaseV2.java b/samples/snippets/src/test/java/com/example/spanner/admin/generated/SampleTestBaseV2.java index 30989214fa3..941f97fe859 100644 --- a/samples/snippets/src/test/java/com/example/spanner/admin/generated/SampleTestBaseV2.java +++ b/samples/snippets/src/test/java/com/example/spanner/admin/generated/SampleTestBaseV2.java @@ -124,7 +124,7 @@ public static void afterClass() throws InterruptedException { for (String configId : idGenerator.getInstanceConfigIds()) { try { // If the config is not found, it is ignored (no exception is thrown) - instanceAdminClient.deleteInstanceConfig(configId); + instanceAdminClient.deleteInstanceConfig(getInstanceConfigName(projectId, configId)); } catch (Exception e) { System.out.println( "Failed to delete instance config " @@ -134,7 +134,6 @@ public static void afterClass() throws InterruptedException { + ", skipping..."); } } - databaseAdminClient.close(); instanceAdminClient.close(); @@ -157,4 +156,12 @@ static String getBackupName(final String projectId, static String getInstanceName(final String projectId, final String instanceId) { return String.format("projects/%s/instances/%s", projectId, instanceId); } + + static String getInstanceConfigName(final String projectId, final String instanceConfigId) { + return String.format("projects/%s/instanceConfigs/%s", projectId, instanceConfigId); + } + + static String getProjectName(final String projectId) { + return String.format("projects/%s", projectId); + } }