11package deploy
22
33import (
4- "bytes"
54 "context"
65 "fmt"
76 "io"
87 "io/fs"
9- "net/http"
10- "net/url"
118
129 "github.com/databricks/cli/bundle"
1310 "github.com/databricks/cli/libs/auth"
1411 "github.com/databricks/cli/libs/filer"
15- "github.com/databricks/databricks-sdk-go/client "
12+ "github.com/databricks/databricks-sdk-go"
1613)
1714
1815// FilerFactory is a function that returns a filer.Filer.
@@ -21,8 +18,8 @@ type FilerFactory func(ctx context.Context, b *bundle.Bundle) (filer.Filer, erro
2118type stateFiler struct {
2219 filer filer.Filer
2320
24- apiClient * client. DatabricksClient
25- root filer.WorkspaceRootPath
21+ workspaceClient * databricks. WorkspaceClient
22+ root filer.WorkspaceRootPath
2623}
2724
2825func (s stateFiler ) Delete (ctx context.Context , path string , mode ... filer.DeleteMode ) error {
@@ -48,20 +45,8 @@ func (s stateFiler) Read(ctx context.Context, path string) (io.ReadCloser, error
4845 return nil , fmt .Errorf ("not a file: %s" , absPath )
4946 }
5047
51- var buf bytes.Buffer
52- // We read via the raw apiClient.Do so the workspace-id routing header goes
53- // through auth.WorkspaceIDHeaders, which drops the CLI-only "none" sentinel
54- // (auth.WorkspaceIDNone, written by `auth login --skip-workspace`). The SDK's
55- // Workspace client would forward that sentinel as a literal workspace id
56- // unless we special-cased it; reusing the shared helper here keeps the change
57- // small.
58- urlPath := "/api/2.0/workspace/export?path=" + url .QueryEscape (absPath ) + "&direct_download=true"
59- err = s .apiClient .Do (ctx , http .MethodGet , urlPath , auth .WorkspaceIDHeaders (s .apiClient .Config ), nil , nil , & buf )
60- if err != nil {
61- return nil , err
62- }
63-
64- return io .NopCloser (& buf ), nil
48+ // Stream via the SDK's /workspace/export (direct_download=true); no 10 MB cap.
49+ return s .workspaceClient .Workspace .Download (ctx , absPath )
6550}
6651
6752func (s stateFiler ) ReadDir (ctx context.Context , path string ) ([]fs.DirEntry , error ) {
@@ -80,19 +65,37 @@ func (s stateFiler) Write(ctx context.Context, path string, reader io.Reader, mo
8065// Reads use the streaming /workspace/export API, which is officially supported,
8166// scoped, and streams state files well beyond the 10 MB JSON export limit.
8267func StateFiler (ctx context.Context , b * bundle.Bundle ) (filer.Filer , error ) {
83- f , err := filer . NewWorkspaceFilesClient ( b . WorkspaceClient ( ctx ) , b . Config . Workspace . StatePath )
68+ w , err := stateWorkspaceClient ( ctx , b )
8469 if err != nil {
8570 return nil , err
8671 }
8772
88- apiClient , err := client . New ( b . WorkspaceClient ( ctx ). Config )
73+ f , err := filer . NewWorkspaceFilesClient ( w , b . Config . Workspace . StatePath )
8974 if err != nil {
90- return nil , fmt . Errorf ( "failed to create API client: %w" , err )
75+ return nil , err
9176 }
9277
9378 return stateFiler {
94- filer : f ,
95- root : filer .NewWorkspaceRootPath (b .Config .Workspace .StatePath ),
96- apiClient : apiClient ,
79+ filer : f ,
80+ root : filer .NewWorkspaceRootPath (b .Config .Workspace .StatePath ),
81+ workspaceClient : w ,
9782 }, nil
9883}
84+
85+ // stateWorkspaceClient returns the bundle's workspace client, cloned with the CLI-only
86+ // "none" workspace-id sentinel stripped so Workspace.Download does not forward it as the
87+ // X-Databricks-Workspace-Id routing header.
88+ func stateWorkspaceClient (ctx context.Context , b * bundle.Bundle ) (* databricks.WorkspaceClient , error ) {
89+ w := b .WorkspaceClient (ctx )
90+ if w .Config .WorkspaceID != auth .WorkspaceIDNone {
91+ return w , nil
92+ }
93+
94+ // config.Config embeds a sync.Mutex, so use the SDK's copylocks-safe clone.
95+ cfg , err := w .Config .NewWithWorkspaceHost (w .Config .Host )
96+ if err != nil {
97+ return nil , err
98+ }
99+ cfg .WorkspaceID = ""
100+ return databricks .NewWorkspaceClient ((* databricks .Config )(cfg ))
101+ }
0 commit comments