diff --git a/catalog/resource_share.go b/catalog/resource_share.go index dc572ac0f7..d59dad2466 100644 --- a/catalog/resource_share.go +++ b/catalog/resource_share.go @@ -2,6 +2,7 @@ package catalog import ( "context" + "reflect" "sort" "github.com/databricks/terraform-provider-databricks/common" @@ -20,6 +21,7 @@ func NewSharesAPI(ctx context.Context, m any) SharesAPI { const ( ShareAdd = "ADD" ShareRemove = "REMOVE" + ShareUpdate = "UPDATE" ) type ShareInfo struct { @@ -148,9 +150,16 @@ func (si ShareInfo) Diff(other ShareInfo) []ShareDataChange { } // not in before so add + // if in before but diff then update for _, afterSdo := range other.Objects { - _, exists := beforeMap[afterSdo.Name] + beforeSdo, exists := beforeMap[afterSdo.Name] if exists { + if !reflect.DeepEqual(beforeSdo, afterSdo) { + changes = append(changes, ShareDataChange{ + Action: ShareUpdate, + DataObject: afterSdo, + }) + } continue } changes = append(changes, ShareDataChange{ diff --git a/catalog/resource_share_test.go b/catalog/resource_share_test.go index e778ac8f32..261de77ef2 100644 --- a/catalog/resource_share_test.go +++ b/catalog/resource_share_test.go @@ -44,6 +44,36 @@ func TestDiffShareInfo(t *testing.T) { }, }, } + thirdShare := ShareInfo{ + Name: "b", + Objects: []SharedDataObject{ + { + Name: "main.c", + DataObjectType: "TABLE", + Comment: "d", + }, + { + Name: "main.b", + DataObjectType: "TABLE", + Comment: "d", + }, + }, + } + fourthShare := ShareInfo{ + Name: "d", + Objects: []SharedDataObject{ + { + Name: "main.b", + DataObjectType: "TABLE", + Comment: "d", + }, + { + Name: "main.a", + DataObjectType: "TABLE", + Comment: "c", + }, + }, + } diffAdd := []ShareDataChange{ { Action: ShareAdd, @@ -80,7 +110,7 @@ func TestDiffShareInfo(t *testing.T) { }, }, } - diffMix := []ShareDataChange{ + diff12 := []ShareDataChange{ { Action: ShareRemove, DataObject: SharedDataObject{ @@ -98,10 +128,48 @@ func TestDiffShareInfo(t *testing.T) { }, }, } + diff13 := []ShareDataChange{ + { + Action: ShareRemove, + DataObject: SharedDataObject{ + Name: "main.a", + DataObjectType: "TABLE", + Comment: "c", + }, + }, + { + Action: ShareAdd, + DataObject: SharedDataObject{ + Name: "main.c", + DataObjectType: "TABLE", + Comment: "d", + }, + }, + { + Action: ShareUpdate, + DataObject: SharedDataObject{ + Name: "main.b", + DataObjectType: "TABLE", + Comment: "d", + }, + }, + } + diff14 := []ShareDataChange{ + { + Action: ShareUpdate, + DataObject: SharedDataObject{ + Name: "main.b", + DataObjectType: "TABLE", + Comment: "d", + }, + }, + } assert.Equal(t, firstShare.Diff(firstShare), []ShareDataChange{}, "Should not have difference") assert.Equal(t, empty.Diff(firstShare), diffAdd, "Should have 2 ADDs") assert.Equal(t, firstShare.Diff(empty), diffRemove, "Should have 2 REMOVEs") - assert.Equal(t, firstShare.Diff(secondShare), diffMix, "Should have 1 ADD and 1 REMOVE") + assert.Equal(t, firstShare.Diff(secondShare), diff12, "Should have 1 ADD and 1 REMOVE") + assert.Equal(t, firstShare.Diff(thirdShare), diff13, "Should have 1 ADD, 1 REMOVE and 1 UPDATE") + assert.Equal(t, firstShare.Diff(fourthShare), diff14, "Should have 1 UPDATE") } func TestShareCornerCases(t *testing.T) {