forked from thousandeyes/thousandeyes-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
90 lines (83 loc) · 2.58 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
package thousandeyes
import (
"fmt"
"time"
)
// User - a user
type User struct {
Name *string `json:"name,omitempty"`
Email *string `json:"email,omitempty"`
UID *int64 `json:"uid,omitempty"`
LastLogin *time.Time `json:"lastLogin,omitempty"`
DateRegistered *time.Time `json:"dateRegistered,omitempty"`
LoginAccountGroup *AccountGroup `json:"loginAccountGroup,omitempty"`
AccountGroupRoles *[]AccountGroupRole `json:"accountGroupRoles,omitempty"`
AllAccountGroupRoles *[]AccountGroupRole `json:"allAccountGroupRoles,omitempty"`
}
// GetUsers - get users
func (c *Client) GetUsers() (*[]User, error) {
resp, err := c.get("/users")
if err != nil {
return nil, err
}
var target map[string][]User
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("could not decode JSON response: %v", dErr)
}
users := target["users"]
return &users, nil
}
// GetUser - get user
func (c *Client) GetUser(id int64) (*User, error) {
resp, err := c.get(fmt.Sprintf("/users/%d", id))
if err != nil {
return nil, err
}
var target map[string][]User
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("could not decode JSON response: %v", dErr)
}
user := target["users"][0]
return &user, nil
}
// DeleteUser - delete user
func (c *Client) DeleteUser(id int64) error {
resp, err := c.post(fmt.Sprintf("/users/%d/delete", id), nil, nil)
if err != nil {
return err
}
if resp.StatusCode != 204 {
return fmt.Errorf("failed to delete alert rule, response code %d", resp.StatusCode)
}
return nil
}
// UpdateUser - update user
func (c *Client) UpdateUser(id int64, user User) (*User, error) {
resp, err := c.post(fmt.Sprintf("/users/%d/update", id), user, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to update alert rule, response code %d", resp.StatusCode)
}
var target User
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("could not decode JSON response: %v", dErr)
}
return &target, nil
}
// CreateUser - create user
func (c *Client) CreateUser(user User) (*User, error) {
resp, err := c.post("/users/new", user, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != 201 {
return nil, fmt.Errorf("failed to update alert rule, response code %d", resp.StatusCode)
}
var target User
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("could not decode JSON response: %v", dErr)
}
return &target, nil
}