Skip to content

Commit

Permalink
fix updating group members
Browse files Browse the repository at this point in the history
  • Loading branch information
xrstf committed Aug 2, 2021
1 parent 6189e40 commit d16d185
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
11 changes: 9 additions & 2 deletions pkg/config/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,18 @@ func ToConfigGroup(gsuiteGroup *directoryv1.Group, settings *groupssettingsv1.Gr
return group, nil
}

func ToGSuiteGroupMember(member *Member) *directoryv1.Member {
return &directoryv1.Member{
func ToGSuiteGroupMember(member *Member, gsuiteMember *directoryv1.Member) *directoryv1.Member {
result := &directoryv1.Member{
Email: member.Email,
Role: member.Role,
}

if gsuiteMember != nil {
result.Id = gsuiteMember.Id
result.Etag = gsuiteMember.Etag
}

return result
}

func ToConfigGroupMember(gsuiteMember *directoryv1.Member) Member {
Expand Down
2 changes: 1 addition & 1 deletion pkg/glib/directory_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (ds *DirectoryService) RemoveMember(ctx context.Context, group *directoryv1
// UpdateMembership changes the role of the member
func (ds *DirectoryService) UpdateMembership(ctx context.Context, group *directoryv1.Group, member *directoryv1.Member) error {
// do NOT use the member email here, as it will lead to "Error 404: Resource Not Found: email, notFound." errors
if _, err := ds.Members.Update(group.Email, member.Id, member).Context(ctx).Do(); err != nil {
if _, err := ds.Members.Update(group.Id, member.Id, member).Context(ctx).Do(); err != nil {
return err
}

Expand Down
9 changes: 7 additions & 2 deletions pkg/sync/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"log"
"sort"

directoryv1 "google.golang.org/api/admin/directory/v1"
"k8s.io/apimachinery/pkg/util/sets"
Expand All @@ -46,6 +47,10 @@ func SyncGroups(

liveGroupEmails := sets.NewString()

sort.Slice(liveGroups, func(i, j int) bool {
return liveGroups[i].Email < liveGroups[j].Email
})

for _, liveGroup := range liveGroups {
liveGroupEmails.Insert(liveGroup.Email)

Expand Down Expand Up @@ -171,7 +176,7 @@ func syncGroupMembers(
log.Printf(" ✎ %s", liveMember.Email)

if confirm {
member := config.ToGSuiteGroupMember(expectedMember)
member := config.ToGSuiteGroupMember(expectedMember, liveMember)
if err := directorySrv.UpdateMembership(ctx, liveGroup, member); err != nil {
return fmt.Errorf("unable to update membership: %v", err)
}
Expand All @@ -184,7 +189,7 @@ func syncGroupMembers(
log.Printf(" + %s", expectedMember.Email)

if confirm {
member := config.ToGSuiteGroupMember(&expectedMember)
member := config.ToGSuiteGroupMember(&expectedMember, nil)
if err := directorySrv.AddNewMember(ctx, liveGroup, member); err != nil {
return fmt.Errorf("unable to add member: %v", err)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/sync/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"log"
"sort"

directoryv1 "google.golang.org/api/admin/directory/v1"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -48,6 +49,10 @@ func SyncUsers(

liveEmails := sets.NewString()

sort.Slice(liveUsers, func(i, j int) bool {
return liveUsers[i].PrimaryEmail < liveUsers[j].PrimaryEmail
})

for _, liveUser := range liveUsers {
liveEmails.Insert(liveUser.PrimaryEmail)

Expand Down

0 comments on commit d16d185

Please sign in to comment.