From bd05be25f14fb66bfadbd30c020ade23f891baf8 Mon Sep 17 00:00:00 2001 From: vuong-nguyen <44292934+nkvuong@users.noreply.github.com> Date: Mon, 6 Mar 2023 13:36:32 +0000 Subject: [PATCH] Migrate `databricks_tables` data source to SDK (#2068) --- catalog/data_tables.go | 15 +++++++-------- catalog/data_tables_test.go | 10 +++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/catalog/data_tables.go b/catalog/data_tables.go index 819466e8e0..a40525735c 100644 --- a/catalog/data_tables.go +++ b/catalog/data_tables.go @@ -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 diff --git a/catalog/data_tables_test.go b/catalog/data_tables_test.go index 936d8f7ed6..3beee31bf3 100644 --- a/catalog/data_tables_test.go +++ b/catalog/data_tables_test.go @@ -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{ { @@ -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{ { @@ -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{ { @@ -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) {