-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
56 lines (47 loc) · 1.31 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package groups
import (
"context"
"fmt"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
)
type Client struct {
adminSvc *admin.Service
Group string
}
func NewClient(group string) (Client, error) {
adminSvc, err := admin.NewService(context.TODO(), option.WithScopes("https://www.googleapis.com/auth/admin.directory.group.member"))
if err != nil {
return Client{}, fmt.Errorf("failed to create admin service: %w", err)
}
return Client{adminSvc: adminSvc, Group: group}, nil
}
func (c *Client) LookupMember(email string) (*admin.Member, error) {
return c.adminSvc.Members.Get(c.Group, email).Do()
}
func (c *Client) ListMembers() ([]*admin.Member, error) {
var memberList []*admin.Member
pageToken := ""
for {
listResp, err := c.adminSvc.Members.List(c.Group).MaxResults(200).PageToken(pageToken).Do()
if err != nil {
return nil, err
}
memberList = append(memberList, listResp.Members...)
pageToken = listResp.NextPageToken
if len(pageToken) == 0 {
break
}
}
return memberList, nil
}
func (c *Client) AddMember(email string) error {
member := admin.Member{
Email: email,
}
_, err := c.adminSvc.Members.Insert(c.Group, &member).Do()
return err
}
func (c *Client) RemoveMember(email string) error {
return c.adminSvc.Members.Delete(c.Group, email).Do()
}