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 auto-generated createDatabase and createInstance APIs. #2764

Merged
merged 26 commits into from Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 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
c249a05
chore: adding a few samples with auto-gen clients.
arpan14 Nov 29, 2023
6eb9f2d
chore: adding integration tests for samples.
arpan14 Dec 18, 2023
e2baca9
chore: fixing the end-point for staging.
arpan14 Dec 21, 2023
01a2369
chore: modified test for CreateDatabaseWithDefaultLeaderSample.
arpan14 Dec 21, 2023
8daf0df
chore: adding sample and integration test for CreateInstanceSample.
arpan14 Dec 21, 2023
22dbd2f
chore: adding license headers.
arpan14 Dec 21, 2023
b0ce7ca
chore: fix lint errors.
arpan14 Dec 21, 2023
cfada59
chore: rename file and add sample tags.
arpan14 Dec 23, 2023
4846143
chore: address comments.
arpan14 Dec 23, 2023
17bf04d
Update samples/snippets/src/main/java/com/example/spanner/v2/CreateDa…
arpan14 Dec 23, 2023
6087d7f
chore: rename file path.
arpan14 Dec 26, 2023
033a505
chore: remove admin settings.
arpan14 Dec 26, 2023
91febb4
Merge branch 'googleapis:main' into auto-gen-admin-client
arpan14 Dec 26, 2023
2bb6e11
chore: address comments.
arpan14 Dec 26, 2023
a20463e
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jan 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions samples/install-without-bom/pom.xml
Expand Up @@ -142,9 +142,9 @@
<version>3.1.2</version>
<configuration>
<systemPropertyVariables>
<spanner.test.instance>spanner-testing-east1</spanner.test.instance>
<spanner.test.instance.mr>spanner-mr-testing</spanner.test.instance.mr>
<spanner.test.instance.config>nam6</spanner.test.instance.config>
<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.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
7 changes: 4 additions & 3 deletions samples/snapshot/pom.xml
Expand Up @@ -141,13 +141,14 @@
<version>3.1.2</version>
<configuration>
<systemPropertyVariables>
<spanner.test.instance>spanner-testing-east1</spanner.test.instance>
<spanner.test.instance.mr>spanner-mr-testing</spanner.test.instance.mr>
<spanner.test.instance.config>nam6</spanner.test.instance.config>
<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.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>
<spanner.sample.database>mysample</spanner.sample.database>
<spanner.sample.instance>mysample-instance</spanner.sample.instance>
<spanner.quickstart.database>quick-db</spanner.quickstart.database>
</systemPropertyVariables>
</configuration>
Expand Down
6 changes: 3 additions & 3 deletions samples/snippets/pom.xml
Expand Up @@ -178,9 +178,9 @@
<version>3.1.2</version>
<configuration>
<systemPropertyVariables>
<spanner.test.instance>spanner-testing-east1</spanner.test.instance>
<spanner.test.instance.mr>spanner-mr-testing</spanner.test.instance.mr>
<spanner.test.instance.config>nam6</spanner.test.instance.config>
<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.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
@@ -0,0 +1,78 @@
/*
* Copyright 2023 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_database_with_default_leader]

import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.common.collect.ImmutableList;
import com.google.spanner.admin.database.v1.CreateDatabaseRequest;
import com.google.spanner.admin.database.v1.Database;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class CreateDatabaseWithDefaultLeaderSample {

static void createDatabaseWithDefaultLeader() throws IOException {
// TODO(developer): Replace these variables before running the sample.
final String instanceName = "projects/my-project/instances/my-instance-id";
final String databaseId = "my-database-name";
final String defaultLeader = "my-default-leader";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could we use a default leader name here that is more similar to the actual value that a user would have to use. That might help users who don't know what the default leader option is, to understand and learn that.

createDatabaseWithDefaultLeader(instanceName, databaseId, defaultLeader);
}

static void createDatabaseWithDefaultLeader(String instanceName, String databaseId,
String defaultLeader) throws IOException {
DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create();

try {
Database createdDatabase =
databaseAdminClient.createDatabaseAsync(
CreateDatabaseRequest.newBuilder()
.setParent(instanceName)
.setCreateStatement("CREATE DATABASE `" + databaseId + "`")
.addAllExtraStatements(
ImmutableList.of("CREATE TABLE Singers ("
+ " SingerId INT64 NOT NULL,"
+ " FirstName STRING(1024),"
+ " LastName STRING(1024),"
+ " SingerInfo BYTES(MAX)"
+ ") PRIMARY KEY (SingerId)",
"CREATE TABLE Albums ("
+ " SingerId INT64 NOT NULL,"
+ " AlbumId INT64 NOT NULL,"
+ " AlbumTitle STRING(MAX)"
+ ") PRIMARY KEY (SingerId, AlbumId),"
+ " INTERLEAVE IN PARENT Singers ON DELETE CASCADE",
"ALTER DATABASE " + "`" + databaseId + "`"
+ " SET OPTIONS ( default_leader = '" + defaultLeader + "' )"))
.build()).get();
System.out.println("Created database [" + createdDatabase.getName() + "]");
System.out.println("\tDefault leader: " + createdDatabase.getDefaultLeader());
} catch (ExecutionException e) {
// If the operation failed during execution, expose the cause.
throw (SpannerException) e.getCause();
} catch (InterruptedException e) {
// Throw when a thread is waiting, sleeping, or otherwise occupied,
// and the thread is interrupted, either before or during the activity.
throw SpannerExceptionFactory.propagateInterrupt(e);
}
}
}
//[END spanner_create_database_with_default_leader]
@@ -0,0 +1,70 @@
/*
* Copyright 2023 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]
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;
import java.util.concurrent.ExecutionException;

class CreateInstanceExample {

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 {
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");
}
}
}
//[END spanner_create_instance]
Expand Up @@ -26,24 +26,38 @@
*/
public class SampleIdGenerator {

private static final int INSTANCE_NAME_MAX_LENGTH = 30;
private static final int DATABASE_NAME_MAX_LENGTH = 30;
private static final int BACKUP_NAME_MAX_LENGTH = 30;
private static final int INSTANCE_CONFIG_ID_MAX_LENGTH = 30;
private final List<String> instanceIds;
private final List<String> databaseIds;
private final List<String> backupIds;
private final List<String> instanceConfigIds;
private final String baseDatabaseId;
private final String baseBackupId;
private final String baseInstanceConfigId;
private final String baseInstanceId;

public SampleIdGenerator(
String baseDatabaseId, String baseBackupId, String baseInstanceConfigId) {
public SampleIdGenerator(String baseDatabaseId, String baseBackupId,
String baseInstanceConfigId, String baseInstanceId) {
this.baseDatabaseId = baseDatabaseId;
this.baseBackupId = baseBackupId;
this.baseInstanceConfigId = baseInstanceConfigId;
this.baseInstanceId = baseInstanceId;
this.databaseIds = new ArrayList<>();
this.backupIds = new ArrayList<>();
this.instanceConfigIds = new ArrayList<>();
this.instanceIds = new ArrayList<>();
}

public String generateInstanceId() {
final String instanceId =
(baseInstanceId + "-" + UUID.randomUUID().toString().replaceAll("-", ""))
.substring(0, INSTANCE_NAME_MAX_LENGTH);

instanceIds.add(instanceId);
return instanceId;
}

public String generateDatabaseId() {
Expand Down Expand Up @@ -81,6 +95,10 @@ public List<String> getBackupIds() {
return backupIds;
}

public List<String> getInstanceIds() {
return instanceIds;
}

public List<String> getInstanceConfigIds() {
return instanceConfigIds;
}
Expand Down
Expand Up @@ -25,7 +25,8 @@

/** Base class for sample integration tests. */
public class SampleTestBase {

private static final String BASE_INSTANCE_ID =
System.getProperty("spanner.sample.instance", "mysample-instance");
private static final String BASE_DATABASE_ID =
System.getProperty("spanner.sample.database", "sampledb");
private static final String BASE_BACKUP_ID = "samplebk";
Expand Down Expand Up @@ -56,11 +57,27 @@ public static void beforeClass() {
spanner = options.getService();
databaseAdminClient = spanner.getDatabaseAdminClient();
instanceAdminClient = spanner.getInstanceAdminClient();
idGenerator = new SampleIdGenerator(BASE_DATABASE_ID, BASE_BACKUP_ID, BASE_INSTANCE_CONFIG_ID);
idGenerator = new SampleIdGenerator(
BASE_DATABASE_ID, BASE_BACKUP_ID, BASE_INSTANCE_CONFIG_ID, BASE_INSTANCE_ID);
}

@AfterClass
public static void afterClass() {
for (String instanceId : idGenerator.getInstanceIds()) {
System.out.println("Trying to drop " + instanceId);
try {
// If the database is not found, it is ignored (no exception is thrown)
instanceAdminClient.deleteInstance(instanceId);
} catch (Exception e) {
System.out.println(
"Failed to drop instance "
+ instanceId
+ " due to "
+ e.getMessage()
+ ", skipping...");
}
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
}

for (String databaseId : idGenerator.getDatabaseIds()) {
System.out.println("Trying to drop " + databaseId);
try {
Expand Down
@@ -0,0 +1,61 @@
/*
* Copyright 2023 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 com.google.spanner.admin.instance.v1.InstanceConfig;
import com.google.spanner.admin.instance.v1.InstanceName;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class CreateDatabaseWithDefaultLeaderSampleIT extends SampleTestBaseV2 {

@Test
public void testCreateDatabaseWithDefaultLeader() throws Exception {
final String databaseId = idGenerator.generateDatabaseId();

// Finds possible default leader

final String instanceConfigId = instanceAdminClient.getInstance(
InstanceName.of(projectId, multiRegionalInstanceId)).getConfig();
final InstanceConfig config = instanceAdminClient.getInstanceConfig(instanceConfigId);
assertTrue(
"Expected instance config " + instanceConfigId + " to have at least one leader option",
config.getLeaderOptionsCount() > 0
);
final String defaultLeader = config.getLeaderOptions(0);

// Runs sample
final String out = SampleRunner.runSample(() ->
CreateDatabaseWithDefaultLeaderSample.createDatabaseWithDefaultLeader(
getInstanceName(projectId, multiRegionalInstanceId),
databaseId,
defaultLeader
)
);

assertTrue(
"Expected created database to have default leader " + defaultLeader + "."
+ " Output received was " + out,
out.contains("Default leader: " + defaultLeader)
);
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2023 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 CreateInstanceSampleIT extends SampleTestBaseV2 {

@Test
public void testCreateInstance() throws Exception {
final String instanceId = idGenerator.generateInstanceId();

// Runs sample
final String out = SampleRunner.runSample(() ->
CreateInstanceExample.createInstance(projectId, instanceId)
);

assertTrue(
"Expected created instance " + instanceId + "."
+ " Output received was " + out, out.contains("was successfully created")
);
}
}