Skip to content

Commit cf91f32

Browse files
authored
Fixed databricks_share update logic (#2022)
1 parent 4aa2406 commit cf91f32

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

catalog/resource_share.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package catalog
22

33
import (
44
"context"
5+
"reflect"
56
"sort"
67

78
"github.com/databricks/terraform-provider-databricks/common"
@@ -20,6 +21,7 @@ func NewSharesAPI(ctx context.Context, m any) SharesAPI {
2021
const (
2122
ShareAdd = "ADD"
2223
ShareRemove = "REMOVE"
24+
ShareUpdate = "UPDATE"
2325
)
2426

2527
type ShareInfo struct {
@@ -148,9 +150,16 @@ func (si ShareInfo) Diff(other ShareInfo) []ShareDataChange {
148150
}
149151

150152
// not in before so add
153+
// if in before but diff then update
151154
for _, afterSdo := range other.Objects {
152-
_, exists := beforeMap[afterSdo.Name]
155+
beforeSdo, exists := beforeMap[afterSdo.Name]
153156
if exists {
157+
if !reflect.DeepEqual(beforeSdo, afterSdo) {
158+
changes = append(changes, ShareDataChange{
159+
Action: ShareUpdate,
160+
DataObject: afterSdo,
161+
})
162+
}
154163
continue
155164
}
156165
changes = append(changes, ShareDataChange{

catalog/resource_share_test.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,36 @@ func TestDiffShareInfo(t *testing.T) {
4444
},
4545
},
4646
}
47+
thirdShare := ShareInfo{
48+
Name: "b",
49+
Objects: []SharedDataObject{
50+
{
51+
Name: "main.c",
52+
DataObjectType: "TABLE",
53+
Comment: "d",
54+
},
55+
{
56+
Name: "main.b",
57+
DataObjectType: "TABLE",
58+
Comment: "d",
59+
},
60+
},
61+
}
62+
fourthShare := ShareInfo{
63+
Name: "d",
64+
Objects: []SharedDataObject{
65+
{
66+
Name: "main.b",
67+
DataObjectType: "TABLE",
68+
Comment: "d",
69+
},
70+
{
71+
Name: "main.a",
72+
DataObjectType: "TABLE",
73+
Comment: "c",
74+
},
75+
},
76+
}
4777
diffAdd := []ShareDataChange{
4878
{
4979
Action: ShareAdd,
@@ -80,7 +110,7 @@ func TestDiffShareInfo(t *testing.T) {
80110
},
81111
},
82112
}
83-
diffMix := []ShareDataChange{
113+
diff12 := []ShareDataChange{
84114
{
85115
Action: ShareRemove,
86116
DataObject: SharedDataObject{
@@ -98,10 +128,48 @@ func TestDiffShareInfo(t *testing.T) {
98128
},
99129
},
100130
}
131+
diff13 := []ShareDataChange{
132+
{
133+
Action: ShareRemove,
134+
DataObject: SharedDataObject{
135+
Name: "main.a",
136+
DataObjectType: "TABLE",
137+
Comment: "c",
138+
},
139+
},
140+
{
141+
Action: ShareAdd,
142+
DataObject: SharedDataObject{
143+
Name: "main.c",
144+
DataObjectType: "TABLE",
145+
Comment: "d",
146+
},
147+
},
148+
{
149+
Action: ShareUpdate,
150+
DataObject: SharedDataObject{
151+
Name: "main.b",
152+
DataObjectType: "TABLE",
153+
Comment: "d",
154+
},
155+
},
156+
}
157+
diff14 := []ShareDataChange{
158+
{
159+
Action: ShareUpdate,
160+
DataObject: SharedDataObject{
161+
Name: "main.b",
162+
DataObjectType: "TABLE",
163+
Comment: "d",
164+
},
165+
},
166+
}
101167
assert.Equal(t, firstShare.Diff(firstShare), []ShareDataChange{}, "Should not have difference")
102168
assert.Equal(t, empty.Diff(firstShare), diffAdd, "Should have 2 ADDs")
103169
assert.Equal(t, firstShare.Diff(empty), diffRemove, "Should have 2 REMOVEs")
104-
assert.Equal(t, firstShare.Diff(secondShare), diffMix, "Should have 1 ADD and 1 REMOVE")
170+
assert.Equal(t, firstShare.Diff(secondShare), diff12, "Should have 1 ADD and 1 REMOVE")
171+
assert.Equal(t, firstShare.Diff(thirdShare), diff13, "Should have 1 ADD, 1 REMOVE and 1 UPDATE")
172+
assert.Equal(t, firstShare.Diff(fourthShare), diff14, "Should have 1 UPDATE")
105173
}
106174

107175
func TestShareCornerCases(t *testing.T) {

0 commit comments

Comments
 (0)