Skip to content

Commit 7ddbeab

Browse files
authored
[Fix] Upload content databricks_workspace_file using raw format (#4200)
## Changes <!-- Summary of your changes that are easy to understand --> This fixes a problem with uploading zip-based files with the `databricks_workspace_file` resource. ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [ ] ~relevant change in `docs/` folder~ - [x] covered with integration tests in `internal/acceptance` - [ ] relevant acceptance tests are passing - [x] using Go SDK
1 parent 80514f5 commit 7ddbeab

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

internal/acceptance/permissions_test.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -466,17 +466,22 @@ func TestAccPermissions_WorkspaceFile_Path(t *testing.T) {
466466
}
467467
resource "databricks_workspace_file" "this" {
468468
source = "{var.CWD}/../../storage/testdata/tf-test-python.py"
469-
path = "${databricks_directory.this.path}/test_notebook"
469+
path = "${databricks_directory.this.path}/test_ws_file"
470470
}`
471471
WorkspaceLevel(t, Step{
472-
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id", groupPermissions("CAN_RUN")),
472+
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id",
473+
groupPermissions("CAN_RUN")),
473474
}, Step{
474-
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id", currentPrincipalPermission(t, "CAN_MANAGE"), allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
475+
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id",
476+
currentPrincipalPermission(t, "CAN_MANAGE"),
477+
allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
475478
}, Step{
476479
// The current user can be removed from permissions since they inherit permissions from the directory they created.
477-
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id", allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
480+
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id",
481+
allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
478482
}, Step{
479-
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id", currentPrincipalPermission(t, "CAN_READ")),
483+
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id",
484+
currentPrincipalPermission(t, "CAN_READ")),
480485
ExpectError: regexp.MustCompile("cannot remove management permissions for the current user for file, allowed levels: CAN_MANAGE"),
481486
})
482487
}
@@ -489,17 +494,22 @@ func TestAccPermissions_WorkspaceFile_Id(t *testing.T) {
489494
}
490495
resource "databricks_workspace_file" "this" {
491496
source = "{var.CWD}/../../storage/testdata/tf-test-python.py"
492-
path = "${databricks_directory.this.path}/test_notebook"
497+
path = "${databricks_directory.this.path}/test_ws_file"
493498
}`
494499
WorkspaceLevel(t, Step{
495-
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id", groupPermissions("CAN_RUN")),
500+
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id",
501+
groupPermissions("CAN_RUN")),
496502
}, Step{
497-
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id", currentPrincipalPermission(t, "CAN_MANAGE"), allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
503+
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id",
504+
currentPrincipalPermission(t, "CAN_MANAGE"),
505+
allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
498506
}, Step{
499507
// The current user can be removed from permissions since they inherit permissions from the directory they created.
500-
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id", allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
508+
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id",
509+
allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
501510
}, Step{
502-
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id", currentPrincipalPermission(t, "CAN_READ")),
511+
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id",
512+
currentPrincipalPermission(t, "CAN_READ")),
503513
ExpectError: regexp.MustCompile("cannot remove management permissions for the current user for file, allowed levels: CAN_MANAGE"),
504514
})
505515
}

internal/acceptance/workspace_file_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ func TestAccWorkspaceFileEmptyFile(t *testing.T) {
2727
})
2828
}
2929

