-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
97 lines (83 loc) · 2.73 KB
/
users.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
package usermanagement
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/zscaler/zscaler-sdk-go/zia/services/common"
)
const (
usersEndpoint = "/users"
)
type Users struct {
ID int `json:"id"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Groups []common.IDNameExtensions `json:"groups,omitempty"`
Department *common.UserDepartment `json:"department,omitempty"`
Comments string `json:"comments,omitempty"`
TempAuthEmail string `json:"tempAuthEmail,omitempty"`
Password string `json:"password,omitempty"`
AdminUser bool `json:"adminUser"`
Type string `json:"type,omitempty"`
Deleted bool `json:"deleted"`
}
func (service *Service) Get(userID int) (*Users, error) {
var user Users
err := service.Client.Read(fmt.Sprintf("%s/%d", usersEndpoint, userID), &user)
if err != nil {
return nil, err
}
service.Client.Logger.Printf("[DEBUG]returning user from Get: %d", user.ID)
return &user, nil
}
func (service *Service) GetUserByName(userName string) (*Users, error) {
var users []Users
err := service.Client.Read(fmt.Sprintf("%s?name=%s", usersEndpoint, url.QueryEscape(userName)), &users)
if err != nil {
return nil, err
}
for _, user := range users {
if strings.EqualFold(user.Name, userName) {
return &user, nil
}
}
return nil, fmt.Errorf("no user found with name: %s", userName)
}
func (service *Service) Create(userID *Users) (*Users, error) {
resp, err := service.Client.Create(usersEndpoint, *userID)
if err != nil {
return nil, err
}
createdUsers, ok := resp.(*Users)
if !ok {
return nil, errors.New("object returned from api was not a user pointer")
}
service.Client.Logger.Printf("[DEBUG]returning user from create: %v", createdUsers.ID)
return createdUsers, nil
}
func (service *Service) Update(userID int, users *Users) (*Users, *http.Response, error) {
resp, err := service.Client.UpdateWithPut(fmt.Sprintf("%s/%d", usersEndpoint, userID), *users)
if err != nil {
return nil, nil, err
}
updatedUser, _ := resp.(*Users)
service.Client.Logger.Printf("[DEBUG]returning user from update: %d", updatedUser.ID)
return updatedUser, nil, nil
}
func (service *Service) Delete(userID int) (*http.Response, error) {
err := service.Client.Delete(fmt.Sprintf("%s/%d", usersEndpoint, userID))
if err != nil {
return nil, err
}
return nil, nil
}
func (service *Service) GetAllUsers() ([]Users, error) {
var users []Users
err := service.Client.Read(usersEndpoint, &users)
if err != nil {
return nil, err
}
return users, nil
}