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

[Internal] Migrate Share resource to Go SDK #3916

Merged
merged 14 commits into from
Sep 3, 2024
45 changes: 23 additions & 22 deletions common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,30 +381,31 @@ func genericDatabricksData[T, P, C any](
var dummy T
var other P
otherFields := StructToSchema(other, nil)
s := StructToSchema(dummy, func(m map[string]*schema.Schema) map[string]*schema.Schema {
// For WorkspaceData and AccountData, a single data type is used to represent all of the fields of
// the resource, so its configuration is correct. For the *WithParams methods, the SdkType parameter
// is copied directly from the resource definition, which means that all fields from that type are
// computed and optional, and the fields from OtherFields are overlaid on top of the schema generated
// by SdkType.
if hasOther {
for k := range m {
m[k].Computed = true
m[k].Required = false
m[k].Optional = true
}
for k, v := range otherFields {
m[k] = v
}

s := StructToSchema(dummy, nil)
// For WorkspaceData and AccountData, a single data type is used to represent all of the fields of
// the resource, so its configuration is correct. For the *WithParams methods, the SdkType parameter
// is copied directly from the resource definition, which means that all fields from that type are
// computed and optional, and the fields from OtherFields are overlaid on top of the schema generated
// by SdkType.
if hasOther {
for k := range s {
s[k].Computed = true
s[k].Required = false
s[k].Optional = true
}
// `id` attribute must be marked as computed, otherwise it's not set!
if v, ok := m["id"]; ok {
v.Computed = true
v.Required = false
for k, v := range otherFields {
s[k] = v
}
// allow c
return customizeSchemaFunc(m)
})
}
// `id` attribute must be marked as computed, otherwise it's not set!
if v, ok := s["id"]; ok {
v.Computed = true
v.Required = false
}
// allow c
s = customizeSchemaFunc(s)

return Resource{
Schema: s,
Read: func(ctx context.Context, d *schema.ResourceData, client *DatabricksClient) (err error) {
Expand Down
36 changes: 19 additions & 17 deletions exporter/importables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1828,23 +1828,25 @@ func TestImportShare(t *testing.T) {
d := tfsharing.ResourceShare().ToResource().TestResourceData()
scm := tfsharing.ResourceShare().Schema
share := tfsharing.ShareInfo{
Name: "stest",
Objects: []tfsharing.SharedDataObject{
{
DataObjectType: "TABLE",
Name: "ctest.stest.table1",
},
{
DataObjectType: "MODEL",
Name: "ctest.stest.model1",
},
{
DataObjectType: "VOLUME",
Name: "ctest.stest.vol1",
},
{
DataObjectType: "NOTEBOOK",
Name: "Test",
ShareInfo: sharing.ShareInfo{
Name: "stest",
Objects: []sharing.SharedDataObject{
{
DataObjectType: "TABLE",
Name: "ctest.stest.table1",
},
{
DataObjectType: "MODEL",
Name: "ctest.stest.model1",
},
{
DataObjectType: "VOLUME",
Name: "ctest.stest.vol1",
},
{
DataObjectType: "NOTEBOOK",
Name: "Test",
},
},
},
}
Expand Down
48 changes: 38 additions & 10 deletions sharing/data_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,48 @@ package sharing
import (
"context"

"github.com/databricks/databricks-sdk-go"
"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"`
type ShareDetail struct {
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"`
}

func (ShareDetail) CustomizeSchema(s *common.CustomizableSchema) *common.CustomizableSchema {
s.SchemaPath("name").SetComputed()
s.SchemaPath("object", "added_at").SetComputed()
s.SchemaPath("object", "added_by").SetComputed()
s.SchemaPath("object", "data_object_type").SetRequired()
s.SchemaPath("object", "status").SetComputed()
s.SchemaPath("object", "partition", "value", "op").SetRequired()
s.SchemaPath("object", "partition", "value", "name").SetRequired()
s.SchemaPath("object", "partition", "value").SetMinItems(1)

return s
}

func (ShareDetail) Aliases() map[string]map[string]string {
return map[string]map[string]string{
"sharing.SharedDataObject": {
"partitions": "partition",
},
"sharing.Partition": {
"values": "value",
},
}
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)
}

func DataSourceShare() common.Resource {
return common.WorkspaceData(func(ctx context.Context, data *ShareDetail, client *databricks.WorkspaceClient) error {
share, err := client.Shares.Get(ctx, sharing.GetShareRequest{
Name: data.Name,
IncludeSharedData: true,
})
if err != nil {
return err
}
Expand Down
11 changes: 7 additions & 4 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,10 +53,12 @@ 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",
Expand Down
22 changes: 12 additions & 10 deletions sharing/data_shares_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"
)

Expand All @@ -15,17 +16,18 @@ func TestSharesData(t *testing.T) {
Response: Shares{
Shares: []ShareInfo{
{
Name: "a",
Objects: []SharedDataObject{
{
Name: "a",
DataObjectType: "TABLE",
Comment: "c",
sharing.ShareInfo{
Name: "a",
Objects: []sharing.SharedDataObject{
{
Name: "a",
DataObjectType: "TABLE",
Comment: "c",
},
},
},
CreatedAt: 0,
CreatedBy: "",
},
CreatedAt: 0,
CreatedBy: "",
}},
},
},
},
Expand Down
Loading
Loading