Skip to content

Commit

Permalink
Migrate databricks_tables data source to SDK (#2068)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkvuong committed Mar 6, 2023
1 parent 29cea04 commit bd05be2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
15 changes: 7 additions & 8 deletions catalog/data_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ package catalog
import (
"context"

"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/unitycatalog"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceTables() *schema.Resource {
type tablesData struct {
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"`
}
return common.DataResource(tablesData{}, func(ctx context.Context, e any, c *common.DatabricksClient) error {
data := e.(*tablesData)
tablesAPI := NewTablesAPI(ctx, c)
tables, err := tablesAPI.listTables(data.CatalogName, data.SchemaName)
}, w *databricks.WorkspaceClient) error {
tables, err := w.Tables.ListAll(ctx, unitycatalog.ListTablesRequest{CatalogName: data.CatalogName, SchemaName: data.SchemaName})
if err != nil {
return err
}
for _, v := range tables.Tables {
for _, v := range tables {
if v.TableType != "VIEW" {
data.Ids = append(data.Ids, v.FullName())
data.Ids = append(data.Ids, v.Name)
}
}
return nil
Expand Down
10 changes: 5 additions & 5 deletions catalog/data_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestTablesData(t *testing.T) {
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/tables/?catalog_name=a&schema_name=b",
Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b",
Response: Tables{
Tables: []TableInfo{
{
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestTablesDataIssue1264(t *testing.T) {
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/tables/?catalog_name=a&schema_name=b",
Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b",
Response: Tables{
Tables: []TableInfo{
{
Expand All @@ -68,13 +68,13 @@ func TestTablesDataIssue1264(t *testing.T) {
require.NoError(t, err)
s := d.Get("ids").(*schema.Set)
assert.Equal(t, 2, s.Len())
assert.True(t, s.Contains("..a"))
assert.True(t, s.Contains("a"))

d, err = qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/tables/?catalog_name=a&schema_name=b",
Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b",
Response: Tables{
Tables: []TableInfo{
{
Expand All @@ -98,7 +98,7 @@ func TestTablesDataIssue1264(t *testing.T) {
require.NoError(t, err)
s = d.Get("ids").(*schema.Set)
assert.Equal(t, 2, s.Len())
assert.True(t, s.Contains("..c"))
assert.True(t, s.Contains("c"))
}

func TestTablesData_Error(t *testing.T) {
Expand Down

0 comments on commit bd05be2

Please sign in to comment.