Skip to content

Commit e068441

Browse files
authored
[Internal] Use resolved host type from host metadata in HostType() (#1606)
## Summary `HostType()` now returns the host type resolved from the `/.well-known/databricks-config` metadata endpoint when available, falling back to URL pattern matching only when metadata has not been resolved. ## Why PR #1596 added `HostType` to the host metadata response and stores it in `Config.resolvedHostType` during resolution. Adding support for the host type metadata in HostType() ## What changed ### Interface changes None. `HostType()` still returns `HostType` with the same possible values. ### Behavioral changes - **`HostType()` now returns all resolved host types** (`WorkspaceHost`, `AccountHost`, `UnifiedHost`) from metadata when `resolvedHostType` is not `HostTypeUnknown`. - The metadata check takes priority over **all** existing fallback logic, including the `Experimental_IsUnifiedHost` flag and URL pattern matching. - When metadata is unavailable, behavior is identical to before. ### Internal changes - Added `resolvedHostType` check at the top of `HostType()` in `config/config.go`. - Updated docstring to reflect the new resolution order. ## How is this tested? Added 7 new unit tests in `config/config_test.go`: - Metadata workspace/account/unified types override URL patterns - Falls back to URL matching when metadata is `HostTypeUnknown` (workspace + account URLs) - Metadata takes priority over `Experimental_IsUnifiedHost` flag - End-to-end: `EnsureResolved()` with mocked metadata → `HostType()` returns server value
1 parent eedcd37 commit e068441

3 files changed

Lines changed: 82 additions & 2 deletions

File tree

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
### Internal Changes
2525

26+
* Use resolved host type from host metadata in `HostType()` method, falling back to URL pattern matching when metadata is unavailable.
2627
* Normalize internal token sources on `auth.TokenSource` for proper context propagation ([#1577](https://github.com/databricks/databricks-sdk-go/pull/1577)).
2728
* Fix `TestAzureGithubOIDCCredentials` hang caused by missing `HTTPTransport` stub: `EnsureResolved` now calls `resolveHostMetadata`, which makes a real network request when no transport is set ([#1550](https://github.com/databricks/databricks-sdk-go/pull/1550)).
2829
* Bump golang.org/x/crypto from 0.21.0 to 0.45.0 in /examples/slog ([#1566](https://github.com/databricks/databricks-sdk-go/pull/1566)).

config/config.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,19 @@ func normalizedHost(host string) string {
447447
}
448448

449449
// HostType returns the type of host that the client is configured for.
450-
// HostType now only returns WorkspaceHost or AccountHost. UnifiedHost is
451-
// deprecated; host metadata resolution handles unified host behavior.
450+
// When host metadata has been resolved (via /.well-known/databricks-config),
451+
// the resolved host type is returned directly. Otherwise, the host type is
452+
// inferred from URL patterns as a fallback.
453+
//
454+
// Deprecated: The SDK is moving towards host-agnostic behavior where host URL
455+
// patterns are no longer used to determine client capabilities. This method is
456+
// retained for backwards compatibility but should not be used in new code.
452457
func (c *Config) HostType() HostType {
458+
// If host metadata resolved a known host type, use it.
459+
if c.resolvedHostType != HostTypeUnknown {
460+
return c.resolvedHostType
461+
}
462+
453463
// TODO: Remove this after TF updates its code.
454464
if c.Experimental_IsUnifiedHost {
455465
return UnifiedHost

config/config_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,75 @@ func TestHostType_UnifiedFlagNoLongerReturnsUnified(t *testing.T) {
8585
assert.Equal(t, WorkspaceHost, c.HostType())
8686
}
8787

88+
func TestHostType_UsesMetadataFirst_Workspace(t *testing.T) {
89+
c := &Config{
90+
Host: "https://accounts.cloud.databricks.com",
91+
resolvedHostType: WorkspaceHost,
92+
}
93+
assert.Equal(t, WorkspaceHost, c.HostType())
94+
}
95+
96+
func TestHostType_UsesMetadataFirst_Account(t *testing.T) {
97+
c := &Config{
98+
Host: "https://my-workspace.cloud.databricks.com",
99+
resolvedHostType: AccountHost,
100+
}
101+
assert.Equal(t, AccountHost, c.HostType())
102+
}
103+
104+
func TestHostType_UsesMetadataFirst_Unified(t *testing.T) {
105+
c := &Config{
106+
Host: "https://my-workspace.cloud.databricks.com",
107+
resolvedHostType: UnifiedHost,
108+
}
109+
assert.Equal(t, UnifiedHost, c.HostType())
110+
}
111+
112+
func TestHostType_FallsBackToURLWhenMetadataUnknown(t *testing.T) {
113+
c := &Config{
114+
Host: "https://accounts.cloud.databricks.com",
115+
resolvedHostType: HostTypeUnknown,
116+
}
117+
assert.Equal(t, AccountHost, c.HostType())
118+
}
119+
120+
func TestHostType_FallsBackToURLWhenMetadataUnknown_Workspace(t *testing.T) {
121+
c := &Config{
122+
Host: "https://my-workspace.cloud.databricks.com",
123+
resolvedHostType: HostTypeUnknown,
124+
}
125+
assert.Equal(t, WorkspaceHost, c.HostType())
126+
}
127+
128+
func TestHostType_MetadataOverridesExperimentalFlag(t *testing.T) {
129+
c := &Config{
130+
Host: "https://my-workspace.cloud.databricks.com",
131+
Experimental_IsUnifiedHost: true,
132+
resolvedHostType: AccountHost,
133+
}
134+
assert.Equal(t, AccountHost, c.HostType())
135+
}
136+
137+
func TestHostType_EndToEnd_MetadataResolvesHostType(t *testing.T) {
138+
noopLoader := mockLoader(func(cfg *Config) error { return nil })
139+
cfg := &Config{
140+
Host: "https://my-workspace.cloud.databricks.com",
141+
Loaders: []Loader{noopLoader},
142+
HTTPTransport: fixtures.SliceTransport{
143+
{
144+
Method: "GET",
145+
Resource: "/.well-known/databricks-config",
146+
ReuseRequest: true,
147+
Status: 200,
148+
Response: `{"oidc_endpoint": "https://my-workspace.cloud.databricks.com/oidc", "account_id": "` + testHMAccountID + `", "host_type": "account"}`,
149+
},
150+
},
151+
}
152+
err := cfg.EnsureResolved()
153+
require.NoError(t, err)
154+
assert.Equal(t, AccountHost, cfg.HostType())
155+
}
156+
88157
func TestIsAccountClient_NoLongerPanicsOnUnifiedHost(t *testing.T) {
89158
c := &Config{
90159
Host: "https://unified.cloud.databricks.com",

0 commit comments

Comments
 (0)