Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix databricks_share update logic #2022

Merged
merged 3 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion catalog/resource_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package catalog

import (
"context"
"reflect"
"sort"

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

type ShareInfo struct {
Expand Down Expand Up @@ -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{
Expand Down
72 changes: 70 additions & 2 deletions catalog/resource_share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -80,7 +110,7 @@ func TestDiffShareInfo(t *testing.T) {
},
},
}
diffMix := []ShareDataChange{
diff12 := []ShareDataChange{
{
Action: ShareRemove,
DataObject: SharedDataObject{
Expand All @@ -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) {
Expand Down