Skip to content

Commit 7eb0c2c

Browse files
committed
Add CurrentWorkspaceIdentifier for non-numeric workspace IDs
CurrentWorkspaceID returns int64 and parses the server's response header with strconv.ParseInt(_, 10, 64). That works for classic numeric workspace IDs but cannot represent workspace identifiers in any other format — the SDK currently has no way to surface those to callers. Add CurrentWorkspaceIdentifier(ctx) (string, error) which returns the identifier exactly as the server echoes it back, without parsing. Refactor CurrentWorkspaceID to delegate to it so there is a single source of truth for the HTTP I/O; the int64 path becomes a thin ParseInt adapter on top. Backwards compatible: existing callers of CurrentWorkspaceID() (int64, error) keep working byte-for-byte against classic numeric workspaces. Callers that need to handle other workspace identifier formats should migrate to CurrentWorkspaceIdentifier. Co-authored-by: Isaac Signed-off-by: Divyansh Vijayvergia <divyansh.vijayvergia@databricks.com>
1 parent 60e716c commit 7eb0c2c

3 files changed

Lines changed: 59 additions & 8 deletions

File tree

NEXT_CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### New Features and Improvements
88

9+
* Add `WorkspaceClient.CurrentWorkspaceIdentifier(ctx) (string, error)` which returns the workspace identifier exactly as the server echoes it back, without attempting to parse it as a number. Use this in preference to `CurrentWorkspaceID` if your code may run against workspaces whose identifier is not a classic numeric ID.
10+
911
### Bug Fixes
1012

1113
### Documentation

workspace_functions.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@ import (
77
"github.com/databricks/databricks-sdk-go/httpclient"
88
)
99

10-
// CurrentWorkspaceID returns the workspace ID of the workspace that this client is
11-
// connected to.
10+
// CurrentWorkspaceIdentifier returns the workspace identifier of the workspace
11+
// that this client is connected to, as the server echoes it back.
12+
//
13+
// The returned value may be either a classic numeric workspace ID (e.g.
14+
// "7474644166319138") or another workspace identifier format that the server
15+
// understands. Prefer this over [WorkspaceClient.CurrentWorkspaceID] if your
16+
// code may run against workspaces whose identifier is not a classic numeric
17+
// ID.
1218
//
1319
// On unified (SPOG) hosts, Config.WorkspaceID is sent in the
1420
// X-Databricks-Workspace-Id request header to route the call to the correct
1521
// workspace; on unified hosts with no WorkspaceID set, the request has no
1622
// routing information and will fail. The ID is read back from the
1723
// X-Databricks-Org-Id response header on /api/2.0/preview/scim/v2/Me — the
1824
// server still emits the legacy response header name during the migration.
19-
func (w *WorkspaceClient) CurrentWorkspaceID(ctx context.Context) (int64, error) {
25+
func (w *WorkspaceClient) CurrentWorkspaceIdentifier(ctx context.Context) (string, error) {
2026
var workspaceIdStr string
2127
opts := []httpclient.DoOption{
2228
httpclient.WithResponseHeader("X-Databricks-Org-Id", &workspaceIdStr),
@@ -25,13 +31,21 @@ func (w *WorkspaceClient) CurrentWorkspaceID(ctx context.Context) (int64, error)
2531
if w.Config != nil && w.Config.WorkspaceID != "" {
2632
opts = append(opts, httpclient.WithRequestHeader("X-Databricks-Workspace-Id", w.Config.WorkspaceID))
2733
}
28-
err := w.apiClient.Do(ctx, "GET", "/api/2.0/preview/scim/v2/Me", opts...)
29-
if err != nil {
30-
return 0, err
34+
if err := w.apiClient.Do(ctx, "GET", "/api/2.0/preview/scim/v2/Me", opts...); err != nil {
35+
return "", err
3136
}
32-
workspaceId, err := strconv.ParseInt(workspaceIdStr, 10, 64)
37+
return workspaceIdStr, nil
38+
}
39+
40+
// CurrentWorkspaceID returns the classic numeric workspace ID of the workspace
41+
// this client is connected to.
42+
//
43+
// Returns a parse error for workspaces whose identifier is not a classic
44+
// numeric ID — use [WorkspaceClient.CurrentWorkspaceIdentifier] in that case.
45+
func (w *WorkspaceClient) CurrentWorkspaceID(ctx context.Context) (int64, error) {
46+
workspaceIdStr, err := w.CurrentWorkspaceIdentifier(ctx)
3347
if err != nil {
3448
return 0, err
3549
}
36-
return workspaceId, nil
50+
return strconv.ParseInt(workspaceIdStr, 10, 64)
3751
}

workspace_functions_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,38 @@ func TestCurrentWorkspaceIDOmitsWorkspaceIdHeaderWhenConfigMissingWorkspaceID(t
101101
assert.Equal(t, 1, meCalls)
102102
assert.Empty(t, gotOrgIdHeader)
103103
}
104+
105+
func TestCurrentWorkspaceIdentifierReturnsRawIdentifier(t *testing.T) {
106+
// CurrentWorkspaceIdentifier returns the workspace identifier exactly as
107+
// the server echoes it back on the X-Databricks-Org-Id response header,
108+
// without attempting to parse it as a number. This is the path callers
109+
// should use when the workspace identifier might be something other than
110+
// a classic numeric workspace ID (e.g. a CPDR connection ID).
111+
const identifier = "7a99b43c-b46c-432b-b0a7-814217701909"
112+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
113+
if r.URL.Path == "/api/2.0/preview/scim/v2/Me" {
114+
w.Header().Set("X-Databricks-Org-Id", identifier)
115+
w.Write([]byte(`{}`))
116+
return
117+
}
118+
http.NotFound(w, r)
119+
}))
120+
defer server.Close()
121+
122+
w, err := NewWorkspaceClient(&Config{
123+
Host: server.URL,
124+
Token: "token",
125+
WorkspaceID: identifier,
126+
})
127+
require.NoError(t, err)
128+
129+
got, err := w.CurrentWorkspaceIdentifier(t.Context())
130+
require.NoError(t, err)
131+
assert.Equal(t, identifier, got)
132+
133+
// CurrentWorkspaceID, by contrast, returns a parse error on non-numeric
134+
// identifiers — callers carrying CPDR workspaces must use
135+
// CurrentWorkspaceIdentifier instead.
136+
_, err = w.CurrentWorkspaceID(t.Context())
137+
assert.Error(t, err)
138+
}

0 commit comments

Comments
 (0)