From 5f17e62a7b0f10035205ec4197b8f77388b059f8 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Fri, 9 Feb 2024 18:33:56 +0000 Subject: [PATCH] fix: Apiary Host returns user set host if set (#2455) Bug: https://github.com/googleapis/java-bigquery/issues/3125 (Removing `Fixes:` as I don't want to close the ticket until BigQuery is able to pull in a new version of shared-deps). Following guidance in doc for Apiary (ping me internally for link). If the user configures the host to be the `DEFAULT_HOST` (a non-valid endpoint for any service using java-core), then it should construct a valid service endpoint back using the universe domain. --- .../main/java/com/google/cloud/ServiceOptions.java | 11 +++++++++-- .../java/com/google/cloud/ServiceOptionsTest.java | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/java-core/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java b/java-core/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java index 9384e7823d..985fac4804 100644 --- a/java-core/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/java-core/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -851,17 +851,24 @@ public String getResolvedHost(String serviceName) { } /** - * Temporarily used for BigQuery and Storage Apiary Wrapped Libraries. To be removed in the future - * when Apiary clients can resolve their endpoints. Returns the host to be used as the rootUrl. + * Returns a host value to be used for BigQuery and Storage Apiary Wrapped Libraries. To be + * removed in the future when Apiary clients can resolve their endpoints. Returns the host to be + * used as the rootUrl. * *

The resolved host will be in `https://{serviceName}.{resolvedUniverseDomain}/` format. The * resolvedUniverseDomain will be set to `googleapis.com` if universeDomain is null. * + *

The host value is set to DEFAULT_HOST if the user didn't configure a host. Returns the host + * value the user set, otherwise constructs the host for the user. + * * @see rootUrl */ @InternalApi public String getResolvedApiaryHost(String serviceName) { + if (!DEFAULT_HOST.equals(host)) { + return host; + } String resolvedUniverseDomain = universeDomain != null ? universeDomain : Credentials.GOOGLE_DEFAULT_UNIVERSE; return "https://" + serviceName + "." + resolvedUniverseDomain + "/"; diff --git a/java-core/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java b/java-core/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java index 3d5ca3eef5..1b79e48ddc 100644 --- a/java-core/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java +++ b/java-core/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java @@ -566,10 +566,10 @@ public void testGetResolvedApiaryHost_customUniverseDomain_customHost() { TestServiceOptions options = TestServiceOptions.newBuilder() .setUniverseDomain("test.com") - .setHost("https://service.random.com") + .setHost("https://service.random.com/") .setProjectId("project-id") .build(); - assertThat(options.getResolvedApiaryHost("service")).isEqualTo("https://service.test.com/"); + assertThat(options.getResolvedApiaryHost("service")).isEqualTo("https://service.random.com/"); } // No User Configuration = GDU, Default Credentials = GDU