-
Notifications
You must be signed in to change notification settings - Fork 23
/
role.go
152 lines (132 loc) · 4.21 KB
/
role.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package thousandeyes
import (
"encoding/json"
"fmt"
)
// AccountGroupRole - an account group role
type AccountGroupRole struct {
RoleName *string `json:"roleName,omitempty"`
RoleID *int64 `json:"roleId,omitempty"`
HasManagementPermissions *bool `json:"hasManagementPermissions,omitempty" te:"int-bool"`
Builtin *bool `json:"builtin,omitempty" te:"int-bool"`
Permissions *[]Permission `json:"permissions,omitempty"`
}
// Permission - permission attached to roles
type Permission struct {
IsManagementPermission *bool `json:"isManagementPermission" te:"int-bool"`
Label *string `json:"label"`
PermissionID *int64 `json:"permissionId"`
}
// MarshalJSON implements the json.Marshaler interface. It ensures
// that ThousandEyes int fields that only use the values 0 or 1 are
// treated as booleans.
func (t AccountGroupRole) MarshalJSON() ([]byte, error) {
type alias AccountGroupRole
data, err := json.Marshal((alias)(t))
if err != nil {
return nil, err
}
return jsonBoolToInt(&t, data)
}
// UnmarshalJSON implements the json.Unmarshaler interface. It ensures
// that ThousandEyes int fields that only use the values 0 or 1 are
// treated as booleans.
func (t *AccountGroupRole) UnmarshalJSON(data []byte) error {
type alias AccountGroupRole
test := (*alias)(t)
data, err := jsonIntToBool(t, data)
if err != nil {
return err
}
return json.Unmarshal(data, &test)
}
// MarshalJSON implements the json.Marshaler interface. It ensures
// that ThousandEyes int fields that only use the values 0 or 1 are
// treated as booleans.
func (t Permission) MarshalJSON() ([]byte, error) {
type alias Permission
data, err := json.Marshal((alias)(t))
if err != nil {
return nil, err
}
return jsonBoolToInt(&t, data)
}
// UnmarshalJSON implements the json.Unmarshaler interface. It ensures
// that ThousandEyes int fields that only use the values 0 or 1 are
// treated as booleans.
func (t *Permission) UnmarshalJSON(data []byte) error {
type alias Permission
test := (*alias)(t)
data, err := jsonIntToBool(t, data)
if err != nil {
return err
}
return json.Unmarshal(data, &test)
}
// GetRoles - get roles
func (c *Client) GetRoles() (*[]AccountGroupRole, error) {
resp, err := c.get("/roles")
if err != nil {
return nil, err
}
var target map[string][]AccountGroupRole
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("could not decode JSON response: %v", dErr)
}
roles := target["roles"]
return &roles, nil
}
// GetRole - get role
func (c *Client) GetRole(id int64) (*AccountGroupRole, error) {
resp, err := c.get(fmt.Sprintf("/roles/%d", id))
if err != nil {
return nil, err
}
var target map[string][]AccountGroupRole
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("could not decode JSON response: %v", dErr)
}
role := target["roles"][0]
return &role, nil
}
// DeleteRole - delete role
func (c *Client) DeleteRole(id int64) error {
resp, err := c.post(fmt.Sprintf("/roles/%d/delete", id), nil, nil)
if err != nil {
return err
}
if resp.StatusCode != 204 {
return fmt.Errorf("failed to delete role, response code %d", resp.StatusCode)
}
return nil
}
// UpdateRole - update role
func (c *Client) UpdateRole(id int64, role AccountGroupRole) (*AccountGroupRole, error) {
resp, err := c.post(fmt.Sprintf("/roles/%d/update", id), role, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to update role, response code %d", resp.StatusCode)
}
var target AccountGroupRole
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("could not decode JSON response: %v", dErr)
}
return &target, nil
}
// CreateRole - create role
func (c *Client) CreateRole(user AccountGroupRole) (*AccountGroupRole, error) {
resp, err := c.post("/roles/new", user, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != 201 {
return nil, fmt.Errorf("failed to update role, response code %d", resp.StatusCode)
}
var target AccountGroupRole
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("could not decode JSON response: %v", dErr)
}
return &target, nil
}