Skip to content

Commit 5154fd5

Browse files
mziccardgarrettjonesgoogle
authored andcommitted
---
yaml --- r: 7849 b: refs/heads/tswast-patch-1 c: d7267f5 h: refs/heads/master i: 7847: 56e9d92
1 parent d23c56d commit 5154fd5

4 files changed

Lines changed: 43 additions & 25 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: a5999728f55f3657482a9b6fa671d432be56989f
60+
refs/heads/tswast-patch-1: d7267f56e7c641f5f758fc0186767e3c3d6a1559
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ Most `google-cloud` libraries require a project ID. There are multiple ways to
109109

110110
`google-cloud` determines the project ID from the following sources in the listed order, stopping once it finds a value:
111111

112-
1. Project ID supplied when building the service options
112+
1. The project ID supplied when building the service options
113113
2. Project ID specified by the environment variable `GOOGLE_CLOUD_PROJECT`
114-
3. App Engine project ID
115-
4. Project ID specified in the JSON credentials file pointed by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable
116-
5. Google Cloud SDK project ID
117-
6. Compute Engine project ID
114+
3. The App Engine project ID
115+
4. The project ID specified in the JSON credentials file pointed by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable
116+
5. The Google Cloud SDK project ID
117+
6. The Compute Engine project ID
118118

119119
Authentication
120120
--------------

branches/tswast-patch-1/google-cloud-core/src/main/java/com/google/cloud/HttpServiceOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static class DefaultHttpTransportFactory implements HttpTransportFactory
5858
@Override
5959
public HttpTransport create() {
6060
// Consider App Engine
61-
if (appEngineAppId() != null) {
61+
if (getAppEngineAppId() != null) {
6262
try {
6363
return new UrlFetchTransport();
6464
} catch (Exception ignore) {

branches/tswast-patch-1/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public B setClock(Clock clock) {
176176
}
177177

178178
/**
179-
* Sets project id.
179+
* Sets the project ID. If no project ID is set, {@link #getDefaultProjectId()} will be used to
180+
* attempt getting the project ID from the environment.
180181
*
181182
* @return the builder
182183
*/
@@ -186,7 +187,8 @@ public B projectId(String projectId) {
186187
}
187188

188189
/**
189-
* Sets project id.
190+
* Sets the project ID. If no project ID is set, {@link #getDefaultProjectId()} will be used to
191+
* attempt getting the project ID from the environment.
190192
*
191193
* @return the builder
192194
*/
@@ -316,10 +318,6 @@ private static GoogleCredentials defaultCredentials() {
316318
}
317319
}
318320

319-
protected static String appEngineAppId() {
320-
return System.getProperty("com.google.appengine.application.id");
321-
}
322-
323321
@Deprecated
324322
protected String defaultHost() {
325323
return getDefaultHost();
@@ -335,21 +333,41 @@ protected String defaultProject() {
335333
}
336334

337335
protected String getDefaultProject() {
336+
return getDefaultProjectId();
337+
}
338+
339+
/**
340+
* Returns the default project ID, or {@code null} if no default project ID could be found. This
341+
* method returns the first available project ID among the following sources:
342+
* <ol>
343+
* <li>The project ID specified by the GOOGLE_CLOUD_PROJECT environment variable
344+
* <li>The App Engine project ID
345+
* <li>The project ID specified in the JSON credentials file pointed by the
346+
* {@code GOOGLE_APPLICATION_CREDENTIALS} environment variable
347+
* <li>The Google Cloud SDK project ID
348+
* <li>The Compute Engine project ID
349+
* </ol>
350+
*/
351+
public static String getDefaultProjectId() {
338352
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
339353
if (projectId == null) {
340354
projectId =
341355
System.getProperty(LEGACY_PROJECT_ENV_NAME, System.getenv(LEGACY_PROJECT_ENV_NAME));
342356
}
343357
if (projectId == null) {
344-
projectId = appEngineProjectId();
358+
projectId = getAppEngineProjectId();
345359
}
346360
if (projectId == null) {
347-
projectId = serviceAccountProjectId();
361+
projectId = getServiceAccountProjectId();
348362
}
349-
return projectId != null ? projectId : googleCloudProjectId();
363+
return projectId != null ? projectId : getGoogleCloudProjectId();
364+
}
365+
366+
protected static String getAppEngineAppId() {
367+
return System.getProperty("com.google.appengine.application.id");
350368
}
351369

352-
private static String activeGoogleCloudConfig(File configDir) {
370+
private static String getActiveGoogleCloudConfig(File configDir) {
353371
String activeGoogleCloudConfig = null;
354372
try {
355373
activeGoogleCloudConfig =
@@ -361,7 +379,7 @@ private static String activeGoogleCloudConfig(File configDir) {
361379
return firstNonNull(activeGoogleCloudConfig, "default");
362380
}
363381

364-
protected static String googleCloudProjectId() {
382+
protected static String getGoogleCloudProjectId() {
365383
File configDir;
366384
if (System.getenv().containsKey("CLOUDSDK_CONFIG")) {
367385
configDir = new File(System.getenv("CLOUDSDK_CONFIG"));
@@ -370,7 +388,7 @@ protected static String googleCloudProjectId() {
370388
} else {
371389
configDir = new File(System.getProperty("user.home"), ".config/gcloud");
372390
}
373-
String activeConfig = activeGoogleCloudConfig(configDir);
391+
String activeConfig = getActiveGoogleCloudConfig(configDir);
374392
FileReader fileReader = null;
375393
try {
376394
fileReader = new FileReader(new File(configDir, "configurations/config_" + activeConfig));
@@ -429,7 +447,7 @@ private static boolean isWindows() {
429447
return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");
430448
}
431449

432-
protected static String appEngineProjectId() {
450+
protected static String getAppEngineProjectId() {
433451
try {
434452
Class<?> factoryClass =
435453
Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory");
@@ -447,7 +465,7 @@ protected static String appEngineProjectId() {
447465
}
448466
}
449467

450-
protected static String serviceAccountProjectId() {
468+
protected static String getServiceAccountProjectId() {
451469
String project = null;
452470
String credentialsPath = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
453471
if (credentialsPath != null) {
@@ -488,17 +506,17 @@ public ServiceRpcT getRpc() {
488506
}
489507

490508
/**
491-
* Returns the project id. Return value can be null (for services that don't require a project
492-
* id).
509+
* Returns the project ID. Return value can be null (for services that don't require a project
510+
* ID).
493511
*/
494512
@Deprecated
495513
public String projectId() {
496514
return getProjectId();
497515
}
498516

499517
/**
500-
* Returns the project id. Return value can be null (for services that don't require a project
501-
* id).
518+
* Returns the project ID. Return value can be null (for services that don't require a project
519+
* ID).
502520
*/
503521
public String getProjectId() {
504522
return projectId;

0 commit comments

Comments
 (0)