diff --git a/common/resource.go b/common/resource.go index 79a1264086..77ba894853 100644 --- a/common/resource.go +++ b/common/resource.go @@ -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) { diff --git a/sharing/data_share.go b/sharing/data_share.go index 45ed30be76..174d1e425c 100644 --- a/sharing/data_share.go +++ b/sharing/data_share.go @@ -3,24 +3,44 @@ 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 []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"` +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").SetMinItems(1) + s.SchemaPath("object", "partition", "value", "op").SetRequired() + s.SchemaPath("object", "partition", "value", "name").SetRequired() + + 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) - client, err := c.WorkspaceClient() - if err != nil { - return err - } +} +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, diff --git a/sharing/data_share_test.go b/sharing/data_share_test.go index d223f2971b..5cda61ea7a 100644 --- a/sharing/data_share_test.go +++ b/sharing/data_share_test.go @@ -62,7 +62,7 @@ func TestShareData(t *testing.T) { "cdf_enabled": false, "status": "ACTIVE", "history_data_sharing_status": "DISABLED", - "partitions": []interface{}{}, + "partition": []interface{}{}, }, d.Get("object").(*schema.Set).List()[0]) } diff --git a/sharing/resource_share.go b/sharing/resource_share.go index d4c2049bef..f8c10fd6ef 100644 --- a/sharing/resource_share.go +++ b/sharing/resource_share.go @@ -16,6 +16,7 @@ type ShareInfo struct { } func (ShareInfo) CustomizeSchema(s *common.CustomizableSchema) *common.CustomizableSchema { + s.SchemaPath("name").SetRequired() s.SchemaPath("name").SetForceNew() s.SchemaPath("name").SetCustomSuppressDiff(common.EqualFoldDiffSuppress) s.SchemaPath("owner").SetSuppressDiff() @@ -24,6 +25,8 @@ func (ShareInfo) CustomizeSchema(s *common.CustomizableSchema) *common.Customiza s.SchemaPath("updated_at").SetComputed() s.SchemaPath("updated_by").SetComputed() + s.SchemaPath("object").SetMinItems(1) + s.SchemaPath("object", "data_object_type").SetRequired() s.SchemaPath("object", "shared_as").SetSuppressDiff() s.SchemaPath("object", "cdf_enabled").SetSuppressDiff() s.SchemaPath("object", "start_version").SetSuppressDiff() @@ -31,6 +34,8 @@ func (ShareInfo) CustomizeSchema(s *common.CustomizableSchema) *common.Customiza s.SchemaPath("object", "status").SetComputed() s.SchemaPath("object", "added_at").SetComputed() s.SchemaPath("object", "added_by").SetComputed() + s.SchemaPath("object", "partition", "value", "op").SetRequired() + s.SchemaPath("object", "partition", "value", "name").SetRequired() return s } @@ -43,6 +48,9 @@ func (ShareInfo) Aliases() map[string]map[string]string { "sharing.SharedDataObject": { "partitions": "partition", }, + "sharing.Partition": { + "values": "value", + }, } }