Skip to content

Commit

Permalink
Add support for Jupyter/.ipynb files in "databricks_notebook" resource
Browse files Browse the repository at this point in the history
Fixes #1647
  • Loading branch information
AlexC committed Nov 25, 2022
1 parent 67a8760 commit 1632d57
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/resources/notebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This resource allows you to manage [Databricks Notebooks](https://docs.databrick

## Example Usage

You can declare Terraform-managed notebook by specifying `source` attribute of corresponding local file. Only `.scala`, `.py`, `.sql` and `.r` extensions are supported, if you would like to omit the `language` attribute.
You can declare Terraform-managed notebook by specifying `source` attribute of corresponding local file. Only `.scala`, `.py`, `.sql`, `.r`, and `.ipynb` extensions are supported, if you would like to omit the `language` attribute.

```hcl
data "databricks_current_user" "me" {
Expand Down Expand Up @@ -48,10 +48,10 @@ resource "databricks_notebook" "lesson" {

The size of a notebook source code must not exceed a few megabytes. The following arguments are supported:

* `path` - (Required) The absolute path of the notebook or directory, beginning with "/", e.g. "/Demo".
* `path` - (Required) The absolute path of the notebook or directory, beginning with "/", e.g. "/Demo".
* `source` - Path to notebook in source code format on local filesystem. Conflicts with `content_base64`.
* `content_base64` - The base64-encoded notebook source code. Conflicts with `source`. Use of `content_base64` is discouraged, as it's increasing memory footprint of Terraform state and should only be used in exceptional circumstances, like creating a notebook with configuration properties for a data pipeline.
* `language` - (required with `content_base64`) One of `SCALA`, `PYTHON`, `SQL`, `R`.
* `language` - (required with `content_base64`) One of `SCALA`, `PYTHON`, `SQL`, `R`. `JUPYTER`,

## Attribute Reference

Expand Down Expand Up @@ -83,7 +83,7 @@ The following resources are often used in the same context:
* [databricks_job](job.md) to manage [Databricks Jobs](https://docs.databricks.com/jobs.html) to run non-interactive code in a [databricks_cluster](cluster.md).
* [databricks_notebook](../data-sources/notebook.md) data to export a notebook from Databricks Workspace.
* [databricks_notebook_paths](../data-sources/notebook_paths.md) data to list notebooks in Databricks Workspace.
* [databricks_pipeline](pipeline.md) to deploy [Delta Live Tables](https://docs.databricks.com/data-engineering/delta-live-tables/index.html).
* [databricks_pipeline](pipeline.md) to deploy [Delta Live Tables](https://docs.databricks.com/data-engineering/delta-live-tables/index.html).
* [databricks_repo](repo.md) to manage [Databricks Repos](https://docs.databricks.com/repos.html).
* [databricks_secret](secret.md) to manage [secrets](https://docs.databricks.com/security/secrets/index.html#secrets-user-guide) in Databricks workspace.
* [databricks_secret_acl](secret_acl.md) to manage access to [secrets](https://docs.databricks.com/security/secrets/index.html#secrets-user-guide) in Databricks workspace.
Expand Down
3 changes: 3 additions & 0 deletions workspace/resource_notebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
Python string = "PYTHON"
SQL string = "SQL"
R string = "R"
Jupyter string = "JUPYTER"
)

type notebookLanguageFormat struct {
Expand All @@ -34,6 +35,7 @@ var extMap = map[string]notebookLanguageFormat{
".py": {"PYTHON", "SOURCE", true},
".sql": {"SQL", "SOURCE", true},
".r": {"R", "SOURCE", true},
".ipynb": {"JUPYTER", "SOURCE", true},
".dbc": {"", "DBC", false},
}

Expand Down Expand Up @@ -192,6 +194,7 @@ func ResourceNotebook() *schema.Resource {
Python,
R,
SQL,
Jupyter,
}, false),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
source := d.Get("source").(string)
Expand Down
55 changes: 55 additions & 0 deletions workspace/resource_notebook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,61 @@ func TestResourceNotebookCreate(t *testing.T) {
assert.Equal(t, "/foo/path.py", d.Id())
}

func TestResourceNotebookCreateSource_Jupyter(t *testing.T) {
d, err := qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: http.MethodPost,
Resource: "/api/2.0/workspace/import",
ExpectedRequest: ImportPath{
Content: "eyJjZWxscyI6W3siY2VsbF90eXBlIjoiY29kZSIsInNvdXJjZSI6WyJwc" +
"mludChcImhlbGxvIHdvcmxkXCIpIl0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0X" +
"21pbm9yIjowfQo=",
Path: "/Mars",
Language: "JUPYTER",
Overwrite: true,
Format: "SOURCE",
},
},
{
Method: http.MethodGet,
Resource: "/api/2.0/workspace/get-status?path=%2FMars",
Response: ObjectStatus{
ObjectID: 4567,
ObjectType: "NOTEBOOK",
Path: "/Mars",
Language: "JUPYTER",
},
},
},
Resource: ResourceNotebook(),
State: map[string]any{
"source": "acceptance/testdata/tf-test-jupyter.ipynb",
"path": "/Mars",
},
Create: true,
}.Apply(t)
assert.NoError(t, err, err)
assert.Equal(t, "/Mars", d.Id())
}

func TestResourceNotebookCreateSource(t *testing.T) {
d, err := qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
Expand Down

0 comments on commit 1632d57

Please sign in to comment.