diff --git a/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java index 87d37100ff07..0d91ee0764fa 100644 --- a/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java +++ b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java @@ -17,6 +17,10 @@ package com.google.cloud.firestore; import com.google.api.core.InternalApi; +import com.google.api.gax.core.CredentialsProvider; +import com.google.api.gax.core.GoogleCredentialsProvider; +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; +import com.google.api.gax.rpc.TransportChannelProvider; import com.google.cloud.ServiceDefaults; import com.google.cloud.ServiceOptions; import com.google.cloud.TransportOptions; @@ -26,6 +30,7 @@ import com.google.cloud.grpc.GrpcTransportOptions; import com.google.common.collect.ImmutableSet; import java.io.IOException; +import java.util.Objects; import java.util.Set; import javax.annotation.Nonnull; @@ -38,13 +43,14 @@ public final class FirestoreOptions extends ServiceOptions { - private String databaseId = DEFAULT_DATABASE_ID; + private String databaseId = FirestoreDefaults.INSTANCE.getDatabaseId(); private boolean timestampsInSnapshotsEnabled = DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED; + private TransportChannelProvider channelProvider = + FirestoreDefaults.INSTANCE.getDefaultTransportChannelProvider(); + private CredentialsProvider credentialsProvider = + FirestoreDefaults.INSTANCE.getDefaultCredentialsProvider(); private Builder() {} private Builder(FirestoreOptions options) { super(options); - timestampsInSnapshotsEnabled = options.timestampsInSnapshotsEnabled; + this.databaseId = options.databaseId; + this.timestampsInSnapshotsEnabled = options.timestampsInSnapshotsEnabled; + this.channelProvider = options.channelProvider; + this.credentialsProvider = options.credentialsProvider; } + /** + * Sets the {@link TransportOptions} to use with this Firestore client. + * + * @param transportOptions A GrpcTransportOptions object that defines the transport options for + * this client. + */ @Nonnull @Override public Builder setTransportOptions(@Nonnull TransportOptions transportOptions) { if (!(transportOptions instanceof GrpcTransportOptions)) { throw new IllegalArgumentException( - "Only grpc transport is allowed for " + API_SHORT_NAME + "."); + "Only GRPC transport is allowed for " + API_SHORT_NAME + "."); } super.setTransportOptions(transportOptions); return this; } + /** + * Sets the {@link TransportChannelProvider} to use with this Firestore client. + * + * @param channelProvider A InstantiatingGrpcChannelProvider object that defines the transport + * provider for this client. + */ + @Nonnull + public Builder setChannelProvider(@Nonnull TransportChannelProvider channelProvider) { + if (!(channelProvider instanceof InstantiatingGrpcChannelProvider)) { + throw new IllegalArgumentException( + "Only GRPC channels are allowed for " + API_SHORT_NAME + "."); + } + this.channelProvider = channelProvider; + return this; + } + + /** + * Sets the {@link CredentialsProvider} to use with this Firestore client. + * + * @param credentialsProvider A CredentialsProvider object that defines the credential provider + * for this client. + */ + @Nonnull + public Builder setCredentialsProvider(@Nonnull CredentialsProvider credentialsProvider) { + this.credentialsProvider = credentialsProvider; + return this; + } + + /** + * Sets the database ID to use with this Firestore client. + * + * @param databaseId The Firestore database ID to use with this client. + */ + public Builder setDatabaseId(@Nonnull String databaseId) { + this.databaseId = databaseId; + return this; + } + /** * Enables the use of {@link com.google.cloud.Timestamp Timestamps} for timestamp fields in * {@link DocumentSnapshot DocumentSnapshots}. @@ -141,12 +206,6 @@ public Builder setTimestampsInSnapshotsEnabled(boolean value) { return this; } - @Nonnull - public Builder setDatabaseId(@Nonnull String databaseId) { - this.databaseId = databaseId; - return this; - } - @Override @Nonnull public FirestoreOptions build() { @@ -159,10 +218,31 @@ protected FirestoreOptions(Builder builder) { super(FirestoreFactory.class, FirestoreRpcFactory.class, builder, new FirestoreDefaults()); this.databaseId = builder.databaseId; + this.channelProvider = builder.channelProvider; + this.credentialsProvider = builder.credentialsProvider; this.timestampsInSnapshotsEnabled = builder.timestampsInSnapshotsEnabled; } private static class FirestoreDefaults implements ServiceDefaults { + private static final FirestoreDefaults INSTANCE = new FirestoreDefaults(); + + private final String HOST = FirestoreSettings.getDefaultEndpoint(); + private final String DATABASE_ID = "(default)"; + private final TransportOptions TRANSPORT_OPTIONS = getDefaultTransportOptionsBuilder().build(); + private final TransportChannelProvider CHANNEL_PROVIDER = + getDefaultTransportChannelProviderBuilder().build(); + private final CredentialsProvider CREDENTIALS_PROVIDER = + getDefaultCredentialsProviderBuilder().build(); + + @Nonnull + String getHost() { + return HOST; + } + + @Nonnull + String getDatabaseId() { + return DATABASE_ID; + } @Nonnull @Override @@ -179,13 +259,34 @@ public FirestoreRpcFactory getDefaultRpcFactory() { @Nonnull @Override public TransportOptions getDefaultTransportOptions() { - return getDefaultGrpcTransportOptions(); + return TRANSPORT_OPTIONS; } + + @Nonnull + TransportChannelProvider getDefaultTransportChannelProvider() { + return CHANNEL_PROVIDER; + } + + @Nonnull + CredentialsProvider getDefaultCredentialsProvider() { + return CREDENTIALS_PROVIDER; + } + } + + @Nonnull + public static GrpcTransportOptions.Builder getDefaultTransportOptionsBuilder() { + return GrpcTransportOptions.newBuilder(); } @Nonnull - public static GrpcTransportOptions getDefaultGrpcTransportOptions() { - return GrpcTransportOptions.newBuilder().build(); + public static InstantiatingGrpcChannelProvider.Builder + getDefaultTransportChannelProviderBuilder() { + return FirestoreSettings.defaultGrpcTransportProviderBuilder(); + } + + @Nonnull + public static GoogleCredentialsProvider.Builder getDefaultCredentialsProviderBuilder() { + return FirestoreSettings.defaultCredentialsProviderBuilder(); } /** @@ -206,13 +307,22 @@ FirestoreRpc getFirestoreRpc() { } @Override - public boolean equals(Object obj) { - return obj instanceof FirestoreOptions && baseEquals((FirestoreOptions) obj); + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + FirestoreOptions that = (FirestoreOptions) o; + return Objects.equals(databaseId, that.databaseId) + && Objects.equals(channelProvider, that.channelProvider) + && baseEquals(that); } @Override public int hashCode() { - return baseHashCode(); + return Objects.hash(databaseId, channelProvider, baseHashCode()); } @Nonnull diff --git a/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/spi/v1beta1/GrpcFirestoreRpc.java b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/spi/v1beta1/GrpcFirestoreRpc.java index 6bfb20e30891..074105d41274 100644 --- a/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/spi/v1beta1/GrpcFirestoreRpc.java +++ b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/spi/v1beta1/GrpcFirestoreRpc.java @@ -21,7 +21,6 @@ import com.google.api.gax.core.GaxProperties; import com.google.api.gax.grpc.GrpcCallContext; import com.google.api.gax.grpc.GrpcTransportChannel; -import com.google.api.gax.rpc.ApiClientHeaderProvider; import com.google.api.gax.rpc.BidiStreamingCallable; import com.google.api.gax.rpc.ClientContext; import com.google.api.gax.rpc.HeaderProvider; @@ -105,11 +104,8 @@ public GrpcFirestoreRpc(final FirestoreOptions options) throws IOException { DatabaseRootName databaseName = DatabaseRootName.of(options.getProjectId(), options.getDatabaseId()); - settingsBuilder.setCredentialsProvider( - GrpcTransportOptions.setUpCredentialsProvider(options)); - settingsBuilder.setTransportChannelProvider( - GrpcTransportOptions.setUpChannelProvider( - FirestoreSettings.defaultGrpcTransportProviderBuilder(), options)); + settingsBuilder.setCredentialsProvider(options.getCredentialsProvider()); + settingsBuilder.setTransportChannelProvider(options.getTransportChannelProvider()); HeaderProvider internalHeaderProvider = FirestoreSettings.defaultApiClientHeaderProviderBuilder()