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

Feature Request: Manage Group members snippets #53

Open
StevenACoffman opened this issue May 21, 2021 · 1 comment
Open

Feature Request: Manage Group members snippets #53

StevenACoffman opened this issue May 21, 2021 · 1 comment

Comments

@StevenACoffman
Copy link

StevenACoffman commented May 21, 2021

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 
@StevenACoffman
Copy link
Author

Oh, here's a good example: admin sdk client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant