Skip to content

Commit

Permalink
Allow Firestore project to be configurable (#31808)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amar3tto authored Jul 10, 2024
1 parent 6c829db commit 4df89c7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.beam.sdk.io.gcp.firestore;

import org.apache.beam.sdk.extensions.gcp.options.GcpOptions;
import org.apache.beam.sdk.options.Default;
import org.apache.beam.sdk.options.Description;
import org.apache.beam.sdk.options.PipelineOptions;
Expand Down Expand Up @@ -75,4 +76,14 @@ public interface FirestoreOptions extends PipelineOptions {
* @param host the host and port to connect to
*/
void setFirestoreHost(String host);

/** The Firestore project ID to connect to. */
@Description("Firestore project ID")
@Nullable
String getFirestoreProject();

/**
* Set the Firestore project ID, it will override the value from {@link GcpOptions#getProject()}.
*/
void setFirestoreProject(String firestoreProject);
}
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,14 @@ public void setup() {
/** {@inheritDoc} */
@Override
public final void startBundle(StartBundleContext c) {
String project = c.getPipelineOptions().as(GcpOptions.class).getProject();
String project = c.getPipelineOptions().as(FirestoreOptions.class).getFirestoreProject();
if (project == null) {
project = c.getPipelineOptions().as(GcpOptions.class).getProject();
}
projectId =
requireNonNull(project, "project must be defined on GcpOptions of PipelineOptions");
requireNonNull(
project,
"project must be defined on FirestoreOptions or GcpOptions of PipelineOptions");
firestoreStub = firestoreStatefulComponentFactory.getFirestoreStub(c.getPipelineOptions());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,16 @@ public void setup() {

@Override
public final void startBundle(StartBundleContext c) {
String project = c.getPipelineOptions().as(GcpOptions.class).getProject();
String project = c.getPipelineOptions().as(FirestoreOptions.class).getFirestoreProject();
if (project == null) {
project = c.getPipelineOptions().as(GcpOptions.class).getProject();
}
String databaseId = c.getPipelineOptions().as(FirestoreOptions.class).getFirestoreDb();
databaseRootName =
DatabaseRootName.of(
requireNonNull(project, "project must be defined on GcpOptions of PipelineOptions"),
requireNonNull(
project,
"project must be defined on FirestoreOptions or GcpOptions of PipelineOptions"),
requireNonNull(
databaseId,
"firestoreDb must be defined on FirestoreOptions of PipelineOptions"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.apache.beam.sdk.io.gcp.firestore.it;

import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.MoreObjects.firstNonNull;

import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
Expand Down Expand Up @@ -132,7 +134,8 @@ public FirestoreTestingHelper(CleanupMode cleanupMode) {
firestoreOptions =
FirestoreOptions.newBuilder()
.setCredentials(gcpOptions.getGcpCredential())
.setProjectId(gcpOptions.getProject())
.setProjectId(
firstNonNull(firestoreBeamOptions.getFirestoreProject(), gcpOptions.getProject()))
.setDatabaseId(firestoreBeamOptions.getFirestoreDb())
.setHost(firestoreBeamOptions.getFirestoreHost())
.build();
Expand Down

0 comments on commit 4df89c7

Please sign in to comment.