-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
117 lines (93 loc) · 3.35 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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 {
// User ID
ID int `json:"id"`
// User name. This appears when choosing users for policies.
Name string `json:"name,omitempty"`
// User email consists of a user name and domain name. It does not have to be a valid email address, but it must be unique and its domain must belong to the organization.
Email string `json:"email,omitempty"`
// List of Groups a user belongs to. Groups are used in policies.
Groups []common.IDNameExtensions `json:"groups,omitempty"`
// Department a user belongs to
Department *common.UserDepartment `json:"department,omitempty"`
// Additional information about this user.
Comments string `json:"comments,omitempty"`
// Temporary Authentication Email. If you enabled one-time tokens or links, enter the email address to which the Zscaler service sends the tokens or links. If this is empty, the service sends the email to the User email.
TempAuthEmail string `json:"tempAuthEmail,omitempty"`
// User's password. Applicable only when authentication type is Hosted DB. Password strength must follow what is defined in the auth settings.
Password string `json:"password,omitempty"`
// True if this user is an Admin user
AdminUser bool `json:"adminUser"`
// User type. Provided only if this user is not an end user.
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 := common.ReadAllPages(service.Client, usersEndpoint, &users)
if err != nil {
return nil, err
}
return users, nil
}