From 325d4cd4331f36328ca986ee5301ebcaaed9c3be Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 12 Sep 2016 16:39:26 +0200 Subject: [PATCH] Expose static methods to get the default project ID and credentials --- .../java/com/google/cloud/ServiceOptions.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java index acd1611797f7..d19faa88e457 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -149,7 +149,7 @@ public B clock(Clock clock) { } /** - * Sets project id. + * Sets project ID. * * @return the builder */ @@ -235,7 +235,18 @@ protected boolean projectIdRequired() { return true; } - private static AuthCredentials defaultAuthCredentials() { + /** + * Returns the default authentication credentials, or {@code null} if no default credentials could + * be found. This method returns the first available credentials among the following sources: + *
    + *
  1. App Engine credentials + *
  2. JSON credentials pointed to by the {@code GOOGLE_APPLICATION_CREDENTIALS} environment + * variable + *
  3. Google Cloud SDK credentials + *
  4. Compute Engine credentials + *
+ */ + public static AuthCredentials defaultAuthCredentials() { // Consider App Engine. if (appEngineAppId() != null) { try { @@ -261,6 +272,21 @@ protected String defaultHost() { } protected String defaultProject() { + return defaultProjectId(); + } + + /** + * Returns the default project ID, or {@code null} if no default project ID could be found. This + * method returns the first available project ID among the following sources: + *
    + *
  1. App Engine project ID + *
  2. Project ID specified in the JSON credentials file pointed by the + * {@code GOOGLE_APPLICATION_CREDENTIALS} environment variable + *
  3. Google Cloud SDK project ID + *
  4. Compute Engine project ID + *
+ */ + public static String defaultProjectId() { String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME)); if (projectId == null) { projectId = @@ -402,8 +428,8 @@ public ServiceRpcT rpc() { } /** - * Returns the project id. Return value can be null (for services that don't require a project - * id). + * Returns the project ID. Return value can be null (for services that don't require a project + * ID). */ public String projectId() { return projectId;