forked from claranet/go-zabbix-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
role.go
128 lines (109 loc) · 2.56 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
package zabbix
import (
"fmt"
)
type Role struct {
RoleID string `json:"roleid,omitempty" zabbix:"id"`
Name string `json:"name"`
Type RoleType `json:"type,string"`
ReadOnly int `json:"readonly,string,omitempty"`
}
type RoleType int
const (
UserRole RoleType = iota + 1
AdminRole
SuperAdminRole
)
var ValidRoleTypes = []string{"user", "admin", "super_admin"}
type Roles []Role
func (api *API) RolesGet(params Params) (res Roles, err error) {
err = api.CallWithErrorParse("role.get", params, &res)
return
}
func (api *API) RoleGetByID(id string) (res Role, err error) {
var roles Roles
err = api.CallWithErrorParse("role.get", Params{"roleids": id}, &roles)
if len(roles) == 0 {
err = fmt.Errorf("role with ID: %s not found", id)
return
}
if err != nil {
return
}
res = roles[0]
return
}
func (api *API) RoleGetByName(roleName string) (role Role, err error) {
params := Params{
"filter": map[string]interface{}{
"name": roleName,
},
}
roles, err := api.RolesGet(params)
role = roles[0]
return
}
func (api *API) RolesCreateAndSetIDs(roles Roles) (err error) {
response, err := api.CallWithError("role.create", roles)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
ids := result["roleids"].([]interface{})
if len(ids) == 0 {
return fmt.Errorf("could not create roles")
}
for i, id := range ids {
roles[i].RoleID = id.(string)
}
return
}
func (api *API) RolesDeleteByIDs(ids []string) (err error) {
_, err = api.CallWithError("role.delete", ids)
return
}
func (api *API) RolesDeleteByID(id string) (err error) {
_, err = api.CallWithError("role.delete", []string{id})
return
}
func (api *API) RolesUpdate(roles Roles) (err error) {
_, err = api.CallWithError("role.update", roles)
return
}
func (role *Role) String() string {
return role.Name
}
func (role *Role) GetType() (roleType string, err error) {
switch role.Type {
case UserRole:
return "user", nil
case AdminRole:
return "admin", nil
case SuperAdminRole:
return "super_admin", nil
default:
return "", fmt.Errorf("invalid_user_type %d", role.Type)
}
}
func NewRoleType(roleTypeString string) (roleType RoleType, err error) {
switch roleTypeString {
case "user":
roleType = UserRole
case "admin":
roleType = AdminRole
case "super_admin":
roleType = SuperAdminRole
default:
err = fmt.Errorf("invalid role type: %s", roleTypeString)
}
return
}
func (role *Role) GetID() string {
return role.RoleID
}
func (role *Role) SetID(s string) {
role.RoleID = s
}
func (role *Role) GetAPIModule() string {
return "role"
}