/
add_user.go
53 lines (48 loc) · 1.5 KB
/
add_user.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
package googleApi
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/acs-dl/mail-module-svc/internal/data"
"gitlab.com/distributed_lab/logan/v3/errors"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/googleapi"
)
func (g *googleApi) AddUserInDomainFromApi(firstName, lastName, email string) (*data.User, error) {
response, err := g.client.Users.Insert(&admin.User{
ChangePasswordAtNextLogin: true,
Name: &admin.UserName{
FamilyName: lastName,
GivenName: firstName,
},
Emails: []admin.UserEmail{
{
Type: "work",
Address: email,
},
},
PrimaryEmail: fmt.Sprintf("%s.%s@centrilisedgym.online", firstName, lastName),
Password: fmt.Sprintf("%s.%s@centrilisedgym.online", firstName, lastName),
}).Do()
if err != nil {
if apiErr, ok := err.(*googleapi.Error); ok {
if apiErr.Code == http.StatusTooManyRequests {
durationInSeconds, err := strconv.ParseInt(apiErr.Header.Get("Retry-After"), 10, 64)
if err != nil {
return nil, errors.Wrap(err, "failed to parse `retry-after` header")
}
timeoutDuration := time.Second * time.Duration(durationInSeconds)
time.Sleep(timeoutDuration)
return g.AddUserInDomainFromApi(firstName, lastName, email)
}
}
g.log.WithError(err).Errorf("failed to add user in domain with email `%s`", email)
return nil, errors.Wrap(err, "failed to add user in domain")
}
return &data.User{
Email: response.PrimaryEmail,
Name: firstName + " " + lastName,
MailId: response.Id,
}, nil
}