Skip to content

Commit ad25de3

Browse files
authored
Fixed updates for databricks_share resource (#2307)
1 parent 069fa96 commit ad25de3

File tree

3 files changed

+42
-66
lines changed

3 files changed

+42
-66
lines changed

catalog/resource_share.go

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"reflect"
66
"sort"
77

8+
"github.com/databricks/databricks-sdk-go/service/sharing"
89
"github.com/databricks/terraform-provider-databricks/common"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
)
@@ -84,11 +85,6 @@ func (si *ShareInfo) suppressCDFEnabledDiff() {
8485
}
8586
}
8687

87-
func (a SharesAPI) create(si *ShareInfo) error {
88-
si.sortSharesByName()
89-
return a.client.Post(a.context, "/unity-catalog/shares", si, si)
90-
}
91-
9288
func (a SharesAPI) get(name string) (si ShareInfo, err error) {
9389
err = a.client.Get(a.context, "/unity-catalog/shares/"+name+"?include_shared_data=true", nil, &si)
9490
si.sortSharesByName()
@@ -103,10 +99,6 @@ func (a SharesAPI) update(name string, su ShareUpdates) error {
10399
return a.client.Patch(a.context, "/unity-catalog/shares/"+name, su)
104100
}
105101

106-
func (a SharesAPI) delete(name string) error {
107-
return a.client.Delete(a.context, "/unity-catalog/shares/"+name, nil)
108-
}
109-
110102
func (si ShareInfo) shareChanges(action string) ShareUpdates {
111103
var changes []ShareDataChange
112104
for _, obj := range si.Objects {
@@ -128,19 +120,23 @@ func (si ShareInfo) resourceShareMap() map[string]SharedDataObject {
128120
return m
129121
}
130122

131-
func Equal(a, b SharedDataObject) bool {
132-
if b.SharedAs == "" {
133-
b.SharedAs = a.SharedAs
123+
func (sdo SharedDataObject) Equal(other SharedDataObject) bool {
124+
if other.SharedAs == "" {
125+
other.SharedAs = sdo.SharedAs
134126
}
135-
return reflect.DeepEqual(a, b)
127+
//don't compare computed fields
128+
other.AddedAt = sdo.AddedAt
129+
other.AddedBy = sdo.AddedBy
130+
other.Status = sdo.Status
131+
return reflect.DeepEqual(sdo, other)
136132
}
137133

138-
func (si ShareInfo) Diff(other ShareInfo) []ShareDataChange {
139-
beforeMap := si.resourceShareMap()
140-
afterMap := other.resourceShareMap()
134+
func (beforeSi ShareInfo) Diff(afterSi ShareInfo) []ShareDataChange {
135+
beforeMap := beforeSi.resourceShareMap()
136+
afterMap := afterSi.resourceShareMap()
141137
changes := []ShareDataChange{}
142138
// not in after so remove
143-
for _, beforeSdo := range si.Objects {
139+
for _, beforeSdo := range beforeSi.Objects {
144140
_, exists := afterMap[beforeSdo.Name]
145141
if exists {
146142
continue
@@ -153,10 +149,12 @@ func (si ShareInfo) Diff(other ShareInfo) []ShareDataChange {
153149

154150
// not in before so add
155151
// if in before but diff then update
156-
for _, afterSdo := range other.Objects {
152+
for _, afterSdo := range afterSi.Objects {
157153
beforeSdo, exists := beforeMap[afterSdo.Name]
158154
if exists {
159-
if !Equal(beforeSdo, afterSdo) {
155+
if !beforeSdo.Equal(afterSdo) {
156+
// do not send SharedAs
157+
afterSdo.SharedAs = ""
160158
changes = append(changes, ShareDataChange{
161159
Action: ShareUpdate,
162160
DataObject: afterSdo,
@@ -179,16 +177,24 @@ func ResourceShare() *schema.Resource {
179177
return common.Resource{
180178
Schema: shareSchema,
181179
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
182-
var si ShareInfo
183-
common.DataToStructPointer(d, shareSchema, &si)
184-
if err := NewSharesAPI(ctx, c).create(&si); err != nil {
180+
w, err := c.WorkspaceClient()
181+
if err != nil {
182+
return err
183+
}
184+
185+
var createRequest sharing.CreateShare
186+
common.DataToStructPointer(d, shareSchema, &createRequest)
187+
if _, err := w.Shares.Create(ctx, createRequest); err != nil {
185188
return err
186189
}
190+
187191
//can only create empty share, objects have to be added using update API
192+
var si ShareInfo
193+
common.DataToStructPointer(d, shareSchema, &si)
188194
shareChanges := si.shareChanges(ShareAdd)
189195
if err := NewSharesAPI(ctx, c).update(si.Name, shareChanges); err != nil {
190196
//delete orphaned share if update fails
191-
if d_err := NewSharesAPI(ctx, c).delete(si.Name); d_err != nil {
197+
if d_err := w.Shares.DeleteByName(ctx, si.Name); d_err != nil {
192198
return d_err
193199
}
194200
return err
@@ -204,19 +210,23 @@ func ResourceShare() *schema.Resource {
204210
return common.StructToData(si, shareSchema, d)
205211
},
206212
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
207-
si, err := NewSharesAPI(ctx, c).get(d.Id())
213+
beforeSi, err := NewSharesAPI(ctx, c).get(d.Id())
208214
if err != nil {
209215
return err
210216
}
211-
var shareInfo ShareInfo
212-
common.DataToStructPointer(d, shareSchema, &shareInfo)
213-
changes := si.Diff(shareInfo)
217+
var afterSi ShareInfo
218+
common.DataToStructPointer(d, shareSchema, &afterSi)
219+
changes := beforeSi.Diff(afterSi)
214220
return NewSharesAPI(ctx, c).update(d.Id(), ShareUpdates{
215221
Updates: changes,
216222
})
217223
},
218224
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
219-
return NewSharesAPI(ctx, c).delete(d.Id())
225+
w, err := c.WorkspaceClient()
226+
if err != nil {
227+
return err
228+
}
229+
return w.Shares.DeleteByName(ctx, d.Id())
220230
},
221231
}.ToResource()
222232
}

catalog/resource_share_test.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,6 @@ func TestCreateShare(t *testing.T) {
184184
Resource: "/api/2.1/unity-catalog/shares",
185185
ExpectedRequest: ShareInfo{
186186
Name: "a",
187-
Objects: []SharedDataObject{
188-
{
189-
Name: "main.a",
190-
DataObjectType: "TABLE",
191-
Comment: "c",
192-
},
193-
{
194-
Name: "main.b",
195-
DataObjectType: "TABLE",
196-
Comment: "c",
197-
},
198-
},
199187
},
200188
Response: RecipientInfo{
201189
Name: "a",
@@ -431,18 +419,6 @@ func TestCreateShare_ThrowError(t *testing.T) {
431419
Resource: "/api/2.1/unity-catalog/shares",
432420
ExpectedRequest: ShareInfo{
433421
Name: "a",
434-
Objects: []SharedDataObject{
435-
{
436-
Name: "main.a",
437-
DataObjectType: "TABLE",
438-
Comment: "c",
439-
},
440-
{
441-
Name: "main.b",
442-
DataObjectType: "TABLE",
443-
Comment: "c",
444-
},
445-
},
446422
},
447423
Response: apierr.APIErrorBody{
448424
ErrorCode: "INVALID_REQUEST",
@@ -479,18 +455,6 @@ func TestCreateShareButPatchFails(t *testing.T) {
479455
Resource: "/api/2.1/unity-catalog/shares",
480456
ExpectedRequest: ShareInfo{
481457
Name: "a",
482-
Objects: []SharedDataObject{
483-
{
484-
Name: "main.a",
485-
DataObjectType: "TABLE",
486-
Comment: "c",
487-
},
488-
{
489-
Name: "main.b",
490-
DataObjectType: "TABLE",
491-
Comment: "c",
492-
},
493-
},
494458
},
495459
Response: RecipientInfo{
496460
Name: "a",
@@ -527,7 +491,7 @@ func TestCreateShareButPatchFails(t *testing.T) {
527491
},
528492
{
529493
Method: "DELETE",
530-
Resource: "/api/2.1/unity-catalog/shares/a",
494+
Resource: "/api/2.1/unity-catalog/shares/a?",
531495
},
532496
},
533497
Resource: ResourceShare(),

docs/resources/share.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ A `databricks_share` is contained within [databricks_metastore](metastore.md) an
99

1010
## Example Usage
1111

12+
-> **Note** In Terraform configuration, it is recommended to define objects in alphabetical order of their `name` arguments, so that you get consistent and readable diff. Whenever objects are added or removed, or `name` is renamed, you'll observe a change in the majority of tasks. It's related to the fact that the current version of the provider treats `object` blocks as an ordered list. Alternatively, `object` block could have been an unordered set, though end-users would see the entire block replaced upon a change in single property of the task.
13+
1214
Creating a Delta Sharing share and add some existing tables to it
1315

1416
```hcl
@@ -72,7 +74,7 @@ The following arguments are required:
7274
* `name` (Required) - Full name of the object, e.g. `catalog.schema.name` for a table.
7375
* `data_object_type` (Required) - Type of the object, currently only `TABLE` is allowed.
7476
* `comment` (Optional) - Description about the object.
75-
* `shared_as` (Optional) - A user-provided new name for the data object within the share. If this new name is not provided, the object's original name will be used as the `shared_as` name. The `shared_as` name must be unique within a Share.
77+
* `shared_as` (Optional) - A user-provided new name for the data object within the share. If this new name is not provided, the object's original name will be used as the `shared_as` name. The `shared_as` name must be unique within a Share. Change forces creation of a new resource.
7678
* `cdf_enabled` (Optional) - Whether to enable Change Data Feed (cdf) on the shared object. When this field is set, field `history_data_sharing_status` can not be set.
7779
* `start_version` (Optional) - The start version associated with the object for cdf. This allows data providers to control the lowest object version that is accessible by clients.
7880
* `history_data_sharing_status` (Optional) - Whether to enable history sharing, one of: `ENABLED`, `DISABLED`. When a table has history sharing enabled, recipients can query table data by version, starting from the current table version. If not specified, clients can only query starting from the version of the object at the time it was added to the share. *NOTE*: The start_version should be less than or equal the current version of the object. When this field is set, field `cdf_enabled` can not be set.

0 commit comments

Comments
 (0)