Skip to content

Commit

Permalink
diff changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchy committed Aug 28, 2024
1 parent ffb2b0c commit d647e61
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 35 deletions.
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
44 changes: 32 additions & 12 deletions sharing/data_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion sharing/data_share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
8 changes: 8 additions & 0 deletions sharing/resource_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -24,13 +25,17 @@ 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()
s.SchemaPath("object", "history_data_sharing_status").SetSuppressDiff()
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
}
Expand All @@ -43,6 +48,9 @@ func (ShareInfo) Aliases() map[string]map[string]string {
"sharing.SharedDataObject": {
"partitions": "partition",
},
"sharing.Partition": {
"values": "value",
},
}
}

Expand Down

0 comments on commit d647e61

Please sign in to comment.