Skip to content

Commit

Permalink
[Internal] migrate Share data source to Go SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchy committed Aug 20, 2024
1 parent e7967c4 commit 65197d9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
20 changes: 14 additions & 6 deletions sharing/data_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@ package sharing
import (
"context"

"github.com/databricks/databricks-sdk-go/service/sharing"
"github.com/databricks/terraform-provider-databricks/common"
)

func DataSourceShare() common.Resource {
type ShareDetail struct {
Name string `json:"name,omitempty" tf:"computed"`
Objects []SharedDataObject `json:"objects,omitempty" tf:"computed,slice_set,alias:object"`
CreatedAt int64 `json:"created_at,omitempty" tf:"computed"`
CreatedBy string `json:"created_by,omitempty" tf:"computed"`
Name string `json:"name,omitempty" tf:"computed"`
Objects []sharing.SharedDataObject `json:"objects,omitempty" tf:"computed,slice_set,alias:object"`
CreatedAt int64 `json:"created_at,omitempty" tf:"computed"`
CreatedBy string `json:"created_by,omitempty" tf:"computed"`
}
return common.DataResource(ShareDetail{}, func(ctx context.Context, e any, c *common.DatabricksClient) error {
data := e.(*ShareDetail)
sharesAPI := NewSharesAPI(ctx, c)
share, err := sharesAPI.get(data.Name)
client, err := c.WorkspaceClient()
if err != nil {
return err
}

share, err := client.Shares.Get(ctx, sharing.GetShareRequest{
Name: data.Name,
IncludeSharedData: true,
})
if err != nil {
return err
}
Expand Down
13 changes: 8 additions & 5 deletions sharing/data_share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sharing
import (
"testing"

"github.com/databricks/databricks-sdk-go/service/sharing"
"github.com/databricks/terraform-provider-databricks/qa"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/assert"
Expand All @@ -14,21 +15,21 @@ func TestShareData(t *testing.T) {
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/shares/a?include_shared_data=true",
Response: ShareInfo{
Response: sharing.ShareInfo{
Name: "a",
Objects: []SharedDataObject{
Objects: []sharing.SharedDataObject{
{
Name: "a",
DataObjectType: "TABLE",
Comment: "c",
CDFEnabled: false,
CdfEnabled: false,
StartVersion: 0,
SharedAs: "",
AddedAt: 0,
AddedBy: "",
HistoryDataSharingStatus: "DISABLED",
Status: "ACTIVE",
Partitions: []Partition{},
Partitions: []sharing.Partition{},
},
},
CreatedBy: "bob",
Expand All @@ -52,14 +53,16 @@ func TestShareData(t *testing.T) {
"added_at": 0,
"added_by": "",
"comment": "c",
"content": "",
"data_object_type": "TABLE",
"name": "a",
"shared_as": "",
"start_version": 0,
"string_shared_as": "",
"cdf_enabled": false,
"status": "ACTIVE",
"history_data_sharing_status": "DISABLED",
"partition": []interface{}{},
"partitions": []interface{}{},
},
d.Get("object").(*schema.Set).List()[0])
}
Expand Down
4 changes: 2 additions & 2 deletions sharing/resource_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ func (si *ShareInfo) suppressCDFEnabledDiff() {

func (a SharesAPI) get(name string) (si ShareInfo, err error) {
err = a.client.Get(a.context, "/unity-catalog/shares/"+name+"?include_shared_data=true", nil, &si)
si.sortSharesByName()
si.suppressCDFEnabledDiff()
return
}

Expand Down Expand Up @@ -228,6 +226,8 @@ func ResourceShare() common.Resource {
if err != nil {
return err
}
beforeSi.sortSharesByName()
beforeSi.suppressCDFEnabledDiff()
var afterSi ShareInfo
common.DataToStructPointer(d, shareSchema, &afterSi)
changes := beforeSi.Diff(afterSi)
Expand Down

0 comments on commit 65197d9

Please sign in to comment.