30+
func TestAccWorkspaceFileZipFile(t *testing.T) {
31+
WorkspaceLevel(t, Step{
32+
Template: `resource "databricks_workspace_file" "zipfile" {
33+
source = "{var.CWD}/../../workspace/acceptance/testdata/zipfile.zip"
34+
path = "/Shared/provider-test/zipfile_{var.RANDOM}.zip"
35+
}`,
36+
})
37+
}
38+
3039
func TestAccWorkspaceFileBase64(t *testing.T) {
3140
WorkspaceLevel(t, Step{
3241
Template: `resource "databricks_workspace_file" "this2" {
171 Bytes
Binary file not shown.

workspace/resource_workspace_file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func ResourceWorkspaceFile() common.Resource {
4444
path := d.Get("path").(string)
4545
importReq := workspace.Import{
4646
Content: base64.StdEncoding.EncodeToString(content),
47-
Format: workspace.ImportFormatAuto,
47+
Format: workspace.ImportFormatRaw,
4848
Path: path,
4949
Overwrite: true,
5050
ForceSendFields: []string{"Content"},
@@ -93,7 +93,7 @@ func ResourceWorkspaceFile() common.Resource {
9393
}
9494
return client.Workspace.Import(ctx, workspace.Import{
9595
Content: base64.StdEncoding.EncodeToString(content),
96-
Format: workspace.ImportFormatAuto,
96+
Format: workspace.ImportFormatRaw,
9797
Overwrite: true,
9898
Path: d.Id(),
9999
ForceSendFields: []string{"Content"},

workspace/resource_workspace_file_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestResourceWorkspaceFileCreate_DirectoryExist(t *testing.T) {
120120
Content: dummyWorkspaceFilePayload,
121121
Path: dummyWorkspaceFilePath,
122122
Overwrite: true,
123-
Format: "AUTO",
123+
Format: "RAW",
124124
},
125125
},
126126
{
@@ -168,7 +168,7 @@ func TestResourceWorkspaceFileCreate_DirectoryDoesntExist(t *testing.T) {
168168
Content: dummyWorkspaceFilePayload,
169169
Path: dummyWorkspaceFilePath,
170170
Overwrite: true,
171-
Format: "AUTO",
171+
Format: "RAW",
172172
},
173173
Response: map[string]string{
174174
"error_code": "RESOURCE_DOES_NOT_EXIST",
@@ -183,7 +183,7 @@ func TestResourceWorkspaceFileCreate_DirectoryDoesntExist(t *testing.T) {
183183
Content: dummyWorkspaceFilePayload,
184184
Path: dummyWorkspaceFilePath,
185185
Overwrite: true,
186-
Format: "AUTO",
186+
Format: "RAW",
187187
},
188188
},
189189
{
@@ -236,7 +236,7 @@ func TestResourceWorkspaceFileCreate_DirectoryCreateError(t *testing.T) {
236236
Content: dummyWorkspaceFilePayload,
237237
Path: dummyWorkspaceFilePath,
238238
Overwrite: true,
239-
Format: "AUTO",
239+
Format: "RAW",
240240
},
241241
Response: map[string]string{
242242
"error_code": "RESOURCE_DOES_NOT_EXIST",
@@ -267,7 +267,7 @@ func TestResourceWorkspaceFileCreateSource(t *testing.T) {
267267
"NPTU1BTkQgLS0tLS0tLS0tLQoKCg==",
268268
Path: "/Dashboard",
269269
Overwrite: true,
270-
Format: "AUTO",
270+
Format: "RAW",
271271
},
272272
},
273273
{
@@ -301,7 +301,7 @@ func TestResourceWorkspaceFileCreateEmptyFileSource(t *testing.T) {
301301
Content: "",
302302
Path: "/__init__.py",
303303
Overwrite: true,
304-
Format: "AUTO",
304+
Format: "RAW",
305305
ForceSendFields: []string{"Content"},
306306
},
307307
},
@@ -334,7 +334,7 @@ func TestResourceWorkspaceFileCreate_Error(t *testing.T) {
334334
Resource: "/api/2.0/workspace/import",
335335
ExpectedRequest: map[string]interface{}{
336336
"content": dummyWorkspaceFilePayload,
337-
"format": "AUTO",
337+
"format": "RAW",
338338
"overwrite": true,
339339
"path": "/path.py",
340340
},
@@ -384,7 +384,7 @@ func TestResourceWorkspaceFileUpdate(t *testing.T) {
384384
Method: "POST",
385385
Resource: "/api/2.0/workspace/import",
386386
ExpectedRequest: ws_api.Import{
387-
Format: "AUTO",
387+
Format: "RAW",
388388
Overwrite: true,
389389
Content: dummyWorkspaceFilePayload,
390390
Path: "abc",

0 commit comments

Comments
 (0)