From e1a84bafb7e7148ba9da82ecc6be88bc65d0713d Mon Sep 17 00:00:00 2001 From: Juan Lara Date: Tue, 11 Apr 2023 03:11:05 +0000 Subject: [PATCH] docs: add a snippet for regional endpoints. --- samples/snippets/pom.xml | 4 + .../snippets/RegionalEndpointSnippets.java | 46 ++++++++++ .../snippets/RegionalEndpointSnippetsIT.java | 86 +++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/firestore/snippets/RegionalEndpointSnippets.java create mode 100644 samples/snippets/src/test/java/com/example/firestore/snippets/RegionalEndpointSnippetsIT.java diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 83d134bd03..6cddc1f404 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -48,6 +48,10 @@ + + com.google.cloud + google-cloud-firestore-admin + org.apache.beam diff --git a/samples/snippets/src/main/java/com/example/firestore/snippets/RegionalEndpointSnippets.java b/samples/snippets/src/main/java/com/example/firestore/snippets/RegionalEndpointSnippets.java new file mode 100644 index 0000000000..43ac37826f --- /dev/null +++ b/samples/snippets/src/main/java/com/example/firestore/snippets/RegionalEndpointSnippets.java @@ -0,0 +1,46 @@ +/* + * Copyright 2023 Google Inc. + * + * 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.firestore.snippets; + +import com.google.auth.oauth2.GoogleCredentials; +import com.google.cloud.firestore.Firestore; +import com.google.cloud.firestore.FirestoreOptions; + + +/** + * Snippets to demonstrate how to set a regional endpoint. + */ +public class RegionalEndpointSnippets { + + /** + * Create a client with a regional endpoint. + **/ + public Firestore RegionalEndpoint(String projectId, String endpoint) throws Exception { + // [START firestore_regional_endpoint] + FirestoreOptions firestoreOptions = + FirestoreOptions.newBuilder() + .setProjectId(projectId) + .setCredentials(GoogleCredentials.getApplicationDefault()) + // set endpoint like nam5-firestore.googleapis.com:443 + .setHost(endpoint) + .build(); + Firestore dbWithEndpoint = firestoreOptions.getService(); + // [END firestore_regional_endpoint] + return dbWithEndpoint; + } + +} diff --git a/samples/snippets/src/test/java/com/example/firestore/snippets/RegionalEndpointSnippetsIT.java b/samples/snippets/src/test/java/com/example/firestore/snippets/RegionalEndpointSnippetsIT.java new file mode 100644 index 0000000000..f15ba01eff --- /dev/null +++ b/samples/snippets/src/test/java/com/example/firestore/snippets/RegionalEndpointSnippetsIT.java @@ -0,0 +1,86 @@ +/* + * Copyright 2023 Google Inc. + * + * 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.firestore.snippets; + +import static org.junit.Assert.assertNotNull; + +import com.example.firestore.BaseIntegrationTest; +import com.google.api.core.ApiFuture; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.cloud.firestore.DocumentReference; +import com.google.cloud.firestore.DocumentSnapshot; +import com.google.cloud.firestore.Firestore; +import com.google.cloud.firestore.FirestoreOptions; +import com.google.cloud.firestore.v1.FirestoreAdminClient; +import com.google.firestore.admin.v1.Database; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class RegionalEndpointSnippetsIT extends BaseIntegrationTest { + + private static RegionalEndpointSnippets regionalEndpointSnippets; + + private static FirestoreAdminClient adminClient; + + private static String getEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' must be set to perform these tests.", varName), + value); + return value; + } + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + regionalEndpointSnippets = new RegionalEndpointSnippets(); + + adminClient = FirestoreAdminClient.create(); + } + + @Test + public void testRegionalEndoint() throws Exception { + projectId = getEnvVar("FIRESTORE_PROJECT_ID"); + // Get database location of the default database in the project + Database dbInfo = adminClient.getDatabase("projects/" + projectId + "/databases/(default)"); + String locationId = dbInfo.getLocationId(); + + // Test regional endpoint with same location as default database + Firestore dbWithEndpoint = regionalEndpointSnippets.RegionalEndpoint(projectId, + locationId + "-firestore.googleapis.com:443"); + assertNotNull(dbWithEndpoint); + + // Retrieve a document to confirm client connection + DocumentReference docRef = dbWithEndpoint.collection("cities").document("SF"); + // asynchronously retrieve the document + ApiFuture future = docRef.get(); + assertNotNull(future); + + dbWithEndpoint.close(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + db.close(); + adminClient.close(); + } +}