-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: samples and tests for auto-generated createDatabase and createI…
…nstance APIs. (#2764) * fix: prevent illegal negative timeout values into thread sleep() method while retrying exceptions in unit tests. * For details on issue see - #2206 * Fixing lint issues. * chore: adding a few samples with auto-gen clients. * chore: adding integration tests for samples. * chore: fixing the end-point for staging. * chore: modified test for CreateDatabaseWithDefaultLeaderSample. * chore: adding sample and integration test for CreateInstanceSample. * chore: adding license headers. * chore: fix lint errors. * chore: rename file and add sample tags. * chore: address comments. * Update samples/snippets/src/main/java/com/example/spanner/v2/CreateDatabaseWithDefaultLeaderSample.java Co-authored-by: Knut Olav Løite <koloite@gmail.com> * chore: rename file path. * chore: remove admin settings. * chore: address comments. * 🦉 Updates from OwlBot post-processor See /~https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Knut Olav Løite <koloite@gmail.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
21f5eba
commit 74a586f
Showing
11 changed files
with
463 additions
and
13 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
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
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
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
78 changes: 78 additions & 0 deletions
78
.../main/java/com/example/spanner/admin/generated/CreateDatabaseWithDefaultLeaderSample.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,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"; | ||
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] |
70 changes: 70 additions & 0 deletions
70
...les/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceExample.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,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] |
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
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
61 changes: 61 additions & 0 deletions
61
...est/java/com/example/spanner/admin/generated/CreateDatabaseWithDefaultLeaderSampleIT.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,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) | ||
); | ||
} | ||
} |
Oops, something went wrong.