-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsuite.go
79 lines (65 loc) · 1.92 KB
/
gsuite.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
package main
import (
"fmt"
"context"
"golang.org/x/oauth2/google"
admin "google.golang.org/api/admin/directory/v1"
plugin "github.com/defensestation/pluginutils"
)
type Gsuite struct {
plugin *plugin.Plugin
client *admin.Service
}
func NewGsuitePlugin(plugin *plugin.Plugin) *Gsuite {
return &Gsuite{
plugin: plugin,
}
}
func (g *Gsuite) Run(ctx context.Context) (error) {
err := g.plugin.ValidateOptions("json_creds", "customer_id", "admin_email")
if err != nil {
return err
}
jsonCreds, _ := g.plugin.GetOption("json_creds");
customerId, _ := g.plugin.GetOption("customer_id");
adminEmail, _ := g.plugin.GetOption("admin_email");
err = g.setGsuiteClient(ctx,jsonCreds.(string), customerId.(string), adminEmail.(string))
if err != nil {
return fmt.Errorf("Unable setup gsuite client: %v", err)
}
// if user is given
if _, ok := g.plugin.GetOption("users"); ok {
err := g.getUsers(customerId.(string))
if err != nil {
return err
}
}
return nil
}
func (g *Gsuite) setGsuiteClient(ctx context.Context, jsonCredentials, customer_id, admin_email string) (error) {
config, err := google.JWTConfigFromJSON(
[]byte(jsonCredentials),
admin.AdminDirectoryGroupScope,
admin.AdminDirectoryUserReadonlyScope,
admin.AdminDirectoryOrgunitScope,
admin.AdminDirectoryDomainScope,
admin.AdminDirectoryRolemanagementScope,
admin.AdminDirectoryUserschemaScope,
admin.AdminDirectoryDeviceMobileScope,
admin.AdminDirectoryResourceCalendarScope,
// reports.AdminReportsAuditReadonlyScope,
// reports.AdminReportsUsageReadonlyScope,
)
if err != nil {
return fmt.Errorf("Unable to parse client secret file to config: %v", err)
}
// Set your admin user email
config.Subject = admin_email
// define service
srv, err := admin.New(config.Client(ctx))
if err != nil {
return fmt.Errorf("Unable to retrieve directory Client %v", err)
}
g.client = srv
return nil
}