Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds databricks_volumes as data source #3150

Merged
merged 7 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions catalog/data_volumes.go
Original file line number Diff line number Diff line change
@@ -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
})
}
44 changes: 44 additions & 0 deletions catalog/data_volumes_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
40 changes: 40 additions & 0 deletions docs/data-sources/volumes.md
Original file line number Diff line number Diff line change
@@ -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.
alexott marked this conversation as resolved.
Show resolved Hide resolved
* [databricks_catalog](../resources/catalog.md) to manage catalogs within Unity Catalog.
60 changes: 60 additions & 0 deletions internal/acceptance/data_volumes_test.go
Original file line number Diff line number Diff line change
@@ -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),
})
}
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down
Loading