Skip to content

Commit 3ebd419

Browse files
authored
Added datarbicks_directory data source (#1902)
* Add `datarbicks_directory` data source It will be necessary for adding `parent` attribute to SQL queries/dashboards, especially in exporter * improve test coverage for `databricks_notebook` data source
1 parent ebbd6d3 commit 3ebd419

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

docs/data-sources/directory.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
subcategory: "Workspace"
3+
---
4+
# databricks_directory Data Source
5+
6+
-> **Note** If you have a fully automated setup with workspaces created by [databricks_mws_workspaces](../resources/mws_workspaces.md) or [azurerm_databricks_workspace](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/databricks_workspace), please make sure to add [depends_on attribute](../index.md#data-resources-and-authentication-is-not-configured-errors) in order to prevent _authentication is not configured for provider_ errors.
7+
8+
This data source allows to get information about a directory in a Databricks Workspace.
9+
10+
## Example Usage
11+
12+
```hcl
13+
data "databricks_notebook" "prod" {
14+
path = "/Production"
15+
}
16+
```
17+
18+
## Argument Reference
19+
20+
* `path` - (Required) Path to a directory in the workspace
21+
22+
## Attribute Reference
23+
24+
This data source exports the following attributes:
25+
26+
* `object_id` - directory object ID

provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func DatabricksProvider() *schema.Provider {
4646
"databricks_current_user": scim.DataSourceCurrentUser(),
4747
"databricks_dbfs_file": storage.DataSourceDbfsFile(),
4848
"databricks_dbfs_file_paths": storage.DataSourceDbfsFilePaths(),
49+
"databricks_directory": workspace.DataSourceDirectory(),
4950
"databricks_group": scim.DataSourceGroup(),
5051
"databricks_jobs": jobs.DataSourceJobs(),
5152
"databricks_job": jobs.DataSourceJob(),

workspace/data_directory.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package workspace
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
// DataSourceDirectory ...
11+
func DataSourceDirectory() *schema.Resource {
12+
s := map[string]*schema.Schema{
13+
"path": {
14+
Type: schema.TypeString,
15+
Required: true,
16+
ForceNew: true,
17+
},
18+
"object_id": {
19+
Type: schema.TypeInt,
20+
Optional: true,
21+
Computed: true,
22+
},
23+
}
24+
return &schema.Resource{
25+
Schema: s,
26+
ReadContext: func(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
27+
notebooksAPI := NewNotebooksAPI(ctx, m)
28+
path := d.Get("path").(string)
29+
data, err := notebooksAPI.Read(path)
30+
if err != nil {
31+
return diag.FromErr(err)
32+
}
33+
if data.ObjectType != Directory { // should we support Repos as well?
34+
return diag.Errorf("'%s' isn't a directory", path)
35+
}
36+
d.SetId(data.Path)
37+
d.Set("object_id", data.ObjectID)
38+
d.Set("path", path)
39+
return nil
40+
},
41+
}
42+
}

workspace/data_directory_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package workspace
2+
3+
import (
4+
"testing"
5+
6+
"github.com/databricks/terraform-provider-databricks/qa"
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestDataSourceDirectory(t *testing.T) {
12+
d, err := qa.ResourceFixture{
13+
Fixtures: []qa.HTTPFixture{
14+
{
15+
Method: "GET",
16+
Resource: "/api/2.0/workspace/get-status?path=%2Fa%2Fb%2Fc",
17+
Response: ObjectStatus{
18+
ObjectID: 987,
19+
ObjectType: "DIRECTORY",
20+
Path: "/a/b/c",
21+
},
22+
},
23+
},
24+
Read: true,
25+
NonWritable: true,
26+
Resource: DataSourceDirectory(),
27+
ID: ".",
28+
State: map[string]any{
29+
"path": "/a/b/c",
30+
},
31+
}.Apply(t)
32+
require.NoError(t, err)
33+
assert.Equal(t, "/a/b/c", d.Id())
34+
assert.Equal(t, 987, d.Get("object_id").(int))
35+
}
36+
37+
func TestDataSourceDirectory_NotDirectory(t *testing.T) {
38+
qa.ResourceFixture{
39+
Fixtures: []qa.HTTPFixture{
40+
{
41+
Method: "GET",
42+
Resource: "/api/2.0/workspace/get-status?path=%2Fa%2Fb%2Fc",
43+
Response: ObjectStatus{
44+
ObjectID: 987,
45+
Language: "PYTHON",
46+
ObjectType: "NOTEBOOK",
47+
Path: "/a/b/c",
48+
},
49+
},
50+
},
51+
Read: true,
52+
NonWritable: true,
53+
Resource: DataSourceDirectory(),
54+
ID: ".",
55+
State: map[string]any{
56+
"path": "/a/b/c",
57+
},
58+
}.ExpectError(t, "'/a/b/c' isn't a directory")
59+
}
60+
61+
func TestDataSourceDirectory_Error(t *testing.T) {
62+
qa.ResourceFixture{
63+
Fixtures: qa.HTTPFailures,
64+
Read: true,
65+
NonWritable: true,
66+
Resource: DataSourceDirectory(),
67+
ID: ".",
68+
State: map[string]any{
69+
"path": "/a/b/c",
70+
},
71+
}.ExpectError(t, "I'm a teapot")
72+
}

workspace/data_notebook_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package workspace
33
import (
44
"testing"
55

6+
"github.com/databricks/terraform-provider-databricks/common"
67
"github.com/databricks/terraform-provider-databricks/qa"
78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
@@ -42,3 +43,52 @@ func TestDataSourceNotebook(t *testing.T) {
4243
assert.Equal(t, "/a/b/c", d.Id())
4344
assert.Equal(t, "SGVsbG8gd29ybGQK", d.Get("content"))
4445
}
46+
47+
func TestDataSourceNotebook_ErrorExport(t *testing.T) {
48+
qa.ResourceFixture{
49+
Fixtures: qa.HTTPFailures,
50+
Read: true,
51+
NonWritable: true,
52+
Resource: DataSourceNotebook(),
53+
ID: ".",
54+
State: map[string]any{
55+
"path": "/a/b/c",
56+
"format": "SOURCE",
57+
},
58+
}.ExpectError(t, "I'm a teapot")
59+
}
60+
61+
func TestDataSourceNotebook_ErrorStatus(t *testing.T) {
62+
qa.ResourceFixture{
63+
Fixtures: []qa.HTTPFixture{
64+
{
65+
Method: "GET",
66+
Resource: "/api/2.0/workspace/get-status?path=%2Fa%2Fb%2Fc",
67+
Response: ObjectStatus{
68+
ObjectID: 987,
69+
Language: "PYTHON",
70+
ObjectType: "NOTEBOOK",
71+
Path: "/a/b/c",
72+
},
73+
},
74+
{
75+
Method: "GET",
76+
Resource: "/api/2.0/workspace/export?format=SOURCE&path=%2Fa%2Fb%2Fc",
77+
Status: 401,
78+
Response: common.APIError{
79+
ErrorCode: "Unauthorized",
80+
StatusCode: 401,
81+
Message: "Unauthorized",
82+
},
83+
},
84+
},
85+
Read: true,
86+
NonWritable: true,
87+
Resource: DataSourceNotebook(),
88+
ID: ".",
89+
State: map[string]any{
90+
"path": "/a/b/c",
91+
"format": "SOURCE",
92+
},
93+
}.ExpectError(t, "Unauthorized")
94+
}

0 commit comments

Comments
 (0)