/
groups.go
206 lines (168 loc) · 5.21 KB
/
groups.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"fmt"
"log"
"google.golang.org/api/admin/directory/v1"
"github.com/urfave/cli"
)
// group list ----------------------------------------------
var commandGroupList = cli.Command{
Name: "grouplist",
Usage: "Shows current groups",
Description: `Shows current groups on Google Apps for Work
The record format:
[ID] EMAIL (NAME DESCRIPTION)
`,
Action: doGroupList,
}
func doGroupList(c *cli.Context) error {
srv, err := NewService()
if err != nil {
return nil
}
r, err := srv.directory.Groups.List().Customer("my_customer").Do()
if err != nil {
log.Fatalf("Unable to retrieve groups in domain. %v", err)
return nil
}
count := len(r.Groups)
if count == 0 {
fmt.Print("No groups found.\n")
} else {
for _, g := range r.Groups {
fmt.Printf("[%v] %v (%v: %v)\n", g.Id, g.Email, g.Name, g.Description)
}
}
return nil
}
// group create ----------------------------------------------
var commandGroupCreate = cli.Command{
Name: "groupcreate",
Usage: "Create a new group",
Description: "Create a new group on Google Apps for Work",
Action: doGroupCreate,
Flags: []cli.Flag{
cli.StringFlag{Name: "email, e", Value: "", Usage: "The group's email address"},
cli.StringFlag{Name: "description, d", Value: "", Usage: "An extended description to help users determine the purpose of a group"},
cli.StringFlag{Name: "name, n", Value: "", Usage: "The group's name"},
},
}
func doGroupCreate(c *cli.Context) error {
srv, err := NewService()
if err != nil {
return nil
}
email := c.String("email")
description := c.String("description")
name := c.String("name")
log.Printf("Email:%v, Description:%v, Name:%v", email, description, name)
group := &admin.Group{
Email: email,
Description: description,
Name: name,
}
group2, err := srv.directory.Groups.Insert(group).Do()
if err != nil {
log.Fatalf("Cannot create group in domain. %v", err)
} else {
log.Printf("Succeed to create group: %v", group2)
}
return nil
}
// group member list ----------------------------------------------
var commandGroupMemberList = cli.Command{
Name: "groupmemberlist",
Usage: "Shows a list of all members in a group",
Description: "Shows a list of all members in a group on Google Apps for Work",
Action: doGroupMemberList,
Flags: []cli.Flag{
cli.StringFlag{Name: "key, k", Value: "", Usage: "The group key"},
},
}
func doGroupMemberList(c *cli.Context) error {
srv, err := NewService()
if err != nil {
return nil
}
groupKey := c.String("key")
r, err := srv.directory.Members.List(groupKey).Do()
if err != nil {
log.Fatalf("Unable to retrieve group members in domain. %v", err)
return nil
}
count := len(r.Members)
if count == 0 {
fmt.Print("No group members found.\n")
} else {
for _, m := range r.Members {
fmt.Printf("%v, %v\n", m.Email, m.Role)
}
}
return nil
}
// group create ----------------------------------------------
var commandGroupMemberCreate = cli.Command{
Name: "groupmembercreate",
Usage: "Add a member to the specified group",
Description: "Add a member to the specified group on Google Apps for Work",
Action: doGroupMemberCreate,
Flags: []cli.Flag{
cli.StringFlag{Name: "key, k", Value: "", Usage: "The group key"},
cli.StringFlag{Name: "email, e", Value: "", Usage: "The member's email address"},
cli.StringFlag{Name: "role, r", Value: "", Usage: "A group member's role can be: OWNER / MANAGER / MEMBER. The default is MEMBER, see: https://goo.gl/VIo3zH"},
},
}
// Role is a group member role
type Role int
const (
// DEFAULT equals MEMBER
DEFAULT Role = iota
// OWNER role can change send messages to the group, add or remove members, change member roles, change group's settings, and delete the group. An OWNER must be a member of the group.
OWNER
// MANAGER role is only available if the Google Groups for Business is enabled using the Admin console. A MANAGER role can do everything done by an OWNER role except make a member an OWNER or delete the group. A group can have multiple OWNER and MANAGER members.
MANAGER
// MEMBER role can subscribe to a group, view discussion archives, and view the group's membership list. For more information about member roles, see https://goo.gl/10gxHA.
MEMBER
)
func (r Role) String() string {
switch r {
case OWNER:
return "OWNER"
case MANAGER:
return "MANAGER"
default:
return "MEMBER"
}
}
func roleFromString(role string) Role {
switch {
case role == "OWNER":
return OWNER
case role == "MANAGER":
return MANAGER
default:
return MEMBER
}
}
func doGroupMemberCreate(c *cli.Context) error {
srv, err := NewService()
if err != nil {
return nil
}
groupKey := c.String("key")
email := c.String("email")
role := c.String("role")
roleStr := roleFromString(role)
member := &admin.Member{
Email: email,
Role: roleStr.String(),
}
log.Printf("Key:%v, Email:%v, Role:%v[%v], Member:%v", groupKey, email, role, roleStr, member)
member2, err := srv.directory.Members.Insert(groupKey, member).Do()
if err != nil {
log.Fatalf("Cannot add a member to the specified group in domain. %v", err)
return nil
}
log.Printf("Succeed to add a member to the specified group: %v, member: %v", groupKey, member2.Email)
return nil
}