Skip to content

Commit 00538fc

Browse files
authored
Adds databricks_volumes as data source (#3150)
* data source and unit tests * acceptance test and docs * review changes to docs * fixed a botched merge
1 parent 4213468 commit 00538fc

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

catalog/data_volumes.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package catalog
2+
3+
import (
4+
"context"
5+
6+
"github.com/databricks/databricks-sdk-go"
7+
"github.com/databricks/databricks-sdk-go/service/catalog"
8+
"github.com/databricks/terraform-provider-databricks/common"
9+
)
10+
11+
func DataSourceVolumes() common.Resource {
12+
return common.WorkspaceData(func(ctx context.Context, data *struct {
13+
CatalogName string `json:"catalog_name"`
14+
SchemaName string `json:"schema_name"`
15+
Ids []string `json:"ids,omitempty" tf:"computed,slice_set"`
16+
}, w *databricks.WorkspaceClient) error {
17+
volumes, err := w.Volumes.ListAll(ctx, catalog.ListVolumesRequest{CatalogName: data.CatalogName, SchemaName: data.SchemaName})
18+
if err != nil {
19+
return err
20+
}
21+
for _, v := range volumes {
22+
data.Ids = append(data.Ids, v.FullName)
23+
}
24+
return nil
25+
})
26+
}

catalog/data_volumes_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package catalog
2+
3+
import (
4+
"testing"
5+
6+
"github.com/databricks/databricks-sdk-go/service/catalog"
7+
"github.com/databricks/terraform-provider-databricks/qa"
8+
)
9+
10+
func TestDataSourceVolumes(t *testing.T) {
11+
qa.ResourceFixture{
12+
Fixtures: []qa.HTTPFixture{
13+
{
14+
Method: "GET",
15+
Resource: "/api/2.1/unity-catalog/volumes?catalog_name=a&schema_name=b",
16+
Response: catalog.ListVolumesResponseContent{
17+
Volumes: []catalog.VolumeInfo{
18+
{
19+
FullName: "a.b.c",
20+
Name: "a",
21+
},
22+
},
23+
},
24+
},
25+
},
26+
Resource: DataSourceVolumes(),
27+
HCL: `
28+
catalog_name = "a"
29+
schema_name = "b"`,
30+
Read: true,
31+
NonWritable: true,
32+
ID: "_",
33+
}.ApplyNoError(t)
34+
}
35+
36+
func TestDataSourceVolumes_Error(t *testing.T) {
37+
qa.ResourceFixture{
38+
Fixtures: qa.HTTPFailures,
39+
Resource: DataSourceVolumes(),
40+
Read: true,
41+
NonWritable: true,
42+
ID: "_",
43+
}.ExpectError(t, "i'm a teapot")
44+
}

docs/data-sources/volumes.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
subcategory: "Unity Catalog"
3+
---
4+
# databricks_volumes Data Source
5+
6+
Retrieves a list of [databricks_volume](../resources/volume.md) ids (full names), that were created by Terraform or manually.
7+
8+
## Example Usage
9+
10+
Listing all volumes in a _things_ [databricks_schema](../resources/schema.md) of a _sandbox_ [databricks_catalog](../resources/catalog.md):
11+
12+
```hcl
13+
data "databricks_volumes" "this" {
14+
catalog_name = "sandbox"
15+
schema_name = "things"
16+
}
17+
18+
output "all_volumes" {
19+
value = data.databricks_volumes.this
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
* `catalog_name` - (Required) Name of [databricks_catalog](../resources/catalog.md)
26+
* `schema_name` - (Required) Name of [databricks_schema](../resources/schema.md)
27+
28+
## Attribute Reference
29+
30+
This data source exports the following attributes:
31+
32+
* `ids` - a list of [databricks_volume](../resources/volume.md) full names: *`catalog`.`schema`.`volume`*
33+
34+
## Related Resources
35+
36+
The following resources are used in the same context:
37+
38+
* [databricks_volume](../resources/volume.md) to manage volumes within Unity Catalog.
39+
* [databricks_schema](../resources/schema.md) to manage schemas within Unity Catalog.
40+
* [databricks_catalog](../resources/catalog.md) to manage catalogs within Unity Catalog.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package acceptance
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func checkDataSourceVolumesPopulated(t *testing.T) func(s *terraform.State) error {
13+
return func(s *terraform.State) error {
14+
_, ok := s.Modules[0].Resources["data.databricks_volumes.this"]
15+
require.True(t, ok, "data.databricks_volumes.this has to be there")
16+
num_volumes, _ := strconv.Atoi(s.Modules[0].Outputs["volumes"].Value.(string))
17+
assert.GreaterOrEqual(t, num_volumes, 1)
18+
return nil
19+
}
20+
}
21+
func TestUcAccDataSourceVolumes(t *testing.T) {
22+
unityWorkspaceLevel(t, step{
23+
Template: `
24+
resource "databricks_catalog" "sandbox" {
25+
name = "sandbox{var.RANDOM}"
26+
comment = "this catalog is managed by terraform"
27+
properties = {
28+
purpose = "testing"
29+
}
30+
}
31+
32+
resource "databricks_schema" "things" {
33+
catalog_name = databricks_catalog.sandbox.id
34+
name = "things{var.RANDOM}"
35+
comment = "this database is managed by terraform"
36+
properties = {
37+
kind = "various"
38+
}
39+
}
40+
41+
resource "databricks_volume" "this" {
42+
name = "volume_data_source_test"
43+
catalog_name = databricks_catalog.sandbox.name
44+
schema_name = databricks_schema.things.name
45+
volume_type = "MANAGED"
46+
}
47+
48+
data "databricks_volumes" "this" {
49+
catalog_name = databricks_catalog.sandbox.name
50+
schema_name = databricks_schema.things.name
51+
depends_on = [ databricks_volume.this ]
52+
}
53+
54+
output "volumes" {
55+
value = length(data.databricks_volumes.this.ids)
56+
}
57+
`,
58+
Check: checkDataSourceVolumesPopulated(t),
59+
})
60+
}

provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func DatabricksProvider() *schema.Provider {
8989
"databricks_sql_warehouses": sql.DataSourceWarehouses().ToResource(),
9090
"databricks_tables": catalog.DataSourceTables().ToResource(),
9191
"databricks_views": catalog.DataSourceViews().ToResource(),
92+
"databricks_volumes": catalog.DataSourceVolumes().ToResource(),
9293
"databricks_user": scim.DataSourceUser().ToResource(),
9394
"databricks_zones": clusters.DataSourceClusterZones().ToResource(),
9495
},

0 commit comments

Comments
 (0)