Skip to content

Commit f2c19c7

Browse files
feat: allow opt-in for gRPC, have datastore-v1-proto-client as default (#1400)
* feat: allow opt-in for gRPC, have datastore-v1-proto-client as default transport * chore: update concepts test (#1382) Fixes #1379 (cherry picked from commit 9818aee) * fix host setting logic * fix test * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * update test * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 2fdc10b commit f2c19c7

File tree

11 files changed

+260
-131
lines changed

11 files changed

+260
-131
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ If you are using Maven without the BOM, add this to your dependencies:
5050
If you are using Gradle 5.x or later, add this to your dependencies:
5151

5252
```Groovy
53-
implementation platform('com.google.cloud:libraries-bom:26.34.0')
53+
implementation platform('com.google.cloud:libraries-bom:26.37.0')
5454
5555
implementation 'com.google.cloud:google-cloud-datastore'
5656
```
5757
If you are using Gradle without BOM, add this to your dependencies:
5858

5959
```Groovy
60-
implementation 'com.google.cloud:google-cloud-datastore:2.18.5'
60+
implementation 'com.google.cloud:google-cloud-datastore:2.19.0'
6161
```
6262

6363
If you are using SBT, add this to your dependencies:
6464

6565
```Scala
66-
libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "2.18.5"
66+
libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "2.19.0"
6767
```
6868
<!-- {x-version-update-end} -->
6969

@@ -380,7 +380,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
380380
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-datastore/java11.html
381381
[stability-image]: https://img.shields.io/badge/stability-stable-green
382382
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-datastore.svg
383-
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-datastore/2.18.5
383+
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-datastore/2.19.0
384384
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
385385
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
386386
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles

google-cloud-datastore/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
<groupId>com.google.api.grpc</groupId>
4343
<artifactId>proto-google-cloud-datastore-admin-v1</artifactId>
4444
</dependency>
45+
<dependency>
46+
<groupId>com.google.cloud.datastore</groupId>
47+
<artifactId>datastore-v1-proto-client</artifactId>
48+
</dependency>
4549
<dependency>
4650
<groupId>io.grpc</groupId>
4751
<artifactId>grpc-api</artifactId>
@@ -102,6 +106,10 @@
102106
<groupId>com.google.oauth-client</groupId>
103107
<artifactId>google-oauth-client</artifactId>
104108
</dependency>
109+
<dependency>
110+
<groupId>com.google.auth</groupId>
111+
<artifactId>google-auth-library-oauth2-http</artifactId>
112+
</dependency>
105113
<dependency>
106114
<groupId>io.opencensus</groupId>
107115
<artifactId>opencensus-api</artifactId>

google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreOptions.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.cloud.datastore.Validator.validateNamespace;
2020

21+
import com.google.api.core.BetaApi;
2122
import com.google.api.gax.core.CredentialsProvider;
2223
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
2324
import com.google.api.gax.rpc.TransportChannelProvider;
@@ -74,15 +75,8 @@ public ServiceRpc create(DatastoreOptions options) {
7475
try {
7576
if (options.getTransportOptions() instanceof GrpcTransportOptions) {
7677
return new GrpcDatastoreRpc(options);
77-
} else if (options.getTransportOptions() instanceof HttpTransportOptions) {
78-
// todo see if we can remove this check
79-
if (DatastoreUtils.isEmulator(options)) {
80-
throw new IllegalArgumentException("Only GRPC channels are allowed for emulator.");
81-
}
82-
return new HttpDatastoreRpc(options);
8378
} else {
84-
throw new IllegalArgumentException(
85-
"unknown transport type: " + options.getTransportOptions());
79+
return new HttpDatastoreRpc(options);
8680
}
8781
} catch (IOException e) {
8882
throw new RuntimeException(e);
@@ -96,6 +90,8 @@ public static class Builder extends ServiceOptions.Builder<Datastore, DatastoreO
9690
private String databaseId;
9791
private TransportChannelProvider channelProvider = null;
9892
private CredentialsProvider credentialsProvider = null;
93+
private String host;
94+
private TransportOptions transportOptions;
9995

10096
private Builder() {}
10197

@@ -109,15 +105,33 @@ private Builder(DatastoreOptions options) {
109105

110106
@Override
111107
public Builder setTransportOptions(TransportOptions transportOptions) {
108+
if (!(transportOptions instanceof HttpTransportOptions)) {
109+
throw new IllegalArgumentException(
110+
"Only http transport is allowed for " + API_SHORT_NAME + ".");
111+
}
112+
this.transportOptions = transportOptions;
113+
return super.setTransportOptions(transportOptions);
114+
}
115+
116+
@BetaApi
117+
public Builder setTransportOptions(GrpcTransportOptions transportOptions) {
118+
this.transportOptions = transportOptions;
112119
return super.setTransportOptions(transportOptions);
113120
}
114121

122+
@Override
123+
public Builder setHost(String host) {
124+
this.host = host;
125+
return super.setHost(host);
126+
}
127+
115128
/**
116129
* Sets the {@link TransportChannelProvider} to use with this Datastore client.
117130
*
118131
* @param channelProvider A InstantiatingGrpcChannelProvider object that defines the transport
119132
* provider for this client.
120133
*/
134+
@BetaApi
121135
public Builder setChannelProvider(TransportChannelProvider channelProvider) {
122136
this.channelProvider = validateChannelProvider(channelProvider);
123137
return this;
@@ -129,13 +143,17 @@ public Builder setChannelProvider(TransportChannelProvider channelProvider) {
129143
* @param credentialsProvider A CredentialsProvider object that defines the credential provider
130144
* for this client.
131145
*/
146+
@BetaApi
132147
public Builder setCredentialsProvider(CredentialsProvider credentialsProvider) {
133148
this.credentialsProvider = credentialsProvider;
134149
return this;
135150
}
136151

137152
@Override
138153
public DatastoreOptions build() {
154+
if (this.host == null && this.transportOptions instanceof GrpcTransportOptions) {
155+
this.setHost(DatastoreSettings.getDefaultEndpoint());
156+
}
139157
return new DatastoreOptions(this);
140158
}
141159

@@ -153,7 +171,7 @@ public Builder setDatabaseId(String databaseId) {
153171

154172
private static TransportChannelProvider validateChannelProvider(
155173
TransportChannelProvider channelProvider) {
156-
if (!(channelProvider instanceof InstantiatingGrpcChannelProvider)) {
174+
if (channelProvider != null && !(channelProvider instanceof InstantiatingGrpcChannelProvider)) {
157175
throw new IllegalArgumentException(
158176
"Only GRPC channels are allowed for " + API_SHORT_NAME + ".");
159177
}
@@ -165,7 +183,6 @@ private DatastoreOptions(Builder builder) {
165183
namespace = MoreObjects.firstNonNull(builder.namespace, defaultNamespace());
166184
databaseId = MoreObjects.firstNonNull(builder.databaseId, DEFAULT_DATABASE_ID);
167185

168-
// todo see if we can update this
169186
if (getTransportOptions() instanceof HttpTransportOptions
170187
&& (builder.channelProvider != null || builder.credentialsProvider != null)) {
171188
throw new IllegalArgumentException(
@@ -195,7 +212,7 @@ public TransportChannelProvider getTransportChannelProvider() {
195212
@Override
196213
protected String getDefaultHost() {
197214
String host = System.getProperty(LOCAL_HOST_ENV_VAR, System.getenv(LOCAL_HOST_ENV_VAR));
198-
return host != null ? host : DatastoreSettings.getDefaultEndpoint();
215+
return host != null ? host : com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST;
199216
}
200217

201218
@Override
@@ -222,8 +239,8 @@ public TransportOptions getDefaultTransportOptions() {
222239
return TRANSPORT_OPTIONS;
223240
}
224241

225-
public static GrpcTransportOptions.Builder getDefaultTransportOptionsBuilder() {
226-
return GrpcTransportOptions.newBuilder();
242+
public static HttpTransportOptions.Builder getDefaultTransportOptionsBuilder() {
243+
return HttpTransportOptions.newBuilder();
227244
}
228245
}
229246

0 commit comments

Comments
 (0)