Skip to content

Commit 325d4cd

Browse files
committed
Expose static methods to get the default project ID and credentials
1 parent 4d5a86c commit 325d4cd

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public B clock(Clock clock) {
149149
}
150150

151151
/**
152-
* Sets project id.
152+
* Sets project ID.
153153
*
154154
* @return the builder
155155
*/
@@ -235,7 +235,18 @@ protected boolean projectIdRequired() {
235235
return true;
236236
}
237237

238-
private static AuthCredentials defaultAuthCredentials() {
238+
/**
239+
* Returns the default authentication credentials, or {@code null} if no default credentials could
240+
* be found. This method returns the first available credentials among the following sources:
241+
* <ol>
242+
* <li>App Engine credentials
243+
* <li>JSON credentials pointed to by the {@code GOOGLE_APPLICATION_CREDENTIALS} environment
244+
* variable
245+
* <li>Google Cloud SDK credentials
246+
* <li>Compute Engine credentials
247+
* </ol>
248+
*/
249+
public static AuthCredentials defaultAuthCredentials() {
239250
// Consider App Engine.
240251
if (appEngineAppId() != null) {
241252
try {
@@ -261,6 +272,21 @@ protected String defaultHost() {
261272
}
262273

263274
protected String defaultProject() {
275+
return defaultProjectId();
276+
}
277+
278+
/**
279+
* Returns the default project ID, or {@code null} if no default project ID could be found. This
280+
* method returns the first available project ID among the following sources:
281+
* <ol>
282+
* <li>App Engine project ID
283+
* <li>Project ID specified in the JSON credentials file pointed by the
284+
* {@code GOOGLE_APPLICATION_CREDENTIALS} environment variable
285+
* <li>Google Cloud SDK project ID
286+
* <li>Compute Engine project ID
287+
* </ol>
288+
*/
289+
public static String defaultProjectId() {
264290
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
265291
if (projectId == null) {
266292
projectId =
@@ -402,8 +428,8 @@ public ServiceRpcT rpc() {
402428
}
403429

404430
/**
405-
* Returns the project id. Return value can be null (for services that don't require a project
406-
* id).
431+
* Returns the project ID. Return value can be null (for services that don't require a project
432+
* ID).
407433
*/
408434
public String projectId() {
409435
return projectId;

0 commit comments

Comments
 (0)