Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/cli/auth-login-workspace-lookup-timeout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `databricks auth login` now gives up on the optional workspace lookup after about 5 seconds instead of retrying for about 5 minutes, and explains the fallback to the manual workspace ID prompt. ([#7522](https://github.com/databricks/cli/pull/7522))
7 changes: 7 additions & 0 deletions cmd/auth/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ var (
NewWorkspaceSearcher = newWorkspaceSearcher
PromptForWorkspaceSelection = promptForWorkspaceSelection
)

// SetWorkspaceListTimeoutSeconds overrides the workspace lookup budget and returns a restore func.
func SetWorkspaceListTimeoutSeconds(seconds int) func() {
previous := workspaceListTimeoutSeconds
workspaceListTimeoutSeconds = seconds
return func() { workspaceListTimeoutSeconds = previous }
}
11 changes: 11 additions & 0 deletions cmd/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,10 @@ func u2mResourcesFromProfile(p *profile.Profile) []string {
return splitScopes(p.Resources)
}

// workspaceListTimeoutSeconds bounds the optional workspace lookup during login.
// It is a variable only so tests can shorten it.
var workspaceListTimeoutSeconds = 5

// promptForWorkspaceSelection lists workspaces for a SPOG account and lets the
// user pick one. Returns the selected workspace ID or empty string if skipped.
// This is best-effort: errors are returned to the caller for logging, not shown
Expand All @@ -1068,14 +1072,21 @@ func promptForWorkspaceSelection(ctx context.Context, authArguments *auth.AuthAr
Host: authArguments.Host,
AccountID: authArguments.AccountID,
Credentials: config.NewTokenSourceStrategy("login-token", authconv.AuthTokenSource(tokenSource)),

// The workspace list is only a convenience with a manual fallback, so
// don't spend the SDK's default 5 minute retry budget on a 503 here.
HTTPTimeoutSeconds: workspaceListTimeoutSeconds,
RetryTimeoutSeconds: workspaceListTimeoutSeconds,
})
if err != nil {
return "", err
}

cmdio.LogString(ctx, "Looking up workspaces for your account...")
workspaces, err := a.Workspaces.List(ctx)
if err != nil {
log.Debugf(ctx, "Failed to load workspaces (this can happen if the user has no account-level access): %v", err)
cmdio.LogString(ctx, "Could not list workspaces for this account. To skip this step, pass a host with the workspace ID, e.g. --host \"<host>?w=<workspace_id>\".")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The non-TTY branch a few lines up tells users to pass --workspace-id, and this hint suggests --host "<host>?w=<workspace_id>" for the same goal. Both work, but it'd be nice if the two messages agreed. --workspace-id is also shorter and doesn't make the user rebuild the host URL. People who only want account-level access probably want --skip-workspace instead, so it could be worth mentioning too. Something like:

cmdio.LogString(ctx, "Could not list workspaces for this account. To skip this step, pass --workspace-id <workspace_id>, or --skip-workspace for account-level access only.")

return promptForWorkspaceID(ctx)
}

Expand Down
19 changes: 19 additions & 0 deletions cmd/auth/login_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,25 @@ func TestPromptForWorkspaceSelectionListError(t *testing.T) {
}
}

func TestPromptForWorkspaceSelectionGivesUpQuicklyOnUnavailable(t *testing.T) {
t.Cleanup(authcmd.SetWorkspaceListTimeoutSeconds(1))
args, requests := newWorkspaceListServer(t, http.StatusServiceUnavailable, nil)
start := time.Now()
tm := termtest.Run(t, func(ctx context.Context) (string, error) {
return authcmd.PromptForWorkspaceSelection(ctx, args, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: workspaceTestToken}))
})
tm.WaitFor(workspaceIDPrompt)
// The SDK's default retry budget is 5 minutes; the lookup must fall back well before that.
assert.Less(t, time.Since(start), 30*time.Second)
tm.Type("123456" + termtest.KeyEnter)
id, err := tm.Result()
require.NoError(t, err)
assert.Equal(t, "123456", id)
assert.Positive(t, requests.Load())
assert.Contains(t, tm.Raw(), "Looking up workspaces")
assert.Contains(t, tm.Raw(), "?w=<workspace_id>")
}

// newWorkspaceListServer exercises the SDK's actual list decoding without a live account.
func newWorkspaceListServer(t *testing.T, status int, workspaces []provisioning.Workspace) (*auth.AuthArguments, *atomic.Int32) {
t.Helper()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Looking up workspaces for your account...
Search: █
? Select a workspace (search by name or ID):
▸ Alpha (111)
Expand Down
Loading