Skip to content

Added DBC format support for databricks_notebook #989

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

Merged
merged 2 commits into from
Dec 20, 2021
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Version changelog

## 0.4.2

* Added `DBC` format support for `databricks_notebook` ([#989](https://github.com/databrickslabs/terraform-provider-databricks/pull/989)).

## 0.4.1

* Added `databricks_library` resource to install library on `databricks_cluster` ([#904](https://github.com/databrickslabs/terraform-provider-databricks/pull/904)).
Expand Down
11 changes: 10 additions & 1 deletion docs/resources/notebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ resource "databricks_notebook" "notebook" {
language = "PYTHON"
}
```


You can also manage [Databricks Archives](https://docs.databricks.com/notebooks/notebooks-manage.html#databricks-archive) to import the whole folders of notebooks statically. Whenever you update `.dbc` file, terraform-managed notebook folder is removed and replaced with contents of the new `.dbc` file. You are strongly advised to use `.dbc` format only with `source` attribute of the resource:

```hcl
resource "databricks_notebook" "lesson" {
source = "${path.module}/IntroNotebooks.dbc"
path = "/Shared/Intro"
}
```

## Argument Reference

-> **Note** Notebook on Databricks workspace would only be changed, if Terraform stage did change. This means that any manual changes to managed notebook won't be overwritten by Terraform, if there's no local change to notebook sources. Notebooks are identified by their path, so changing notebook's name manually on the workspace and then applying Terraform state would result in creation of notebook from Terraform state.
Expand Down
2 changes: 1 addition & 1 deletion permissions/acceptance/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func TestAccPermissionsNotebooks(t *testing.T) {

notebookPath := fmt.Sprintf("%s/Dummy", notebookDir)

err = workspaceAPI.Create(workspace.ImportRequest{
err = workspaceAPI.Create(workspace.ImportPath{
Path: notebookPath,
Content: "MSsx",
Format: "SOURCE",
Expand Down
8 changes: 4 additions & 4 deletions workspace/data_notebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func DataSourceNotebook() *schema.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(DBC),
string(Source),
string(HTML),
"DBC",
"SOURCE",
"HTML",
}, false),
},
"content": {
Expand Down Expand Up @@ -53,7 +53,7 @@ func DataSourceNotebook() *schema.Resource {
notebooksAPI := NewNotebooksAPI(ctx, m)
path := d.Get("path").(string)
format := d.Get("format").(string)
notebookContent, err := notebooksAPI.Export(path, ExportFormat(format))
notebookContent, err := notebooksAPI.Export(path, format)
if err != nil {
return diag.FromErr(err)
}
Expand Down
6 changes: 0 additions & 6 deletions workspace/data_notebook_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ func DataSourceNotebookPaths() *schema.Resource {
return diag.FromErr(err)
}
d.SetId(path)
if err = d.Set("recursive", recursive); err != nil {
return diag.FromErr(err)
}
if err = d.Set("path", path); err != nil {
return diag.FromErr(err)
}
var notebookPathList []map[string]string
for _, v := range notebookList {
notebookPathMap := map[string]string{}
Expand Down
43 changes: 37 additions & 6 deletions workspace/data_notebook_paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"testing"

"github.com/databrickslabs/terraform-provider-databricks/qa"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDataSourceNotebookPaths(t *testing.T) {
d, err := qa.ResourceFixture{
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Expand Down Expand Up @@ -53,7 +51,40 @@ func TestDataSourceNotebookPaths(t *testing.T) {
"path": "/a/b/c",
"recursive": true,
},
}.Apply(t)
require.NoError(t, err)
assert.Equal(t, "/a/b/c", d.Id())
}.ApplyNoError(t)
}

func TestDataSourceNotebookPaths_NoRecursive(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.0/workspace/list?path=%2Fa%2Fb%2Fc",
Response: objectList{
Objects: []ObjectStatus{
{
ObjectID: 988,
ObjectType: Notebook,
Language: Python,
Path: "/a/b/c/d/e",
},
{
ObjectID: 989,
ObjectType: Notebook,
Language: SQL,
Path: "/a/b/c/d/f",
},
},
},
},
},
Read: true,
NonWritable: true,
Resource: DataSourceNotebookPaths(),
ID: ".",
State: map[string]interface{}{
"path": "/a/b/c",
"recursive": false,
},
}.ApplyNoError(t)
}
2 changes: 1 addition & 1 deletion workspace/data_notebook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestDataSourceNotebook(t *testing.T) {
{
Method: "GET",
Resource: "/api/2.0/workspace/export?format=SOURCE&path=%2Fa%2Fb%2Fc",
Response: NotebookContent{
Response: ExportPath{
Content: "SGVsbG8gd29ybGQK",
},
},
Expand Down
1 change: 0 additions & 1 deletion workspace/file_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func ReadContent(d *schema.ResourceData) (content []byte, err error) {
if err != nil {
return
}
// TODO: file size
d.Set("md5", fmt.Sprintf("%x", md5.Sum(content)))
log.Printf("[INFO] Setting file content hash to %s", d.Get("md5"))
return
Expand Down
4 changes: 2 additions & 2 deletions workspace/resource_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestResourceDirectoryDelete(t *testing.T) {
Method: http.MethodPost,
Resource: "/api/2.0/workspace/delete",
Status: http.StatusOK,
ExpectedRequest: NotebookDeleteRequest{Path: path, Recursive: delete_recursive},
ExpectedRequest: DeletePath{Path: path, Recursive: delete_recursive},
},
},
Resource: ResourceDirectory(),
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestResourceDirectoryDelete_Error(t *testing.T) {
{
Method: "POST",
Resource: "/api/2.0/workspace/delete",
ExpectedRequest: NotebookDeleteRequest{Path: path, Recursive: false},
ExpectedRequest: DeletePath{Path: path, Recursive: false},
Response: common.APIErrorBody{
ErrorCode: "INVALID_REQUEST",
Message: "Internal error happened",
Expand Down
Loading