You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would really appreciate some Golang snippets demonstrating manage-group-members operations, especially checking group membership, adding members to a group.
package google_groups_sync
import (
"context"
"io/ioutil"
"log"
"strings"
"time"
"github.com/dxe/adb/config"
"github.com/dxe/adb/model"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"golang.org/x/oauth2/google"
admin "google.golang.org/api/admin/directory/v1"
)
func getAdminService() (*admin.Service, error) {
key, err := ioutil.ReadFile(config.SyncMailingListsConfigFile)
if err != nil {
return nil, errors.Wrap(err, "Could not read google auth key")
}
oauthConfig, err := google.JWTConfigFromJSON(key, "https://www.googleapis.com/auth/admin.directory.group")
if err != nil {
return nil, errors.Wrap(err, "Could not read JWT config from google auth key")
}
oauthConfig.Subject = config.SyncMailingListsOauthSubject
client := oauthConfig.Client(context.Background())
adminService, err := admin.New(client)
if err != nil {
return nil, errors.Wrap(err, "Could not construct admin service")
}
return adminService, nil
}
func listMembers(adminService *admin.Service, groupEmail string) ([]string, error) {
var memberEmails []string
call := adminService.Members.List(groupEmail)
err := call.Pages(context.Background(), func(members *admin.Members) error {
for _, m := range members.Members {
memberEmails = append(memberEmails, m.Email)
}
return nil
})
if err != nil {
return nil, errors.Wrapf(err, "Could not page members for group %s", groupEmail)
}
return memberEmails, nil
}
func insertMember(adminService *admin.Service, groupEmail, memberEmail string) error {
_, err := adminService.Members.Insert(groupEmail, &admin.Member{Email: memberEmail}).Do()
return errors.Wrapf(err, "Could not insert member %s into group %s ", memberEmail, groupEmail)
}
func removeMember(adminService *admin.Service, groupEmail, memberEmail string) error {
err := adminService.Members.Delete(groupEmail, memberEmail).Do()
return errors.Wrapf(err, "Could not delete member %s from group %s", memberEmail, groupEmail)
}
// something something main mumble unit tests
The text was updated successfully, but these errors were encountered:
I would really appreciate some Golang snippets demonstrating manage-group-members operations, especially checking group membership, adding members to a group.
The text was updated successfully, but these errors were encountered: