diff --git a/catalog/data_volumes.go b/catalog/data_volumes.go new file mode 100644 index 0000000000..903e7f0bb9 --- /dev/null +++ b/catalog/data_volumes.go @@ -0,0 +1,26 @@ +package catalog + +import ( + "context" + + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/service/catalog" + "github.com/databricks/terraform-provider-databricks/common" +) + +func DataSourceVolumes() common.Resource { + return common.WorkspaceData(func(ctx context.Context, data *struct { + CatalogName string `json:"catalog_name"` + SchemaName string `json:"schema_name"` + Ids []string `json:"ids,omitempty" tf:"computed,slice_set"` + }, w *databricks.WorkspaceClient) error { + volumes, err := w.Volumes.ListAll(ctx, catalog.ListVolumesRequest{CatalogName: data.CatalogName, SchemaName: data.SchemaName}) + if err != nil { + return err + } + for _, v := range volumes { + data.Ids = append(data.Ids, v.FullName) + } + return nil + }) +} diff --git a/catalog/data_volumes_test.go b/catalog/data_volumes_test.go new file mode 100644 index 0000000000..d570dba2f1 --- /dev/null +++ b/catalog/data_volumes_test.go @@ -0,0 +1,44 @@ +package catalog + +import ( + "testing" + + "github.com/databricks/databricks-sdk-go/service/catalog" + "github.com/databricks/terraform-provider-databricks/qa" +) + +func TestDataSourceVolumes(t *testing.T) { + qa.ResourceFixture{ + Fixtures: []qa.HTTPFixture{ + { + Method: "GET", + Resource: "/api/2.1/unity-catalog/volumes?catalog_name=a&schema_name=b", + Response: catalog.ListVolumesResponseContent{ + Volumes: []catalog.VolumeInfo{ + { + FullName: "a.b.c", + Name: "a", + }, + }, + }, + }, + }, + Resource: DataSourceVolumes(), + HCL: ` + catalog_name = "a" + schema_name = "b"`, + Read: true, + NonWritable: true, + ID: "_", + }.ApplyNoError(t) +} + +func TestDataSourceVolumes_Error(t *testing.T) { + qa.ResourceFixture{ + Fixtures: qa.HTTPFailures, + Resource: DataSourceVolumes(), + Read: true, + NonWritable: true, + ID: "_", + }.ExpectError(t, "i'm a teapot") +} diff --git a/docs/data-sources/volumes.md b/docs/data-sources/volumes.md new file mode 100644 index 0000000000..1a1739e093 --- /dev/null +++ b/docs/data-sources/volumes.md @@ -0,0 +1,40 @@ +--- +subcategory: "Unity Catalog" +--- +# databricks_volumes Data Source + +Retrieves a list of [databricks_volume](../resources/volume.md) ids (full names), that were created by Terraform or manually. + +## Example Usage + +Listing all volumes in a _things_ [databricks_schema](../resources/schema.md) of a _sandbox_ [databricks_catalog](../resources/catalog.md): + +```hcl +data "databricks_volumes" "this" { + catalog_name = "sandbox" + schema_name = "things" +} + +output "all_volumes" { + value = data.databricks_volumes.this +} +``` + +## Argument Reference + +* `catalog_name` - (Required) Name of [databricks_catalog](../resources/catalog.md) +* `schema_name` - (Required) Name of [databricks_schema](../resources/schema.md) + +## Attribute Reference + +This data source exports the following attributes: + +* `ids` - a list of [databricks_volume](../resources/volume.md) full names: *`catalog`.`schema`.`volume`* + +## Related Resources + +The following resources are used in the same context: + +* [databricks_volume](../resources/volume.md) to manage volumes within Unity Catalog. +* [databricks_schema](../resources/schema.md) to manage schemas within Unity Catalog. +* [databricks_catalog](../resources/catalog.md) to manage catalogs within Unity Catalog. diff --git a/internal/acceptance/data_volumes_test.go b/internal/acceptance/data_volumes_test.go new file mode 100644 index 0000000000..1aafe87862 --- /dev/null +++ b/internal/acceptance/data_volumes_test.go @@ -0,0 +1,60 @@ +package acceptance + +import ( + "strconv" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func checkDataSourceVolumesPopulated(t *testing.T) func(s *terraform.State) error { + return func(s *terraform.State) error { + _, ok := s.Modules[0].Resources["data.databricks_volumes.this"] + require.True(t, ok, "data.databricks_volumes.this has to be there") + num_volumes, _ := strconv.Atoi(s.Modules[0].Outputs["volumes"].Value.(string)) + assert.GreaterOrEqual(t, num_volumes, 1) + return nil + } +} +func TestUcAccDataSourceVolumes(t *testing.T) { + unityWorkspaceLevel(t, step{ + Template: ` + resource "databricks_catalog" "sandbox" { + name = "sandbox{var.RANDOM}" + comment = "this catalog is managed by terraform" + properties = { + purpose = "testing" + } + } + + resource "databricks_schema" "things" { + catalog_name = databricks_catalog.sandbox.id + name = "things{var.RANDOM}" + comment = "this database is managed by terraform" + properties = { + kind = "various" + } + } + + resource "databricks_volume" "this" { + name = "volume_data_source_test" + catalog_name = databricks_catalog.sandbox.name + schema_name = databricks_schema.things.name + volume_type = "MANAGED" + } + + data "databricks_volumes" "this" { + catalog_name = databricks_catalog.sandbox.name + schema_name = databricks_schema.things.name + depends_on = [ databricks_volume.this ] + } + + output "volumes" { + value = length(data.databricks_volumes.this.ids) + } + `, + Check: checkDataSourceVolumesPopulated(t), + }) +} diff --git a/provider/provider.go b/provider/provider.go index 2e94459399..892b451090 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -89,6 +89,7 @@ func DatabricksProvider() *schema.Provider { "databricks_sql_warehouses": sql.DataSourceWarehouses().ToResource(), "databricks_tables": catalog.DataSourceTables().ToResource(), "databricks_views": catalog.DataSourceViews().ToResource(), + "databricks_volumes": catalog.DataSourceVolumes().ToResource(), "databricks_user": scim.DataSourceUser().ToResource(), "databricks_zones": clusters.DataSourceClusterZones().ToResource(), },