Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose static method to get default project ID #1380

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Expose static method to get default project ID
  • Loading branch information
mziccard committed Nov 9, 2016
commit ac5360bc249fb733d7a6df76af742668ddb2f73f
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -105,12 +105,12 @@ Most `google-cloud` libraries require a project ID. There are multiple ways to

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

1. Project ID supplied when building the service options
1. The project ID supplied when building the service options
2. Project ID specified by the environment variable `GOOGLE_CLOUD_PROJECT`
3. App Engine project ID
4. Project ID specified in the JSON credentials file pointed by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable
5. Google Cloud SDK project ID
6. Compute Engine project ID
3. The App Engine project ID
4. The project ID specified in the JSON credentials file pointed by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable
5. The Google Cloud SDK project ID
6. The Compute Engine project ID

Authentication
--------------
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ public static class DefaultHttpTransportFactory implements HttpTransportFactory
@Override
public HttpTransport create() {
// Consider App Engine
if (appEngineAppId() != null) {
if (getAppEngineAppId() != null) {
try {
return new UrlFetchTransport();
} catch (Exception ignore) {
Original file line number Diff line number Diff line change
@@ -171,7 +171,8 @@ public B setClock(Clock clock) {
}

/**
* Sets project id.
* Sets the project ID. If no project ID is set, {@link #getDefaultProjectId()} will be used to
* attempt getting the project ID from the environment.
*
* @return the builder
*/
@@ -181,7 +182,8 @@ public B projectId(String projectId) {
}

/**
* Sets project id.
* Sets the project ID. If no project ID is set, {@link #getDefaultProjectId()} will be used to
* attempt getting the project ID from the environment.
*
* @return the builder
*/
@@ -311,10 +313,6 @@ private static GoogleCredentials defaultCredentials() {
}
}

protected static String appEngineAppId() {
return System.getProperty("com.google.appengine.application.id");
}

@Deprecated
protected String defaultHost() {
return getDefaultHost();
@@ -330,21 +328,41 @@ protected String defaultProject() {
}

protected String getDefaultProject() {
return getDefaultProjectId();
}

/**
* 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:
* <ol>
* <li>The project ID specified by the GOOGLE_CLOUD_PROJECT environment variable
* <li>The App Engine project ID
* <li>The project ID specified in the JSON credentials file pointed by the
* {@code GOOGLE_APPLICATION_CREDENTIALS} environment variable
* <li>The Google Cloud SDK project ID
* <li>The Compute Engine project ID
* </ol>
*/
public static String getDefaultProjectId() {
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
if (projectId == null) {
projectId =
System.getProperty(LEGACY_PROJECT_ENV_NAME, System.getenv(LEGACY_PROJECT_ENV_NAME));
}
if (projectId == null) {
projectId = appEngineProjectId();
projectId = getAppEngineProjectId();
}
if (projectId == null) {
projectId = serviceAccountProjectId();
projectId = getServiceAccountProjectId();
}
return projectId != null ? projectId : googleCloudProjectId();
return projectId != null ? projectId : getGoogleCloudProjectId();
}

protected static String getAppEngineAppId() {
return System.getProperty("com.google.appengine.application.id");
}

private static String activeGoogleCloudConfig(File configDir) {
private static String getActiveGoogleCloudConfig(File configDir) {
String activeGoogleCloudConfig = null;
try {
activeGoogleCloudConfig =
@@ -356,7 +374,7 @@ private static String activeGoogleCloudConfig(File configDir) {
return firstNonNull(activeGoogleCloudConfig, "default");
}

protected static String googleCloudProjectId() {
protected static String getGoogleCloudProjectId() {
File configDir;
if (System.getenv().containsKey("CLOUDSDK_CONFIG")) {
configDir = new File(System.getenv("CLOUDSDK_CONFIG"));
@@ -365,7 +383,7 @@ protected static String googleCloudProjectId() {
} else {
configDir = new File(System.getProperty("user.home"), ".config/gcloud");
}
String activeConfig = activeGoogleCloudConfig(configDir);
String activeConfig = getActiveGoogleCloudConfig(configDir);
FileReader fileReader = null;
try {
fileReader = new FileReader(new File(configDir, "configurations/config_" + activeConfig));
@@ -422,7 +440,7 @@ private static boolean isWindows() {
return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");
}

protected static String appEngineProjectId() {
protected static String getAppEngineProjectId() {
try {
Class<?> factoryClass =
Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory");
@@ -440,7 +458,7 @@ protected static String appEngineProjectId() {
}
}

protected static String serviceAccountProjectId() {
protected static String getServiceAccountProjectId() {
String project = null;
String credentialsPath = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
if (credentialsPath != null) {
@@ -481,17 +499,17 @@ public ServiceRpcT getRpc() {
}

/**
* 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).
*/
@Deprecated
public String projectId() {
return getProjectId();
}

/**
* 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 getProjectId() {
return projectId;