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

Add a databricks_table data source #3170

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
24 changes: 24 additions & 0 deletions catalog/data_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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"
)

type tableParams struct {
FullName string `json:"full_name" tf:"computed,optional`
alexott marked this conversation as resolved.
Show resolved Hide resolved
}

func DataSourceTable() *schema.Resource {
return common.WorkspaceDataWithParams(func(ctx context.Context, data tableParams, w *databricks.WorkspaceClient) (*catalog.TableInfo, error) {
table_info, err := w.Tables.Get(ctx, catalog.GetTableRequest{FullName: data.FullName})
if err != nil {
return nil, err
}
return table_info, nil
jdavidheiser marked this conversation as resolved.
Show resolved Hide resolved
})
}
48 changes: 48 additions & 0 deletions catalog/data_table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package catalog

import (
"testing"

"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/terraform-provider-databricks/qa"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTableData(t *testing.T) {
d, err := qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use MockWorkspaceClientFunc instead of Fixtures

MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
    e:= w.GetMockTablesAPI().EXPECT()
    e.GetByFullName(mock.Anything, "a.b.c").Return(&catalog.TableInfo{})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pretty much copy pasted patterns I saw elsewhere in this repo - I don't really work in golang so things like this are outside of my comfort zone. I wonder if it might be a lot faster for someone with more familiarity to clean up the test style in a separate pass?

{
Method: "GET",
Resource: "/api/2.1/unity-catalog/tables/a.b.c?",
Response: catalog.TableInfo{
CreatedAt: 1706294508998,
CatalogName: "a",
SchemaName: "b",
Name: "c",
},
},
},
Resource: DataSourceTable(),
HCL: `
full_name = "a.b.c"`,
Read: true,
NonWritable: true,
ID: "_",
}.Apply(t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could rewrite this as

ApplyAndExpectData(t, map[string]any{
"catalog_name": "a",
...
})

require.NoError(t, err)
assert := assert.New(t)
assert.Equal(d.Get("catalog_name"), "a")
assert.Equal(d.Get("schema_name"), "b")
assert.Equal(d.Get("name"), "c")
}

func TestTableData_Error(t *testing.T) {
qa.ResourceFixture{
Fixtures: qa.HTTPFailures,
Resource: DataSourceTable(),
Read: true,
NonWritable: true,
ID: "_",
}.ExpectError(t, "i'm a teapot")
}
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func DatabricksProvider() *schema.Provider {
"databricks_spark_version": clusters.DataSourceSparkVersion(),
"databricks_sql_warehouse": sql.DataSourceWarehouse(),
"databricks_sql_warehouses": sql.DataSourceWarehouses(),
"databricks_table": catalog.DataSourceTable(),
"databricks_tables": catalog.DataSourceTables(),
"databricks_views": catalog.DataSourceViews(),
"databricks_user": scim.DataSourceUser(),
Expand Down
Loading