Skip to content

do not update group list if the member group list is not changed #59

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 25 additions & 6 deletions pkg/looker/resource_group_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ func resourceGroupMembershipRead(ctx context.Context, d *schema.ResourceData, m
return nil
}

func getGroupIDs(m interface{}, groupId string) ([]string, error) {
client := m.(*apiclient.LookerSDK)
groups, err := client.AllGroupGroups(groupId, "", nil) // todo: imeplement paging
if err != nil {
return nil, err
}

return flattenGroupIDs(groups), nil
}

func resourceGroupMembershipUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
targetGroupID := d.Id()

Expand All @@ -122,21 +132,30 @@ func resourceGroupMembershipUpdate(ctx context.Context, d *schema.ResourceData,
if err != nil {
return diag.FromErr(err)
}

err = removeAllGroupsFromGroup(m, targetGroupID)
groupIDs := expandStringListFromSet(d.Get("group_ids"))
actualGroupIDs, err := getGroupIDs(m, targetGroupID)
if err != nil {
return diag.FromErr(err)
}
updateGroupsFromGroupRequired := differentStringSlices(actualGroupIDs, groupIDs)

if updateGroupsFromGroupRequired {
err = removeAllGroupsFromGroup(m, targetGroupID)
if err != nil {
return diag.FromErr(err)
}
}

err = addGroupUsers(m, targetGroupID, userIDs)
if err != nil {
return diag.FromErr(err)
}

groupIDs := expandStringListFromSet(d.Get("group_ids"))
err = addGroupGroups(m, targetGroupID, groupIDs)
if err != nil {
return diag.FromErr(err)
if updateGroupsFromGroupRequired {
err = addGroupGroups(m, targetGroupID, groupIDs)
if err != nil {
return diag.FromErr(err)
}
}

return resourceGroupMembershipRead(ctx, d, m)
Expand Down
17 changes: 17 additions & 0 deletions pkg/looker/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,20 @@ func hash(val interface{}) string {
sha := sha256.Sum256([]byte(val.(string)))
return hex.EncodeToString(sha[:])
}

func differentStringSlices(a, b []string) bool {
if len(a) != len(b) {
return true
}
counts := make(map[string]int)
for _, item := range a {
counts[item]++
}
for _, item := range b {
counts[item]--
if counts[item] < 0 {
return true
}
}
return false
}
31 changes: 31 additions & 0 deletions pkg/looker/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@ import (
"github.com/stretchr/testify/assert"
)

func TestDifferentStringSlices(t *testing.T) {
tests := map[string]struct {
a []string
b []string
wantRes bool
}{
"same slice": {
a: []string{"a", "b", "c"},
b: []string{"a", "b", "c"},
wantRes: false,
},
"different slice": {
a: []string{"a", "b", "c"},
b: []string{"a", "b", "d"},
wantRes: true,
},
"one slice is empty": {
a: []string{},
b: []string{"a", "b", "c"},
wantRes: true,
},
}

for key, tt := range tests {
t.Run(key, func(t *testing.T) {
actual := differentStringSlices(tt.a, tt.b)
assert.Equal(t, tt.wantRes, actual)
})
}
}

func TestBuildTwoPartID(t *testing.T) {
tests := map[string]struct {
a string
Expand Down
Loading