-
Notifications
You must be signed in to change notification settings - Fork 66
feat: Add Universe Domain to Java-Core #2329
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
Changes from 13 commits
0397cc3
9ba6313
b0e7614
6cc86ac
c68e996
2c23732
db4965a
3d63297
5f8d48f
881b5c1
8fcf832
08bbe56
8e878f9
acb94a8
f4eaad3
6709f2c
ac02baf
793211c
5a2e6db
876b864
a6f3234
73db273
60c096b
f63e8cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ | |
import java.io.InputStream; | ||
import java.io.ObjectInputStream; | ||
import java.io.Serializable; | ||
import java.net.URI; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Locale; | ||
|
@@ -81,7 +82,6 @@ public abstract class ServiceOptions< | |
implements Serializable { | ||
|
||
public static final String CREDENTIAL_ENV_NAME = "GOOGLE_APPLICATION_CREDENTIALS"; | ||
|
||
private static final String DEFAULT_HOST = "https://www.googleapis.com"; | ||
private static final String LEGACY_PROJECT_ENV_NAME = "GCLOUD_PROJECT"; | ||
private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT"; | ||
|
@@ -95,6 +95,7 @@ public abstract class ServiceOptions< | |
protected final String clientLibToken; | ||
|
||
private final String projectId; | ||
private final String universeDomain; | ||
private final String host; | ||
private final RetrySettings retrySettings; | ||
private final String serviceRpcFactoryClassName; | ||
|
@@ -125,6 +126,7 @@ public abstract static class Builder< | |
private final ImmutableSet<String> allowedClientLibTokens = | ||
ImmutableSet.of(ServiceOptions.getGoogApiClientLibName()); | ||
private String projectId; | ||
private String universeDomain; | ||
private String host; | ||
protected Credentials credentials; | ||
private RetrySettings retrySettings; | ||
|
@@ -142,6 +144,7 @@ protected Builder() {} | |
@InternalApi("This class should only be extended within google-cloud-java") | ||
protected Builder(ServiceOptions<ServiceT, OptionsT> options) { | ||
projectId = options.projectId; | ||
universeDomain = options.universeDomain; | ||
host = options.host; | ||
credentials = options.credentials; | ||
retrySettings = options.retrySettings; | ||
|
@@ -199,6 +202,17 @@ public B setHost(String host) { | |
return self(); | ||
} | ||
|
||
/** | ||
* Universe Domain is the domain for Google Cloud Services. It follows the format of | ||
suztomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* `{ServiceName}.{UniverseDomain}`. For example, speech.googleapis.com would have a Universe | ||
* Domain value of `googleapis.com` and cloudasset.test.com would have a Universe Domain of | ||
* `test.com`. If this value is not set, this will default to `googleapis.com`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you add a paragraph that explains the validity of the argument in terms of |
||
*/ | ||
public B setUniverseDomain(String universeDomain) { | ||
this.universeDomain = universeDomain; | ||
suztomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self(); | ||
} | ||
|
||
/** | ||
* Sets the service authentication credentials. If no credentials are set, {@link | ||
* GoogleCredentials#getApplicationDefault()} will be used to attempt getting credentials from | ||
|
@@ -306,6 +320,7 @@ protected ServiceOptions( | |
"A project ID is required for this service but could not be determined from the builder " | ||
+ "or the environment. Please set a project ID using the builder."); | ||
} | ||
universeDomain = builder.universeDomain; | ||
host = firstNonNull(builder.host, getDefaultHost()); | ||
credentials = builder.credentials != null ? builder.credentials : defaultCredentials(); | ||
retrySettings = firstNonNull(builder.retrySettings, getDefaultRetrySettings()); | ||
|
@@ -582,6 +597,16 @@ public String getProjectId() { | |
return projectId; | ||
} | ||
|
||
/** | ||
* Universe Domain is the domain for Google Cloud Services. It follows the format of | ||
* `{ServiceName}.{UniverseDomain}`. For example, speech.googleapis.com would have a Universe | ||
* Domain value of `googleapis.com` and cloudasset.test.com would have a Universe Domain of | ||
* `test.com`. If this value is not set, this will default to `googleapis.com`. | ||
suztomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public String getUniverseDomain() { | ||
return universeDomain; | ||
} | ||
|
||
/** Returns the service host. */ | ||
public String getHost() { | ||
return host; | ||
|
@@ -767,4 +792,62 @@ public String getClientLibToken() { | |
public String getQuotaProjectId() { | ||
return quotaProjectId; | ||
} | ||
|
||
/** | ||
* Returns the resolved endpoint for the Service to connect to Google Cloud | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
suztomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* <p>The resolved endpoint is always in `host:port` format | ||
suztomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public String getResolvedEndpoint(String serviceName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the format of host is intended to be I see that the handwritten libraries handle these inside their own codebase:
I think it should be that the gRPC clients use |
||
if (universeDomain == null) { | ||
return formatEndpoint(serviceName, Credentials.GOOGLE_DEFAULT_UNIVERSE); | ||
} else if (universeDomain.isEmpty()) { | ||
throw new IllegalArgumentException("Universe Domain cannot be empty"); | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
if (host != null) { | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return normalizeEndpoint(); | ||
} | ||
return formatEndpoint(serviceName, getUniverseDomain()); | ||
} | ||
} | ||
|
||
private String formatEndpoint(String serviceName, String universeDomain) { | ||
return serviceName + "." + universeDomain + ":443"; | ||
} | ||
|
||
// Best effort endpoint normalization to ensure it results in {domain}:{port} format | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// for gRPC-Java. `host` is expected to be in format of http(s)://{domain} | ||
String normalizeEndpoint() { | ||
URI uri = URI.create(host); | ||
String scheme = uri.getScheme(); | ||
int port = uri.getPort(); | ||
|
||
// If no port is provided, http:// defaults to 80 and https:// defaults to 443 | ||
if (scheme.equals("http")) { | ||
return String.format("%s:%s", uri.getHost(), port > 0 ? port : 80); | ||
} else if (scheme.equals("https")) { | ||
return String.format("%s:%s", uri.getHost(), port > 0 ? port : 443); | ||
} else { | ||
throw new RuntimeException("Invalid host: " + host + ". Expecting http(s)://{domain}"); | ||
} | ||
} | ||
|
||
/** | ||
* Temporarily used for BigQuery and Storage Apiary Wrapped Libraries. To be removed in the | ||
* future. Returns the host to be used for the rootUrl and output is in the format of: | ||
suztomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* "https://serviceName.universeDomain/" | ||
*/ | ||
@InternalApi | ||
public String getResolvedApiaryHost(String serviceName) { | ||
String resolvedUniverseDomain = | ||
getUniverseDomain() != null ? getUniverseDomain() : Credentials.GOOGLE_DEFAULT_UNIVERSE; | ||
return "https://www." + serviceName + "." + resolvedUniverseDomain + "/"; | ||
} | ||
|
||
/** Validates that Credentials' Universe Domain matches the user configured Universe Domain. */ | ||
public boolean isValidUniverseDomain() throws IOException { | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
suztomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
String resolvedUniverseDomain = | ||
getUniverseDomain() != null ? getUniverseDomain() : Credentials.GOOGLE_DEFAULT_UNIVERSE; | ||
suztomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return resolvedUniverseDomain.equals(getCredentials().getUniverseDomain()); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.