Skip to content

Commit 793220e

Browse files
committed
Add a snippet for regional endpoints.
1 parent f1c2948 commit 793220e

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

samples/snippets/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
</dependency>
4949
<!-- [END firestore_install_with_bom] -->
5050

51+
<dependency>
52+
<groupId>com.google.cloud</groupId>
53+
<artifactId>google-cloud-firestore-admin</artifactId>
54+
</dependency>
5155
<!-- used for beam samples-->
5256
<dependency>
5357
<groupId>org.apache.beam</groupId>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2023 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.firestore.snippets;
18+
19+
import com.google.auth.oauth2.GoogleCredentials;
20+
import com.google.cloud.firestore.Firestore;
21+
import com.google.cloud.firestore.FirestoreOptions;
22+
23+
24+
/**
25+
* Snippets to demonstrate how to set a regional endpoint.
26+
*/
27+
public class RegionalEndpointSnippets {
28+
29+
/**
30+
* Create a client with a regional endpoint.
31+
**/
32+
public Firestore RegionalEndpoint(String projectId, String endpoint) throws Exception {
33+
// [START firestore_regional_endpoint]
34+
FirestoreOptions firestoreOptions =
35+
FirestoreOptions.newBuilder()
36+
.setProjectId(projectId)
37+
.setCredentials(GoogleCredentials.getApplicationDefault())
38+
// set endpoint like nam5-firestore.googleapis.com:443
39+
.setHost(endpoint)
40+
.build();
41+
Firestore dbWithEndpoint = firestoreOptions.getService();
42+
// [END firestore_regional_endpoint]
43+
return dbWithEndpoint;
44+
}
45+
46+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2023 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.firestore.snippets;
18+
19+
import static org.junit.Assert.assertNotNull;
20+
21+
import com.example.firestore.BaseIntegrationTest;
22+
import com.google.api.core.ApiFuture;
23+
import com.google.auth.oauth2.GoogleCredentials;
24+
import com.google.cloud.firestore.DocumentReference;
25+
import com.google.cloud.firestore.DocumentSnapshot;
26+
import com.google.cloud.firestore.Firestore;
27+
import com.google.cloud.firestore.FirestoreOptions;
28+
import com.google.cloud.firestore.v1.FirestoreAdminClient;
29+
import com.google.firestore.admin.v1.Database;
30+
import org.junit.AfterClass;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
36+
37+
@RunWith(JUnit4.class)
38+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
39+
public class RegionalEndpointSnippetsIT extends BaseIntegrationTest {
40+
41+
private static RegionalEndpointSnippets regionalEndpointSnippets;
42+
43+
private static FirestoreAdminClient adminClient;
44+
45+
private static String getEnvVar(String varName) {
46+
String value = System.getenv(varName);
47+
assertNotNull(
48+
String.format("Environment variable '%s' must be set to perform these tests.", varName),
49+
value);
50+
return value;
51+
}
52+
53+
@BeforeClass
54+
public static void setUpBeforeClass() throws Exception {
55+
regionalEndpointSnippets = new RegionalEndpointSnippets();
56+
57+
adminClient = FirestoreAdminClient.create();
58+
}
59+
60+
@Test
61+
public void testRegionalEndoint() throws Exception {
62+
projectId = getEnvVar("FIRESTORE_PROJECT_ID");
63+
// Get database location of the default database in the project
64+
Database dbInfo = adminClient.getDatabase("projects/" + projectId + "/databases/(default)");
65+
String locationId = dbInfo.getLocationId();
66+
67+
// Test regional endpoint with same location as default database
68+
Firestore dbWithEndpoint = regionalEndpointSnippets.RegionalEndpoint(projectId,
69+
locationId + "-firestore.googleapis.com:443");
70+
assertNotNull(dbWithEndpoint);
71+
72+
// Retrieve a document to confirm client connection
73+
DocumentReference docRef = dbWithEndpoint.collection("cities").document("SF");
74+
// asynchronously retrieve the document
75+
ApiFuture<DocumentSnapshot> future = docRef.get();
76+
assertNotNull(future);
77+
78+
dbWithEndpoint.close();
79+
}
80+
81+
@AfterClass
82+
public static void tearDownAfterClass() throws Exception {
83+
db.close();
84+
adminClient.close();
85+
}
86+
}

0 commit comments

Comments
 (0)