Skip to content

Commit cdb83b0

Browse files
authored
Fix diff detection for databricks_share resource (#2197)
1 parent 1647d2f commit cdb83b0

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

catalog/resource_share.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ func (si ShareInfo) resourceShareMap() map[string]SharedDataObject {
128128
return m
129129
}
130130

131+
func Equal(a, b SharedDataObject) bool {
132+
if b.SharedAs == "" {
133+
b.SharedAs = a.SharedAs
134+
}
135+
return reflect.DeepEqual(a, b)
136+
}
137+
131138
func (si ShareInfo) Diff(other ShareInfo) []ShareDataChange {
132139
beforeMap := si.resourceShareMap()
133140
afterMap := other.resourceShareMap()
@@ -149,7 +156,7 @@ func (si ShareInfo) Diff(other ShareInfo) []ShareDataChange {
149156
for _, afterSdo := range other.Objects {
150157
beforeSdo, exists := beforeMap[afterSdo.Name]
151158
if exists {
152-
if !reflect.DeepEqual(beforeSdo, afterSdo) {
159+
if !Equal(beforeSdo, afterSdo) {
153160
changes = append(changes, ShareDataChange{
154161
Action: ShareUpdate,
155162
DataObject: afterSdo,

catalog/resource_share_test.go

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func TestUpdateShare(t *testing.T) {
289289
{
290290
Method: "PATCH",
291291
Resource: "/api/2.1/unity-catalog/shares/abc",
292-
Response: ShareUpdates{
292+
ExpectedRequest: ShareUpdates{
293293
Updates: []ShareDataChange{
294294
{
295295
Action: "REMOVE",
@@ -310,7 +310,7 @@ func TestUpdateShare(t *testing.T) {
310310
{
311311
Action: "ADD",
312312
DataObject: SharedDataObject{
313-
Comment: "d",
313+
Comment: "c",
314314
DataObjectType: "TABLE",
315315
Name: "b",
316316
},
@@ -550,3 +550,88 @@ func TestCreateShareButPatchFails(t *testing.T) {
550550
qa.AssertErrorStartsWith(t, err, "Internal error happened")
551551
assert.Equal(t, "", d.Id(), "Id should be empty for error creates")
552552
}
553+
554+
func TestUpdateShareComplexDiff(t *testing.T) {
555+
qa.ResourceFixture{
556+
Fixtures: []qa.HTTPFixture{
557+
{
558+
Method: "GET",
559+
Resource: "/api/2.1/unity-catalog/shares/abc?include_shared_data=true",
560+
Response: ShareInfo{
561+
Name: "abc",
562+
Objects: []SharedDataObject{
563+
{
564+
Name: "a",
565+
DataObjectType: "TABLE",
566+
Comment: "c",
567+
SharedAs: "b",
568+
AddedAt: 0,
569+
AddedBy: "",
570+
},
571+
},
572+
},
573+
},
574+
{
575+
Method: "PATCH",
576+
Resource: "/api/2.1/unity-catalog/shares/abc",
577+
ExpectedRequest: ShareUpdates{
578+
Updates: []ShareDataChange{
579+
{
580+
Action: "ADD",
581+
DataObject: SharedDataObject{
582+
Comment: "c",
583+
DataObjectType: "TABLE",
584+
Name: "b",
585+
},
586+
},
587+
},
588+
},
589+
},
590+
{
591+
Method: "GET",
592+
Resource: "/api/2.1/unity-catalog/shares/abc?include_shared_data=true",
593+
Response: ShareInfo{
594+
Name: "abc",
595+
Objects: []SharedDataObject{
596+
{
597+
Name: "a",
598+
DataObjectType: "TABLE",
599+
Comment: "c",
600+
SharedAs: "",
601+
AddedAt: 0,
602+
AddedBy: "",
603+
},
604+
{
605+
Name: "b",
606+
DataObjectType: "TABLE",
607+
Comment: "c",
608+
SharedAs: "",
609+
AddedAt: 0,
610+
AddedBy: "",
611+
},
612+
},
613+
},
614+
},
615+
},
616+
ID: "abc",
617+
Update: true,
618+
RequiresNew: true,
619+
InstanceState: map[string]string{
620+
"name": "abc",
621+
},
622+
HCL: `
623+
name = "abc"
624+
object {
625+
name = "a"
626+
comment = "c"
627+
data_object_type = "TABLE"
628+
}
629+
object {
630+
name = "b"
631+
comment = "c"
632+
data_object_type = "TABLE"
633+
}
634+
`,
635+
Resource: ResourceShare(),
636+
}.ApplyNoError(t)
637+
}

0 commit comments

Comments
 (0)