Skip to content

Commit eac7007

Browse files
authored
Exporter: relax handling of problematic files/directories (#2258)
The new UI has a bug that allows creation of directories with `/` character in the name - it breaks the Workspace List API, and prevents exporting of the workspace objects. The temporary solution is explicitly add the `ignoreErrors` flag when using exporter.
1 parent 4b21f81 commit eac7007

File tree

7 files changed

+34
-28
lines changed

7 files changed

+34
-28
lines changed

exporter/context.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ func (ic *importContext) HasInState(r *resource, onlyAdded bool) bool {
392392
continue
393393
}
394394
for _, i := range sr.Instances {
395-
if i.Attributes[k].(string) == v {
395+
tv, ok := i.Attributes[k].(string)
396+
if ok && tv == v {
396397
return true
397398
}
398399
}

exporter/exporter_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,12 @@ func TestImportingDLTPipelines(t *testing.T) {
16281628
},
16291629
},
16301630
},
1631+
{
1632+
Method: "GET",
1633+
Resource: "/api/2.0/workspace/get-status?path=%2FUsers%2Fuser%40domain.com",
1634+
Response: map[string]any{},
1635+
ReuseRequest: true,
1636+
},
16311637
{
16321638
Method: "GET",
16331639
Resource: "/api/2.0/permissions/repos/123",

exporter/importables.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,20 +1150,20 @@ var resourcesMap map[string]importable = map[string]importable{
11501150
},
11511151
List: func(ic *importContext) error {
11521152
notebooksAPI := workspace.NewNotebooksAPI(ic.Context, ic.Client)
1153-
notebookList, err := notebooksAPI.List("/", true)
1153+
notebookList, err := notebooksAPI.List("/", true, true)
11541154
if err != nil {
11551155
return err
11561156
}
11571157
for offset, notebook := range notebookList {
11581158
if strings.HasPrefix(notebook.Path, "/Repos") {
11591159
continue
11601160
}
1161-
// TODO: emit permissions for notebook folders if non-default,
1162-
// as per-notebook permission entry would be a noise in the state
1163-
ic.Emit(&resource{
1164-
Resource: "databricks_notebook",
1165-
ID: notebook.Path,
1166-
})
1161+
if notebook.ObjectType == workspace.Notebook {
1162+
ic.Emit(&resource{
1163+
Resource: "databricks_notebook",
1164+
ID: notebook.Path,
1165+
})
1166+
}
11671167
if offset%50 == 0 {
11681168
log.Printf("[INFO] Scanned %d of %d notebooks",
11691169
offset+1, len(notebookList))
@@ -1204,9 +1204,8 @@ var resourcesMap map[string]importable = map[string]importable{
12041204

12051205
if ic.meAdmin {
12061206
ic.Emit(&resource{
1207-
Resource: "databricks_directory",
1208-
Attribute: "path",
1209-
Value: directoryPath,
1207+
Resource: "databricks_directory",
1208+
ID: directoryPath,
12101209
})
12111210
}
12121211

exporter/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (ic *importContext) emitNotebookOrRepo(path string) {
143143
func (ic *importContext) getAllDirectories() []workspace.ObjectStatus {
144144
if len(ic.allDirectories) == 0 {
145145
notebooksAPI := workspace.NewNotebooksAPI(ic.Context, ic.Client)
146-
ic.allDirectories, _ = notebooksAPI.ListDirectories("/", true)
146+
ic.allDirectories, _ = notebooksAPI.ListDirectories("/", true, true)
147147
}
148148
return ic.allDirectories
149149
}

workspace/data_notebook_paths.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func DataSourceNotebookPaths() *schema.Resource {
1313
ReadContext: func(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
1414
path := d.Get("path").(string)
1515
recursive := d.Get("recursive").(bool)
16-
notebookList, err := NewNotebooksAPI(ctx, m).List(path, recursive)
16+
notebookList, err := NewNotebooksAPI(ctx, m).List(path, recursive, false)
1717
if err != nil {
1818
return diag.FromErr(err)
1919
}

workspace/resource_notebook.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ func (a NotebooksAPI) Mkdirs(path string) error {
133133
// List will list all objects in a path on the workspace
134134
// and with the recursive flag it will recursively list
135135
// all the objects
136-
func (a NotebooksAPI) List(path string, recursive bool) ([]ObjectStatus, error) {
136+
func (a NotebooksAPI) List(path string, recursive bool, ignoreErrors bool) ([]ObjectStatus, error) {
137137
if recursive {
138138
var paths []ObjectStatus
139-
err := a.recursiveAddPaths(path, &paths)
139+
err := a.recursiveAddPaths(path, &paths, ignoreErrors)
140140
if err != nil {
141141
return nil, err
142142
}
@@ -145,28 +145,28 @@ func (a NotebooksAPI) List(path string, recursive bool) ([]ObjectStatus, error)
145145
return a.list(path)
146146
}
147147

148-
func (a NotebooksAPI) recursiveAddPaths(path string, pathList *[]ObjectStatus) error {
148+
func (a NotebooksAPI) recursiveAddPaths(path string, pathList *[]ObjectStatus, ignoreErrors bool) error {
149149
notebookInfoList, err := a.list(path)
150-
if err != nil {
150+
if err != nil && !ignoreErrors {
151151
return err
152152
}
153153
for _, v := range notebookInfoList {
154154
if v.ObjectType == Notebook || v.ObjectType == File {
155155
*pathList = append(*pathList, v)
156156
} else if v.ObjectType == Directory {
157-
err := a.recursiveAddPaths(v.Path, pathList)
157+
err := a.recursiveAddPaths(v.Path, pathList, ignoreErrors)
158158
if err != nil {
159159
return err
160160
}
161161
}
162162
}
163-
return err
163+
return nil
164164
}
165165

166-
func (a NotebooksAPI) ListDirectories(path string, recursive bool) ([]ObjectStatus, error) {
166+
func (a NotebooksAPI) ListDirectories(path string, recursive bool, ignoreErrors bool) ([]ObjectStatus, error) {
167167
if recursive {
168168
var paths []ObjectStatus
169-
err := a.recursiveAddDirectoryPaths(path, &paths)
169+
err := a.recursiveAddDirectoryPaths(path, &paths, ignoreErrors)
170170
if err != nil {
171171
return nil, err
172172
}
@@ -175,21 +175,21 @@ func (a NotebooksAPI) ListDirectories(path string, recursive bool) ([]ObjectStat
175175
return a.list(path)
176176
}
177177

178-
func (a NotebooksAPI) recursiveAddDirectoryPaths(path string, pathList *[]ObjectStatus) error {
178+
func (a NotebooksAPI) recursiveAddDirectoryPaths(path string, pathList *[]ObjectStatus, ignoreErrors bool) error {
179179
directoryInfoList, err := a.list(path)
180-
if err != nil {
180+
if err != nil && !ignoreErrors {
181181
return err
182182
}
183183
for _, v := range directoryInfoList {
184184
if v.ObjectType == Directory {
185185
*pathList = append(*pathList, v)
186-
err := a.recursiveAddDirectoryPaths(v.Path, pathList)
186+
err := a.recursiveAddDirectoryPaths(v.Path, pathList, ignoreErrors)
187187
if err != nil {
188188
return err
189189
}
190190
}
191191
}
192-
return err
192+
return nil
193193
}
194194

195195
type ObjectList struct {

workspace/resource_notebook_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func TestListDirectories(t *testing.T) {
457457
ctx := context.Background()
458458
notebooksAPI := NewNotebooksAPI(ctx, client)
459459

460-
directoryList, err := notebooksAPI.ListDirectories("/", true)
460+
directoryList, err := notebooksAPI.ListDirectories("/", true, false)
461461
var paths []string
462462
for _, directory := range directoryList {
463463
paths = append(paths, directory.Path)
@@ -546,7 +546,7 @@ func TestListDirectories_NoneRecursive(t *testing.T) {
546546
require.NoError(t, err)
547547
ctx := context.Background()
548548
notebooksAPI := NewNotebooksAPI(ctx, client)
549-
directoryList_not_recursive, err := notebooksAPI.ListDirectories("/", false)
549+
directoryList_not_recursive, err := notebooksAPI.ListDirectories("/", false, false)
550550
var paths []string
551551
for _, directory := range directoryList_not_recursive {
552552
paths = append(paths, directory.Path)
@@ -586,7 +586,7 @@ func TestListDirectoriesRecursive_Error(t *testing.T) {
586586
require.NoError(t, err)
587587
ctx := context.Background()
588588
notebooksAPI := NewNotebooksAPI(ctx, client)
589-
directoryList, err := notebooksAPI.ListDirectories("/", true)
589+
directoryList, err := notebooksAPI.ListDirectories("/", true, false)
590590
assert.Error(t, err, err)
591591
assert.Nil(t, directoryList)
592592
}

0 commit comments

Comments
 (0)