Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Upload content databricks_workspace_file using raw format #4200

Merged
merged 2 commits into from
Nov 8, 2024
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
30 changes: 20 additions & 10 deletions internal/acceptance/permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,17 +466,22 @@ func TestAccPermissions_WorkspaceFile_Path(t *testing.T) {
}
resource "databricks_workspace_file" "this" {
source = "{var.CWD}/../../storage/testdata/tf-test-python.py"
path = "${databricks_directory.this.path}/test_notebook"
path = "${databricks_directory.this.path}/test_ws_file"
}`
WorkspaceLevel(t, Step{
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id", groupPermissions("CAN_RUN")),
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id",
groupPermissions("CAN_RUN")),
}, Step{
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id", currentPrincipalPermission(t, "CAN_MANAGE"), allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id",
currentPrincipalPermission(t, "CAN_MANAGE"),
allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
}, Step{
// The current user can be removed from permissions since they inherit permissions from the directory they created.
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id", allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id",
allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
}, Step{
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id", currentPrincipalPermission(t, "CAN_READ")),
Template: workspaceFile + makePermissionsTestStage("workspace_file_path", "databricks_workspace_file.this.id",
currentPrincipalPermission(t, "CAN_READ")),
ExpectError: regexp.MustCompile("cannot remove management permissions for the current user for file, allowed levels: CAN_MANAGE"),
})
}
Expand All @@ -489,17 +494,22 @@ func TestAccPermissions_WorkspaceFile_Id(t *testing.T) {
}
resource "databricks_workspace_file" "this" {
source = "{var.CWD}/../../storage/testdata/tf-test-python.py"
path = "${databricks_directory.this.path}/test_notebook"
path = "${databricks_directory.this.path}/test_ws_file"
}`
WorkspaceLevel(t, Step{
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id", groupPermissions("CAN_RUN")),
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id",
groupPermissions("CAN_RUN")),
}, Step{
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")),
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")),
}, Step{
// The current user can be removed from permissions since they inherit permissions from the directory they created.
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id", allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id",
allPrincipalPermissions("CAN_RUN", "CAN_READ", "CAN_EDIT", "CAN_MANAGE")),
}, Step{
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id", currentPrincipalPermission(t, "CAN_READ")),
Template: workspaceFile + makePermissionsTestStage("workspace_file_id", "databricks_workspace_file.this.object_id",
currentPrincipalPermission(t, "CAN_READ")),
ExpectError: regexp.MustCompile("cannot remove management permissions for the current user for file, allowed levels: CAN_MANAGE"),
})
}
Expand Down
9 changes: 9 additions & 0 deletions internal/acceptance/workspace_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ func TestAccWorkspaceFileEmptyFile(t *testing.T) {
})
}

func TestAccWorkspaceFileZipFile(t *testing.T) {
WorkspaceLevel(t, Step{
Template: `resource "databricks_workspace_file" "zipfile" {
source = "{var.CWD}/../../workspace/acceptance/testdata/zipfile.zip"
path = "/Shared/provider-test/zipfile_{var.RANDOM}.zip"
}`,
})
}

func TestAccWorkspaceFileBase64(t *testing.T) {
WorkspaceLevel(t, Step{
Template: `resource "databricks_workspace_file" "this2" {
Expand Down
Binary file added workspace/acceptance/testdata/zipfile.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions workspace/resource_workspace_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func ResourceWorkspaceFile() common.Resource {
path := d.Get("path").(string)
importReq := workspace.Import{
Content: base64.StdEncoding.EncodeToString(content),
Format: workspace.ImportFormatAuto,
Format: workspace.ImportFormatRaw,
Path: path,
Overwrite: true,
ForceSendFields: []string{"Content"},
Expand Down Expand Up @@ -93,7 +93,7 @@ func ResourceWorkspaceFile() common.Resource {
}
return client.Workspace.Import(ctx, workspace.Import{
Content: base64.StdEncoding.EncodeToString(content),
Format: workspace.ImportFormatAuto,
Format: workspace.ImportFormatRaw,
Overwrite: true,
Path: d.Id(),
ForceSendFields: []string{"Content"},
Expand Down
16 changes: 8 additions & 8 deletions workspace/resource_workspace_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestResourceWorkspaceFileCreate_DirectoryExist(t *testing.T) {
Content: dummyWorkspaceFilePayload,
Path: dummyWorkspaceFilePath,
Overwrite: true,
Format: "AUTO",
Format: "RAW",
},
},
{
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestResourceWorkspaceFileCreate_DirectoryDoesntExist(t *testing.T) {
Content: dummyWorkspaceFilePayload,
Path: dummyWorkspaceFilePath,
Overwrite: true,
Format: "AUTO",
Format: "RAW",
},
Response: map[string]string{
"error_code": "RESOURCE_DOES_NOT_EXIST",
Expand All @@ -183,7 +183,7 @@ func TestResourceWorkspaceFileCreate_DirectoryDoesntExist(t *testing.T) {
Content: dummyWorkspaceFilePayload,
Path: dummyWorkspaceFilePath,
Overwrite: true,
Format: "AUTO",
Format: "RAW",
},
},
{
Expand Down Expand Up @@ -236,7 +236,7 @@ func TestResourceWorkspaceFileCreate_DirectoryCreateError(t *testing.T) {
Content: dummyWorkspaceFilePayload,
Path: dummyWorkspaceFilePath,
Overwrite: true,
Format: "AUTO",
Format: "RAW",
},
Response: map[string]string{
"error_code": "RESOURCE_DOES_NOT_EXIST",
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestResourceWorkspaceFileCreateSource(t *testing.T) {
"NPTU1BTkQgLS0tLS0tLS0tLQoKCg==",
Path: "/Dashboard",
Overwrite: true,
Format: "AUTO",
Format: "RAW",
},
},
{
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestResourceWorkspaceFileCreateEmptyFileSource(t *testing.T) {
Content: "",
Path: "/__init__.py",
Overwrite: true,
Format: "AUTO",
Format: "RAW",
ForceSendFields: []string{"Content"},
},
},
Expand Down Expand Up @@ -334,7 +334,7 @@ func TestResourceWorkspaceFileCreate_Error(t *testing.T) {
Resource: "/api/2.0/workspace/import",
ExpectedRequest: map[string]interface{}{
"content": dummyWorkspaceFilePayload,
"format": "AUTO",
"format": "RAW",
"overwrite": true,
"path": "/path.py",
},
Expand Down Expand Up @@ -384,7 +384,7 @@ func TestResourceWorkspaceFileUpdate(t *testing.T) {
Method: "POST",
Resource: "/api/2.0/workspace/import",
ExpectedRequest: ws_api.Import{
Format: "AUTO",
Format: "RAW",
Overwrite: true,
Content: dummyWorkspaceFilePayload,
Path: "abc",
Expand Down
Loading