-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
77 lines (65 loc) · 1.83 KB
/
helpers.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
package gsuite_admin_app
import (
"context"
"errors"
"fmt"
"golang.org/x/oauth2"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
)
func (this App) Service(tok *oauth2.Token, ctx ...context.Context) (service *admin.Service, err error) {
client := this.Crendentials.Client(Context(ctx...), tok)
return admin.NewService(context.Background(), option.WithHTTPClient(client))
}
func (this App) LoadDomain(authCode string, ctx ...context.Context) (domain string, tok *oauth2.Token, err error) {
if tok, err = this.Crendentials.Exchange(context.TODO(), authCode); err != nil {
err = fmt.Errorf("unable to load token from auth code: %v", err)
return
}
service, err := this.Service(tok, ctx...)
if err != nil {
err = fmt.Errorf("unable to create service %v", err)
return
}
domains, err := admin.NewDomainsService(service).List(this.Customer).Do()
if err != nil {
err = fmt.Errorf("unable to retrieve domains: %v", err)
return
}
if len(domains.Domains) == 0 {
err = errors.New("account not have domains")
return
}
for _, d := range domains.Domains {
if d.IsPrimary {
domain = d.DomainName
return
}
}
err = fmt.Errorf("no domains found: %v", err)
return
}
func (this App) FindDomain(tok *oauth2.Token, ctx ...context.Context) (domain string, err error) {
service, err := this.Service(tok, ctx...)
if err != nil {
err = fmt.Errorf("unable to create service %v", err)
return
}
domains, err := admin.NewDomainsService(service).List(this.Customer).Do()
if err != nil {
err = fmt.Errorf("unable to retrieve domains: %v", err)
return
}
if len(domains.Domains) == 0 {
err = errors.New("account not have domains")
return
}
for _, d := range domains.Domains {
if d.IsPrimary {
domain = d.DomainName
return
}
}
err = fmt.Errorf("no domains found: %v", err)
return
}