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

feat(spanner): add support of using emulator using autogenerated admin clients #2864

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.api.core.BetaApi;
import com.google.api.gax.core.GoogleCredentialsProvider;
import com.google.api.gax.core.InstantiatingExecutorProvider;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider;
import com.google.api.gax.rpc.ApiClientHeaderProvider;
Expand All @@ -36,6 +37,7 @@
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStubSettings;
import com.google.common.base.Strings;
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.Policy;
import com.google.iam.v1.SetIamPolicyRequest;
Expand Down Expand Up @@ -117,6 +119,8 @@
@Generated("by gapic-generator-java")
public class DatabaseAdminSettings extends ClientSettings<DatabaseAdminSettings> {

static final String SPANNER_EMULATOR_HOST_ENV_VAR = "SPANNER_EMULATOR_HOST";

/** Returns the object with the settings used for calls to listDatabases. */
public PagedCallSettings<ListDatabasesRequest, ListDatabasesResponse, ListDatabasesPagedResponse>
listDatabasesSettings() {
Expand Down Expand Up @@ -314,6 +318,20 @@ public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuil

/** Returns a new gRPC builder for this class. */
public static Builder newBuilder() {
String hostAndPort = System.getenv(SPANNER_EMULATOR_HOST_ENV_VAR);
Copy link
Contributor

Choose a reason for hiding this comment

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

@olavloite has suggested not to add logic on autogenerated files since there is a chance of this file getting overwritten during the next autogenerated PRs.
In NodeJS, we are writing a function similar to Python which can return a new autogenerated admin client with the emulator host set.

Copy link
Contributor Author

@rahul2393 rahul2393 Feb 6, 2024

Choose a reason for hiding this comment

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

if (!Strings.isNullOrEmpty(hostAndPort)) {
int port;
try {
port = Integer.parseInt(hostAndPort.substring(hostAndPort.lastIndexOf(":") + 1));
return newBuilderForEmulator(hostAndPort.substring(0, hostAndPort.lastIndexOf(":")), port);
} catch (NumberFormatException | IndexOutOfBoundsException ex) {
throw new RuntimeException(
"Invalid host/port in "
+ SPANNER_EMULATOR_HOST_ENV_VAR
+ " environment variable: "
+ hostAndPort);
}
}
return Builder.createDefault();
}

Expand All @@ -328,6 +346,34 @@ public static Builder newBuilder(ClientContext clientContext) {
return new Builder(clientContext);
}

/** Create a new builder preconfigured to connect to the Spanner emulator with port number. */
public static Builder newBuilderForEmulator(int port) {
return newBuilderForEmulator("localhost", port);
}

/**
* Creates a new builder preconfigured to connect to the Spanner emulator with host name and port
* number.
*/
public static Builder newBuilderForEmulator(String hostname, int port) {
Builder builder =
new Builder(
DatabaseAdminStubSettings.newBuilder()
.setEndpoint(hostname + ":" + port)
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
InstantiatingGrpcChannelProvider.newBuilder()
.setEndpoint(hostname + ":" + port)
.setPoolSize(1)
.setChannelConfigurator(
input -> {
input.usePlaintext();
return input;
})
.build()));
return builder;
}

/** Returns a builder containing all the values of this settings class. */
public Builder toBuilder() {
return new Builder(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.api.core.BetaApi;
import com.google.api.gax.core.GoogleCredentialsProvider;
import com.google.api.gax.core.InstantiatingExecutorProvider;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider;
import com.google.api.gax.rpc.ApiClientHeaderProvider;
Expand All @@ -34,6 +35,7 @@
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStubSettings;
import com.google.common.base.Strings;
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.Policy;
import com.google.iam.v1.SetIamPolicyRequest;
Expand Down Expand Up @@ -104,6 +106,8 @@
@Generated("by gapic-generator-java")
public class InstanceAdminSettings extends ClientSettings<InstanceAdminSettings> {

static final String SPANNER_EMULATOR_HOST_ENV_VAR = "SPANNER_EMULATOR_HOST";

/** Returns the object with the settings used for calls to listInstanceConfigs. */
public PagedCallSettings<
ListInstanceConfigsRequest, ListInstanceConfigsResponse, ListInstanceConfigsPagedResponse>
Expand Down Expand Up @@ -256,6 +260,20 @@ public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuil

/** Returns a new gRPC builder for this class. */
public static Builder newBuilder() {
String hostAndPort = System.getenv(SPANNER_EMULATOR_HOST_ENV_VAR);
if (!Strings.isNullOrEmpty(hostAndPort)) {
int port;
try {
port = Integer.parseInt(hostAndPort.substring(hostAndPort.lastIndexOf(":") + 1));
return newBuilderForEmulator(hostAndPort.substring(0, hostAndPort.lastIndexOf(":")), port);
} catch (NumberFormatException | IndexOutOfBoundsException ex) {
throw new RuntimeException(
"Invalid host/port in "
+ SPANNER_EMULATOR_HOST_ENV_VAR
+ " environment variable: "
+ hostAndPort);
}
}
return Builder.createDefault();
}

Expand All @@ -270,6 +288,34 @@ public static Builder newBuilder(ClientContext clientContext) {
return new Builder(clientContext);
}

/** Create a new builder preconfigured to connect to the Spanner emulator with port number. */
public static Builder newBuilderForEmulator(int port) {
return newBuilderForEmulator("localhost", port);
}

/**
* Creates a new builder preconfigured to connect to the Spanner emulator with host name and port
* number.
*/
public static Builder newBuilderForEmulator(String hostname, int port) {
Builder builder =
new Builder(
InstanceAdminStubSettings.newBuilder()
.setEndpoint(hostname + ":" + port)
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
InstantiatingGrpcChannelProvider.newBuilder()
.setEndpoint(hostname + ":" + port)
.setPoolSize(1)
.setChannelConfigurator(
input -> {
input.usePlaintext();
return input;
})
.build()));
return builder;
}

/** Returns a builder containing all the values of this settings class. */
public Builder toBuilder() {
return new Builder(this);
Expand Down