Skip to content

Commit d7267f5

Browse files
mziccardgarrettjonesgoogle
authored andcommitted
Expose static method to get default project ID (#1380)
1 parent a599972 commit d7267f5

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

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
--------------

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) {

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)