Skip to content

Commit 0bca75c

Browse files
lqiu96blakeli0gemini-code-assist[bot]jinseopkim0
authored
feat(datastore): Remove deprecated classes and methods and bump ObsoleteApi to Deprecated (#12971)
In preparation of the V3 major version bump --------- Co-authored-by: Blake Li <blakeli@google.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Jin Seop Kim <jinseop@google.com>
1 parent cd57169 commit 0bca75c

15 files changed

Lines changed: 130 additions & 313 deletions

File tree

java-datastore/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/DatastoreEmulator.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.google.datastore.v1.client;
1717

18-
import static com.google.api.client.util.Preconditions.checkNotNull;
1918
import static com.google.api.client.util.Preconditions.checkState;
2019

2120
import java.io.BufferedReader;
@@ -102,37 +101,6 @@ public void clear() throws DatastoreEmulatorException {
102101
sendEmptyRequest("/reset", "POST");
103102
}
104103

105-
/**
106-
* Starts the emulator. It is the caller's responsibility to call {@link #stop}. Note that
107-
* receiving an exception does not indicate that the server did not start. We recommend calling
108-
* {@link #stop} to ensure the server is not running regardless of the result of this method.
109-
*
110-
* @param emulatorDir The path to the emulator directory, e.g. /usr/local/cloud-datastore-emulator
111-
* @param projectId The project ID
112-
* @param commandLineOptions Command line options to pass to the emulator on startup
113-
* @throws DatastoreEmulatorException If {@link #start} has already been called or the server does
114-
* not start successfully.
115-
* @deprecated prefer setting options in the emulator options and calling {#start()}.
116-
*/
117-
@Deprecated
118-
public synchronized void start(String emulatorDir, String projectId, String... commandLineOptions)
119-
throws DatastoreEmulatorException {
120-
checkNotNull(emulatorDir, "emulatorDir cannot be null");
121-
checkNotNull(projectId, "projectId cannot be null");
122-
checkState(state == State.NEW, "Cannot call start() more than once.");
123-
try {
124-
startEmulatorInternal(
125-
emulatorDir + "/cloud_datastore_emulator", projectId, Arrays.asList(commandLineOptions));
126-
state = State.STARTED;
127-
} finally {
128-
if (state != State.STARTED) {
129-
// If we're not able to start the server we don't want people trying again. Just move it
130-
// straight to the STOPPED state.
131-
state = State.STOPPED;
132-
}
133-
}
134-
}
135-
136104
public synchronized void start() throws DatastoreEmulatorException {
137105
checkState(state == State.NEW, "Cannot call start() more than once.");
138106
try {

java-datastore/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/DatastoreHelper.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ private static GoogleCredential.Builder getCredentialBuilderWithoutPrivateKey(
219219
* <li>Google Application Default as described <a
220220
* href="https://cloud.google.com/docs/authentication/production">here</a>.
221221
* </ol>
222+
*
223+
* <p><b>Warning:</b> The <code>__DATASTORE_URL_OVERRIDE</code> environment variable is not
224+
* well-supported or documented. To maintain legacy behavior, the URL must include a scheme (e.g.
225+
* <code>http://localhost:8080</code>). Omitting the scheme will result in an invalid endpoint
226+
* URL.
227+
*
228+
* <p>Users should instead use the standard and supported <code>DATASTORE_EMULATOR_HOST</code>
229+
* environment variable (e.g., <code>localhost:8080</code>) to connect to local emulators.
222230
*/
223231
public static DatastoreOptions.Builder getOptionsFromEnv()
224232
throws GeneralSecurityException, IOException {
@@ -322,9 +330,41 @@ private static void setProjectEndpointFromEnv(DatastoreOptions.Builder options)
322330
LOCAL_HOST_ENV_VAR));
323331
}
324332
String projectId = getProjectIdFromEnv();
325-
if (System.getenv(URL_OVERRIDE_ENV_VAR) != null) {
326-
options.projectEndpoint(
327-
String.format("%s/projects/%s", System.getenv(URL_OVERRIDE_ENV_VAR), projectId));
333+
String urlOverride = System.getenv(URL_OVERRIDE_ENV_VAR);
334+
if (urlOverride != null) {
335+
if (!urlOverride.startsWith("http://") && !urlOverride.startsWith("https://")) {
336+
throw new IllegalArgumentException(
337+
String.format("Project endpoint \"%s\" must include scheme.", urlOverride));
338+
}
339+
logger.warning(
340+
String.format(
341+
"The environment variable %s is not well-supported or documented. "
342+
+ "Consider using the standard %s environment variable instead. "
343+
+ "See https://docs.cloud.google.com/datastore/docs/tools/datastore-emulator",
344+
URL_OVERRIDE_ENV_VAR, LOCAL_HOST_ENV_VAR));
345+
options.projectId(projectId);
346+
// To maintain legacy behavior for undocumented overrides, the URL override must include a
347+
// scheme (e.g., http://).
348+
// Since host and localHost methods don't accept a scheme, we strip it if present.
349+
// We then check if it's an HTTP or HTTPS URL to use options.localHost(...) or
350+
// options.host(...) accordingly. We use options.localHost(...) for all HTTP URLs
351+
// (not just localhost or 127.0.0.1) because the emulator might be running on a
352+
// different host in containerized or CI environments.
353+
String host = urlOverride;
354+
boolean isHttp = host.startsWith("http://");
355+
if (isHttp) {
356+
host = host.substring("http://".length());
357+
} else if (host.startsWith("https://")) {
358+
host = host.substring("https://".length());
359+
}
360+
if (host.endsWith("/")) {
361+
host = host.substring(0, host.length() - 1);
362+
}
363+
if (isHttp) {
364+
options.localHost(host);
365+
} else {
366+
options.host(host);
367+
}
328368
return;
329369
}
330370
if (System.getenv(LOCAL_HOST_ENV_VAR) != null) {
@@ -333,7 +373,6 @@ private static void setProjectEndpointFromEnv(DatastoreOptions.Builder options)
333373
return;
334374
}
335375
options.projectId(projectId);
336-
return;
337376
}
338377

339378
/**

java-datastore/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/DatastoreOptions.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,6 @@ public Builder localHost(String localHost) {
132132
return this;
133133
}
134134

135-
/**
136-
* Sets the project endpoint used to access Cloud Datastore. Prefer using {@link #projectId}
137-
* and/or {@link #host}/{@link #localHost} when possible.
138-
*
139-
* @deprecated Use {@link #projectId} and/or {@link #host}/{@link #localHost} instead.
140-
*/
141-
@Deprecated
142-
public Builder projectEndpoint(String projectEndpoint) {
143-
checkArgument(projectId == null, PROJECT_ENDPOINT_AND_PROJECT_ID_ERROR);
144-
checkArgument(localHost == null && host == null, PROJECT_ENDPOINT_AND_HOST_ERROR);
145-
if (!includesScheme(projectEndpoint)) {
146-
throw new IllegalArgumentException(
147-
String.format("Project endpoint \"%s\" must include scheme.", projectEndpoint));
148-
}
149-
this.projectEndpoint = projectEndpoint;
150-
return this;
151-
}
152-
153135
/** Sets the (optional) initializer to run on HTTP requests to Cloud Datastore. */
154136
public Builder initializer(HttpRequestInitializer initializer) {
155137
this.initializer = initializer;

java-datastore/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/DatastoreClientTest.java

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -76,51 +76,6 @@ public void options_NoProjectIdOrProjectEndpoint() {
7676
factory.create(options.build());
7777
}
7878

79-
@Test
80-
public void options_ProjectIdAndProjectEndpoint() throws Exception {
81-
IllegalArgumentException exception =
82-
assertThrows(
83-
IllegalArgumentException.class,
84-
() ->
85-
new DatastoreOptions.Builder()
86-
.projectId(PROJECT_ID)
87-
.projectEndpoint(
88-
"http://localhost:1234/datastore/v1beta42/projects/project-id"));
89-
assertThat(exception)
90-
.hasMessageThat()
91-
.contains("Cannot set both project endpoint and project ID");
92-
}
93-
94-
@Test
95-
public void options_LocalHostAndProjectEndpoint() throws Exception {
96-
IllegalArgumentException exception =
97-
assertThrows(
98-
IllegalArgumentException.class,
99-
() ->
100-
new DatastoreOptions.Builder()
101-
.localHost("localhost:8080")
102-
.projectEndpoint(
103-
"http://localhost:1234/datastore/v1beta42/projects/project-id"));
104-
assertThat(exception)
105-
.hasMessageThat()
106-
.contains("Can set at most one of project endpoint, host, and local host");
107-
}
108-
109-
@Test
110-
public void options_HostAndProjectEndpoint() throws Exception {
111-
IllegalArgumentException exception =
112-
assertThrows(
113-
IllegalArgumentException.class,
114-
() ->
115-
new DatastoreOptions.Builder()
116-
.host("foo-datastore.googleapis.com")
117-
.projectEndpoint(
118-
"http://localhost:1234/datastore/v1beta42/projects/project-id"));
119-
assertThat(exception)
120-
.hasMessageThat()
121-
.contains("Can set at most one of project endpoint, host, and local host");
122-
}
123-
12479
@Test
12580
public void options_HostAndLocalHost() throws Exception {
12681
IllegalArgumentException exception =
@@ -235,34 +190,6 @@ public void create_DefaultHost() {
235190
.isEqualTo("https://datastore.googleapis.com/v1/projects/project-id");
236191
}
237192

238-
@Test
239-
public void create_ProjectEndpoint() {
240-
Datastore datastore =
241-
factory.create(
242-
new DatastoreOptions.Builder()
243-
.projectEndpoint("http://prom-qa/datastore/v1beta42/projects/project-id")
244-
.build());
245-
assertThat(datastore.remoteRpc.getUrl())
246-
.isEqualTo("http://prom-qa/datastore/v1beta42/projects/project-id");
247-
}
248-
249-
@Test
250-
public void create_ProjectEndpointNoScheme() {
251-
IllegalArgumentException exception =
252-
assertThrows(
253-
IllegalArgumentException.class,
254-
() ->
255-
factory.create(
256-
new DatastoreOptions.Builder()
257-
.projectEndpoint("localhost:1234/datastore/v1beta42/projects/project-id")
258-
.build()));
259-
assertThat(exception)
260-
.hasMessageThat()
261-
.contains(
262-
"Project endpoint \"localhost:1234/datastore/v1beta42/projects/project-id\" must"
263-
+ " include scheme.");
264-
}
265-
266193
@Test
267194
public void initializer() throws Exception {
268195
options.initializer(

java-datastore/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/DatastoreEmulatorTest.java

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,11 @@
2626
@RunWith(JUnit4.class)
2727
public class DatastoreEmulatorTest {
2828

29-
private static final DatastoreEmulatorOptions options =
30-
new DatastoreEmulatorOptions.Builder().build();
31-
32-
@Test
33-
public void testArgs() throws DatastoreEmulatorException {
34-
DatastoreEmulator datastore =
35-
new DatastoreEmulator(null, "blar", options) {
36-
@Override
37-
void startEmulatorInternal(
38-
String emulatorDir, String projectId, List<String> cmdLineOpts) {
39-
// no-op for testing
40-
}
41-
};
42-
43-
try {
44-
datastore.start(null, "projectId");
45-
fail("expected exception");
46-
} catch (NullPointerException npe) {
47-
// good
48-
}
49-
50-
try {
51-
datastore.start("path/to/emulator", null);
52-
fail("expected exception");
53-
} catch (NullPointerException npe) {
54-
// good
55-
}
56-
57-
datastore.start("path/to/emulator", "projectId");
58-
}
59-
6029
@Test
6130
public void testLifecycle() throws DatastoreEmulatorException {
31+
DatastoreEmulatorOptions options =
32+
new DatastoreEmulatorOptions.Builder().setCommand("/yar").setProjectId("myproject").build();
33+
6234
DatastoreEmulator datastore =
6335
new DatastoreEmulator(null, "blar", options) {
6436
@Override
@@ -73,12 +45,9 @@ protected void stopEmulatorInternal() {
7345
}
7446
};
7547

76-
String emulatorDir = "/yar";
77-
String myProject = "myproject";
78-
79-
datastore.start(emulatorDir, myProject);
48+
datastore.start();
8049
try {
81-
datastore.start(emulatorDir, myProject);
50+
datastore.start();
8251
fail("expected exception");
8352
} catch (IllegalStateException e) {
8453
// good
@@ -90,7 +59,7 @@ protected void stopEmulatorInternal() {
9059

9160
// Once we've stopped we can't start again.
9261
try {
93-
datastore.start(emulatorDir, myProject);
62+
datastore.start();
9463
fail("expected exception");
9564
} catch (IllegalStateException e) {
9665
// good

java-datastore/google-cloud-datastore-utils/src/main/java/com/google/datastore/utils/DatastoreHelper.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ private static GoogleCredential.Builder getCredentialBuilderWithoutPrivateKey(
221221
* <li>Google Application Default as described <a
222222
* href="https://cloud.google.com/docs/authentication/production">here</a>.
223223
* </ol>
224+
*
225+
* <p><b>Warning:</b> The <code>__DATASTORE_URL_OVERRIDE</code> environment variable is not
226+
* well-supported or documented. To maintain legacy behavior, the URL must include a scheme (e.g.
227+
* <code>http://localhost:8080</code>). Omitting the scheme will result in an invalid endpoint
228+
* URL.
229+
*
230+
* <p>Users should instead use the standard and supported <code>DATASTORE_EMULATOR_HOST</code>
231+
* environment variable (e.g., <code>localhost:8080</code>) to connect to local emulators.
224232
*/
225233
public static DatastoreOptions.Builder getOptionsFromEnv()
226234
throws GeneralSecurityException, IOException {
@@ -324,9 +332,41 @@ private static void setProjectEndpointFromEnv(DatastoreOptions.Builder options)
324332
LOCAL_HOST_ENV_VAR));
325333
}
326334
String projectId = getProjectIdFromEnv();
327-
if (System.getenv(URL_OVERRIDE_ENV_VAR) != null) {
328-
options.projectEndpoint(
329-
String.format("%s/projects/%s", System.getenv(URL_OVERRIDE_ENV_VAR), projectId));
335+
String urlOverride = System.getenv(URL_OVERRIDE_ENV_VAR);
336+
if (urlOverride != null) {
337+
if (!urlOverride.startsWith("http://") && !urlOverride.startsWith("https://")) {
338+
throw new IllegalArgumentException(
339+
String.format("Project endpoint \"%s\" must include scheme.", urlOverride));
340+
}
341+
logger.warning(
342+
String.format(
343+
"The environment variable %s is not well-supported or documented. "
344+
+ "Consider using the standard %s environment variable instead. "
345+
+ "See https://docs.cloud.google.com/datastore/docs/tools/datastore-emulator",
346+
URL_OVERRIDE_ENV_VAR, LOCAL_HOST_ENV_VAR));
347+
options.projectId(projectId);
348+
// To maintain legacy behavior for undocumented overrides, the URL override must include a
349+
// scheme (e.g., http://).
350+
// Since host and localHost methods don't accept a scheme, we strip it if present.
351+
// We then check if it's an HTTP or HTTPS URL to use options.localHost(...) or
352+
// options.host(...) accordingly. We use options.localHost(...) for all HTTP URLs
353+
// (not just localhost or 127.0.0.1) because the emulator might be running on a
354+
// different host in containerized or CI environments.
355+
String host = urlOverride;
356+
boolean isHttp = host.startsWith("http://");
357+
if (isHttp) {
358+
host = host.substring("http://".length());
359+
} else if (host.startsWith("https://")) {
360+
host = host.substring("https://".length());
361+
}
362+
if (host.endsWith("/")) {
363+
host = host.substring(0, host.length() - 1);
364+
}
365+
if (isHttp) {
366+
options.localHost(host);
367+
} else {
368+
options.host(host);
369+
}
330370
return;
331371
}
332372
if (System.getenv(LOCAL_HOST_ENV_VAR) != null) {
@@ -335,7 +375,6 @@ private static void setProjectEndpointFromEnv(DatastoreOptions.Builder options)
335375
return;
336376
}
337377
options.projectId(projectId);
338-
return;
339378
}
340379

341380
/**

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,6 @@ public Builder localHost(String localHost) {
133133
return this;
134134
}
135135

136-
/**
137-
* Sets the project endpoint used to access Cloud Datastore. Prefer using {@link #projectId}
138-
* and/or {@link #host}/{@link #localHost} when possible.
139-
*
140-
* @deprecated Use {@link #projectId} and/or {@link #host}/{@link #localHost} instead.
141-
*/
142-
@Deprecated
143-
public Builder projectEndpoint(String projectEndpoint) {
144-
checkArgument(projectId == null, PROJECT_ENDPOINT_AND_PROJECT_ID_ERROR);
145-
checkArgument(localHost == null && host == null, PROJECT_ENDPOINT_AND_HOST_ERROR);
146-
if (!includesScheme(projectEndpoint)) {
147-
throw new IllegalArgumentException(
148-
String.format("Project endpoint \"%s\" must include scheme.", projectEndpoint));
149-
}
150-
this.projectEndpoint = projectEndpoint;
151-
return this;
152-
}
153-
154136
/** Sets the (optional) initializer to run on HTTP requests to Cloud Datastore. */
155137
public Builder initializer(HttpRequestInitializer initializer) {
156138
this.initializer = initializer;

0 commit comments

Comments
 (0)