forked from nixys/nxs-go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usermacro.go
137 lines (104 loc) · 4.05 KB
/
usermacro.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
package zabbix
// For `UsermacroObject` field: `Type`
const (
UsermacroTypeText = 0
UsermacroTypeSecret = 1
)
// UsermacroObject struct is used to store hostmacro and globalmacro operations results.
// In API docs Global and Host it is a two different object types that are joined in this package
// into one object `UsermacroObject` that includes fields form both API objects.
// The reason is the some other objects do not separates this types.
//
// see: https://www.zabbix.com/documentation/5.0/manual/api/reference/usermacro/object#host_macro
// and https://www.zabbix.com/documentation/5.0/manual/api/reference/usermacro/object#global_macro
type UsermacroObject struct {
// Gobal macro fields only
GlobalmacroID int `json:"globalmacroid,omitempty"`
// Host macro fields only
HostmacroID int `json:"hostmacroid,omitempty"`
HostID int `json:"hostid,omitempty"`
// Common fields
Macro string `json:"macro,omitempty"`
Value string `json:"value,omitempty"`
Type int `json:"type,omitempty"` // has defined consts, see above
Description string `json:"description,omitempty"`
Groups []HostgroupObject `json:"groups,omitempty"`
Hosts []HostObject `json:"hosts,omitempty"`
Templates []TemplateObject `json:"templates,omitempty"`
}
// UsermacroGetParams struct is used for hostmacro get requests
//
// see: https://www.zabbix.com/documentation/5.0/manual/api/reference/usermacro/get#parameters
type UsermacroGetParams struct {
GetParameters
Globalmacro bool `json:"globalmacro,omitempty"`
GlobalmacroIDs []int `json:"globalmacroids,omitempty"`
GroupIDs []int `json:"groupids,omitempty"`
HostIDs []int `json:"hostids,omitempty"`
HostmacroIDs []int `json:"hostmacroids,omitempty"`
TemplateIDs []int `json:"templateids,omitempty"`
SelectGroups SelectQuery `json:"selectGroups,omitempty"`
SelectHosts SelectQuery `json:"selectHosts,omitempty"`
SelectTemplates SelectQuery `json:"selectTemplates,omitempty"`
}
// Structure to store creation result
type hostmacroCreateResult struct {
HostmacroIDs []int `json:"hostmacroids"`
}
// Structure to store creation global macros result
type globalmacroCreateResult struct {
GlobalmacroIDs []int `json:"globalmacroids"`
}
// Structure to store deletion result
type hostmacroDeleteResult struct {
HostmacroIDs []int `json:"hostmacroids"`
}
// Structure to store deletion global macros result
type globalmacroDeleteResult struct {
GlobalmacroIDs []int `json:"globalmacroids"`
}
// UsermacroGet gets global or host macros according to the given parameters
func (z *Context) UsermacroGet(params UsermacroGetParams) ([]UsermacroObject, int, error) {
var result []UsermacroObject
status, err := z.request("usermacro.get", params, &result)
if err != nil {
return nil, status, err
}
return result, status, nil
}
// HostmacroCreate creates new hostmacros
func (z *Context) HostmacroCreate(params []UsermacroObject) ([]int, int, error) {
var result hostmacroCreateResult
status, err := z.request("usermacro.create", params, &result)
if err != nil {
return nil, status, err
}
return result.HostmacroIDs, status, nil
}
// GlobalmacroCreate creates new globalmacros
func (z *Context) GlobalmacroCreate(params []UsermacroObject) ([]int, int, error) {
var result globalmacroCreateResult
status, err := z.request("usermacro.createglobal", params, &result)
if err != nil {
return nil, status, err
}
return result.GlobalmacroIDs, status, nil
}
// HostmacroDelete deletes hostmacros
func (z *Context) HostmacroDelete(hostmacroIDs []int) ([]int, int, error) {
var result hostmacroDeleteResult
status, err := z.request("usermacro.delete", hostmacroIDs, &result)
if err != nil {
return nil, status, err
}
return result.HostmacroIDs, status, nil
}
// GlobalmacroDelete deletes globalmacros
func (z *Context) GlobalmacroDelete(globalmacroIDs []int) ([]int, int, error) {
var result globalmacroDeleteResult
status, err := z.request("usermacro.deleteglobal", globalmacroIDs, &result)
if err != nil {
return nil, status, err
}
return result.GlobalmacroIDs, status, nil
}