Skip to content

Commit de63be7

Browse files
committed
Use resolved host type from host metadata in getHostType()
Signed-off-by: Tanmay Rustagi <tanmay.rustagi@databricks.com>
1 parent 747e138 commit de63be7

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,15 @@ public boolean isAccountClient() {
775775
return host.startsWith("https://accounts.") || host.startsWith("https://accounts-dod.");
776776
}
777777

778-
/** Returns the host type based on configuration settings and host URL. */
778+
/**
779+
* Returns the host type based on configuration settings and host URL. When host metadata has been
780+
* resolved (via /.well-known/databricks-config), the resolved host type is returned directly.
781+
* Otherwise, the host type is inferred from URL patterns as a fallback.
782+
*/
779783
public HostType getHostType() {
784+
if (resolvedHostType != null) {
785+
return resolvedHostType;
786+
}
780787
if (experimentalIsUnifiedHost != null && experimentalIsUnifiedHost) {
781788
return HostType.UNIFIED;
782789
}

databricks-sdk-java/src/test/java/com/databricks/sdk/core/UnifiedHostTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,71 @@ public void testNoHeaderInjectionForTraditionalWorkspace() {
231231
assertEquals("Bearer test-token", headers.get("Authorization"));
232232
assertNull(headers.get("X-Databricks-Org-Id"));
233233
}
234+
235+
// --- Resolved host type from metadata tests ---
236+
237+
@Test
238+
public void testMetadataWorkspaceOverridesAccountLikeUrl() {
239+
DatabricksConfig config =
240+
new DatabricksConfig()
241+
.setHost("https://accounts.cloud.databricks.com")
242+
.setResolvedHostType(HostType.WORKSPACE);
243+
assertEquals(HostType.WORKSPACE, config.getHostType());
244+
}
245+
246+
@Test
247+
public void testMetadataAccountOverridesWorkspaceLikeUrl() {
248+
DatabricksConfig config =
249+
new DatabricksConfig()
250+
.setHost("https://my-workspace.cloud.databricks.com")
251+
.setResolvedHostType(HostType.ACCOUNTS);
252+
assertEquals(HostType.ACCOUNTS, config.getHostType());
253+
}
254+
255+
@Test
256+
public void testMetadataUnifiedIsReturned() {
257+
DatabricksConfig config =
258+
new DatabricksConfig()
259+
.setHost("https://my-workspace.cloud.databricks.com")
260+
.setResolvedHostType(HostType.UNIFIED);
261+
assertEquals(HostType.UNIFIED, config.getHostType());
262+
}
263+
264+
@Test
265+
public void testFallsBackToUrlMatchingWhenResolvedHostTypeNull() {
266+
DatabricksConfig config =
267+
new DatabricksConfig().setHost("https://accounts.cloud.databricks.com");
268+
// resolvedHostType is null by default
269+
assertEquals(HostType.ACCOUNTS, config.getHostType());
270+
}
271+
272+
@Test
273+
public void testMetadataOverridesExperimentalFlag() {
274+
DatabricksConfig config =
275+
new DatabricksConfig()
276+
.setHost("https://my-workspace.cloud.databricks.com")
277+
.setExperimentalIsUnifiedHost(true)
278+
.setResolvedHostType(HostType.ACCOUNTS);
279+
// Resolved host type takes priority over experimental flag
280+
assertEquals(HostType.ACCOUNTS, config.getHostType());
281+
}
282+
283+
@Test
284+
public void testEndToEndResolveToGetHostType() throws IOException {
285+
String response =
286+
"{\"oidc_endpoint\":\"https://ws.databricks.com/oidc\","
287+
+ "\"account_id\":\"test-account\","
288+
+ "\"host_type\":\"unified\"}";
289+
try (FixtureServer server =
290+
new FixtureServer()
291+
.with("GET", "/.well-known/databricks-config", response, 200)
292+
.with("GET", "/.well-known/databricks-config", response, 200)) {
293+
DatabricksConfig config =
294+
new DatabricksConfig().setHost(server.getUrl()).setExperimentalIsUnifiedHost(true);
295+
config.resolve(
296+
new Environment(new HashMap<>(), new ArrayList<>(), System.getProperty("os.name")));
297+
// After resolve(), tryResolveHostMetadata() should have set resolvedHostType
298+
assertEquals(HostType.UNIFIED, config.getHostType());
299+
}
300+
}
234301
}

0 commit comments

Comments
 (0)