-
Notifications
You must be signed in to change notification settings - Fork 124
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
Changes from 18 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 49a85df
Merge pull request #1 from arpan14/retryerror
arpan14 4cd497b
Fixing lint issues.
arpan14 4a6aa8e
Merge branch 'googleapis:main' into main
arpan14 b2aa09d
Merge branch 'googleapis:main' into main
arpan14 8d6d71e
Merge branch 'googleapis:main' into main
arpan14 77e6e7d
Merge branch 'googleapis:main' into main
arpan14 e8b7fad
Merge branch 'googleapis:main' into main
arpan14 8aa84e1
Merge branch 'googleapis:main' into main
arpan14 57fd405
Merge branch 'googleapis:main' into main
arpan14 1253563
Merge branch 'googleapis:main' into main
arpan14 c249a05
chore: adding a few samples with auto-gen clients.
arpan14 6eb9f2d
chore: adding integration tests for samples.
arpan14 e2baca9
chore: fixing the end-point for staging.
arpan14 01a2369
chore: modified test for CreateDatabaseWithDefaultLeaderSample.
arpan14 8daf0df
chore: adding sample and integration test for CreateInstanceSample.
arpan14 22dbd2f
chore: adding license headers.
arpan14 b0ce7ca
chore: fix lint errors.
arpan14 cfada59
chore: rename file and add sample tags.
arpan14 4846143
chore: address comments.
arpan14 17bf04d
Update samples/snippets/src/main/java/com/example/spanner/v2/CreateDa…
arpan14 6087d7f
chore: rename file path.
arpan14 033a505
chore: remove admin settings.
arpan14 91febb4
Merge branch 'googleapis:main' into auto-gen-admin-client
arpan14 2bb6e11
chore: address comments.
arpan14 a20463e
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
77 changes: 77 additions & 0 deletions
77
.../snippets/src/main/java/com/example/spanner/v2/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,77 @@ | ||
/* | ||
* 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.v2; | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
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.CreateDatabaseMetadata; | ||
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 = "my-instance-name"; | ||
olavloite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 { | ||
OperationFuture<Database, CreateDatabaseMetadata> op1 = | ||
databaseAdminClient.createDatabaseAsync( | ||
CreateDatabaseRequest.newBuilder() | ||
.setParent(instanceName) | ||
.setCreateStatement("CREATE DATABASE " + "`" + databaseId + "`") | ||
arpan14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.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()); | ||
Database createdDatabase = op1.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 SpannerExceptionFactory.asSpannerException(e); | ||
olavloite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} 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); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
samples/snippets/src/main/java/com/example/spanner/v2/CreateInstanceSample.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,80 @@ | ||
/* | ||
* 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.v2; | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.spanner.SpannerExceptionFactory; | ||
import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; | ||
import com.google.cloud.spanner.admin.instance.v1.InstanceAdminSettings; | ||
import com.google.spanner.admin.instance.v1.CreateInstanceMetadata; | ||
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 CreateInstanceSample { | ||
|
||
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 { | ||
InstanceAdminSettings instanceAdminSettings = | ||
InstanceAdminSettings.newBuilder().setQuotaProjectId(projectId).build(); | ||
InstanceAdminClient instanceAdminClient = InstanceAdminClient.create(instanceAdminSettings); | ||
|
||
|
||
// 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(); | ||
OperationFuture<Instance, CreateInstanceMetadata> operation = | ||
instanceAdminClient.createInstanceAsync( | ||
CreateInstanceRequest.newBuilder() | ||
.setParent(ProjectName.of(projectId).toString()) | ||
.setInstanceId(instanceId) | ||
.setInstance(instance) | ||
.build()); | ||
|
||
try { | ||
// Wait for the createInstance operation to finish. | ||
Instance createdInstance = operation.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()); | ||
throw SpannerExceptionFactory.asSpannerException(e); | ||
} catch (InterruptedException e) { | ||
System.out.println("Error: Waiting for createInstance operation to finish was interrupted"); | ||
throw SpannerExceptionFactory.propagateInterrupt(e); | ||
} | ||
} | ||
} |
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
...nippets/src/test/java/com/example/spanner/v2/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.v2; | ||
|
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it is going to be confusing if we use
.v2
as a package name for these samples, as the actual change is that the code will start using classes that are in a package called.v1
, while they before that used classes that were in packages without a version number.Or could we temporarily use this
.v2
package, and when we make the switch, move these samples intocom.example.spanner
and the old samples intocom.example.spanner.deprecated
or something like that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah as you described, I plan to use this path temporarily. Also instead of calling it
.v2
, I changed it to.admin.generated
. Instead of calling the new pathdeprecated
we can name itarchived
. So, the new samples will be moved intocom.example.spanner
and the old samples intocom.example.spanner.archived
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