Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: samples and tests for instance APIs. #2768

Merged
merged 37 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
edc5bbf
fix: prevent illegal negative timeout values into thread sleep() meth…
arpan14 Feb 6, 2023
49a85df
Merge pull request #1 from arpan14/retryerror
arpan14 Feb 8, 2023
4cd497b
Fixing lint issues.
arpan14 Feb 8, 2023
4a6aa8e
Merge branch 'googleapis:main' into main
arpan14 Mar 13, 2023
b2aa09d
Merge branch 'googleapis:main' into main
arpan14 Mar 15, 2023
8d6d71e
Merge branch 'googleapis:main' into main
arpan14 May 9, 2023
77e6e7d
Merge branch 'googleapis:main' into main
arpan14 Jul 17, 2023
e8b7fad
Merge branch 'googleapis:main' into main
arpan14 Jul 25, 2023
8aa84e1
Merge branch 'googleapis:main' into main
arpan14 Oct 10, 2023
57fd405
Merge branch 'googleapis:main' into main
arpan14 Oct 27, 2023
1253563
Merge branch 'googleapis:main' into main
arpan14 Nov 20, 2023
d4f6a60
Merge branch 'googleapis:main' into main
arpan14 Dec 15, 2023
3efaf7c
Merge branch 'googleapis:main' into main
arpan14 Dec 26, 2023
f41b39f
Merge branch 'googleapis:main' into main
arpan14 Jan 3, 2024
82a16e9
chore: adding a few samples with auto-gen clients.
arpan14 Nov 29, 2023
c187621
chore: adding integration tests for samples.
arpan14 Dec 18, 2023
360d5db
chore: fixing the end-point for staging.
arpan14 Dec 21, 2023
a12fb43
chore: modified test for CreateDatabaseWithDefaultLeaderSample.
arpan14 Dec 21, 2023
f144f42
chore: adding sample and integration test for CreateInstanceSample.
arpan14 Dec 21, 2023
091414c
chore: adding license headers.
arpan14 Dec 21, 2023
988fd7a
chore: fix lint errors.
arpan14 Dec 21, 2023
922f47b
chore: rename file and add sample tags.
arpan14 Dec 23, 2023
0bf6f87
chore: address comments.
arpan14 Dec 23, 2023
396419d
Update samples/snippets/src/main/java/com/example/spanner/v2/CreateDa…
arpan14 Dec 23, 2023
eb997ae
chore: rewrite CreateInstanceConfigSample.
arpan14 Dec 25, 2023
b54bb0f
chore: rewrite instance API samples.
arpan14 Dec 25, 2023
fa0aa1e
chore: removed previous files and adding new integration test.
arpan14 Dec 26, 2023
78beaac
chore: changing instance integration tests.
arpan14 Dec 26, 2023
7fb407b
chore: add samples/tests for instance creation with processing units …
arpan14 Dec 26, 2023
c602c5b
chore:rename directory.
arpan14 Jan 3, 2024
be2b503
chore: minor fixes.
arpan14 Jan 4, 2024
335b465
chore: address comments.
arpan14 Jan 11, 2024
97ee485
chore: fix lint errors.
arpan14 Jan 11, 2024
fbf4df5
chore: fix lint errors.
arpan14 Jan 11, 2024
1b6a011
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jan 11, 2024
a975ecf
chore: close client resources.
arpan14 Jan 11, 2024
87bea00
chore: fix lint issues.
arpan14 Jan 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
<!-- {x-version-update-end} -->

Expand Down Expand Up @@ -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) |



Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion samples/install-without-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
<relativePath></relativePath>
arpan14 marked this conversation as resolved.
Show resolved Hide resolved
</parent>

<properties>
Expand Down Expand Up @@ -144,7 +145,7 @@
<systemPropertyVariables>
<spanner.test.instance>java-client-integration-test</spanner.test.instance>
<spanner.test.instance.mr>java-client-mr-integration-test</spanner.test.instance.mr>
<spanner.test.instance.config>nam15</spanner.test.instance.config>
<spanner.test.instance.config>nam11</spanner.test.instance.config>
arpan14 marked this conversation as resolved.
Show resolved Hide resolved
<spanner.test.key.location>us-east1</spanner.test.key.location>
<spanner.test.key.ring>cmek-test-key-ring</spanner.test.key.ring>
<spanner.test.key.name>cmek-test-key</spanner.test.key.name>
Expand Down
1 change: 1 addition & 0 deletions samples/native-image/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
<relativePath></relativePath>
</parent>

<properties>
Expand Down
3 changes: 2 additions & 1 deletion samples/snapshot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
<relativePath></relativePath>
</parent>

<properties>
Expand Down Expand Up @@ -143,7 +144,7 @@
<systemPropertyVariables>
<spanner.test.instance>java-client-integration-test</spanner.test.instance>
<spanner.test.instance.mr>java-client-mr-integration-test</spanner.test.instance.mr>
<spanner.test.instance.config>nam15</spanner.test.instance.config>
<spanner.test.instance.config>nam11</spanner.test.instance.config>
<spanner.test.key.location>us-east1</spanner.test.key.location>
<spanner.test.key.ring>cmek-test-key-ring</spanner.test.key.ring>
<spanner.test.key.name>cmek-test-key</spanner.test.key.name>
Expand Down
2 changes: 1 addition & 1 deletion samples/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
<systemPropertyVariables>
<spanner.test.instance>java-client-integration-test</spanner.test.instance>
<spanner.test.instance.mr>java-client-mr-integration-test</spanner.test.instance.mr>
<spanner.test.instance.config>nam15</spanner.test.instance.config>
<spanner.test.instance.config>nam11</spanner.test.instance.config>
<spanner.test.key.location>us-east1</spanner.test.key.location>
<spanner.test.key.ring>cmek-test-key-ring</spanner.test.key.ring>
<spanner.test.key.name>cmek-test-key</spanner.test.key.name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
olavloite marked this conversation as resolved.
Show resolved Hide resolved

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<ReplicaInfo> 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]
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}
}
}
}
Expand Down
Loading
Loading