From f18f243d7466b7df02d6ec8d49265139baa19e2a Mon Sep 17 00:00:00 2001 From: Karol Luszczek Date: Thu, 25 Jan 2024 01:05:55 +0100 Subject: [PATCH 1/4] data source and unit tests --- catalog/data_volumes.go | 27 ++++++++++++++++++++++ catalog/data_volumes_test.go | 44 ++++++++++++++++++++++++++++++++++++ provider/provider.go | 1 + 3 files changed, 72 insertions(+) create mode 100644 catalog/data_volumes.go create mode 100644 catalog/data_volumes_test.go diff --git a/catalog/data_volumes.go b/catalog/data_volumes.go new file mode 100644 index 0000000000..bc64c8bc05 --- /dev/null +++ b/catalog/data_volumes.go @@ -0,0 +1,27 @@ +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" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func DataSourceVolumes() *schema.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/provider/provider.go b/provider/provider.go index b5210b8a55..8c19d07711 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -89,6 +89,7 @@ func DatabricksProvider() *schema.Provider { "databricks_sql_warehouses": sql.DataSourceWarehouses(), "databricks_tables": catalog.DataSourceTables(), "databricks_views": catalog.DataSourceViews(), + "databricks_volumes": catalog.DataSourceVolumes(), "databricks_user": scim.DataSourceUser(), "databricks_zones": clusters.DataSourceClusterZones(), }, From 3ff48f3ac8bac2c79875903cc040703d24ee15e5 Mon Sep 17 00:00:00 2001 From: Karol Luszczek Date: Thu, 25 Jan 2024 01:33:20 +0100 Subject: [PATCH 2/4] acceptance test and docs --- docs/data-sources/volumes.md | 39 +++++++++++++++ internal/acceptance/data_volumes_test.go | 60 ++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 docs/data-sources/volumes.md create mode 100644 internal/acceptance/data_volumes_test.go diff --git a/docs/data-sources/volumes.md b/docs/data-sources/volumes.md new file mode 100644 index 0000000000..53d93c8b12 --- /dev/null +++ b/docs/data-sources/volumes.md @@ -0,0 +1,39 @@ +--- +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 set of [databricks_volume](../resources/volume.md) full names: *`catalog`.`schema`.`volume`* + +## Related Resources + +The following resources are used in the same context: + +* [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), + }) +} From fda25e42f9fbb9b12dea933dacc0486a4da8a2d2 Mon Sep 17 00:00:00 2001 From: Karol Luszczek Date: Mon, 29 Jan 2024 17:22:03 +0100 Subject: [PATCH 3/4] review changes to docs --- docs/data-sources/volumes.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/data-sources/volumes.md b/docs/data-sources/volumes.md index 53d93c8b12..1a1739e093 100644 --- a/docs/data-sources/volumes.md +++ b/docs/data-sources/volumes.md @@ -29,11 +29,12 @@ output "all_volumes" { This data source exports the following attributes: -* `ids` - a set of [databricks_volume](../resources/volume.md) full names: *`catalog`.`schema`.`volume`* +* `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. From dca634cd66229696729066148398e9af3caa41a3 Mon Sep 17 00:00:00 2001 From: Karol Luszczek Date: Thu, 1 Feb 2024 20:49:52 +0100 Subject: [PATCH 4/4] fixed a botched merge --- catalog/data_volumes.go | 3 +-- provider/provider.go | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/catalog/data_volumes.go b/catalog/data_volumes.go index bc64c8bc05..903e7f0bb9 100644 --- a/catalog/data_volumes.go +++ b/catalog/data_volumes.go @@ -6,10 +6,9 @@ import ( "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/databricks/terraform-provider-databricks/common" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -func DataSourceVolumes() *schema.Resource { +func DataSourceVolumes() common.Resource { return common.WorkspaceData(func(ctx context.Context, data *struct { CatalogName string `json:"catalog_name"` SchemaName string `json:"schema_name"` diff --git a/provider/provider.go b/provider/provider.go index 070ab32247..892b451090 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -89,7 +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(), + "databricks_volumes": catalog.DataSourceVolumes().ToResource(), "databricks_user": scim.DataSourceUser().ToResource(), "databricks_zones": clusters.DataSourceClusterZones().ToResource(), },