forked from AlekSi/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
146 lines (128 loc) · 4.56 KB
/
template.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
// templates
package zabbix
import (
"github.com/wOvAN/reflector"
)
const (
// Template Selectors
SelectGroups = "selectGroups"
SelectHosts = "selectHosts"
SelectTemplates = "selectTemplates"
SelectParentTemplates = "selectParentTemplates"
SelectHttpTests = "selectHttpTests"
SelectItems = "selectItems"
SelectDiscoveries = "selectDiscoveries"
SelectTriggers = "selectTriggers"
SelectGraphs = "selectGraphs"
SelectApplications = "selectApplications"
SelectMacros = "selectMacros"
SelectScreens = "selectScreens"
)
// https://www.zabbix.com/documentation/4.0/manual/api/reference/template/object
type Template struct {
TemplateId string `json:"templateid,omitempty"`
Host string `json:"host"`
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
// Extended fields
Groups HostGroups `json:"groups,omitempty"`
Templates Templates `json:"templates,omitempty"`
Items Items `json:"items,omitempty"`
Hosts Hosts `json:"hosts,omitempty"`
ParentTemplates Templates `json:"parentTemplates,omitempty"`
HttpTests string `json:"httpTests,omitempty"`
Discoveries string `json:"discoveries,omitempty"`
Triggers Triggers `json:"triggers,omitempty"`
Graphs string `json:"graphs,omitempty"`
Applications string `json:"applications,omitempty"`
Macros string `json:"macros,omitempty"`
Screens string `json:"screens,omitempty"`
}
type Templates []Template
type TemplateId struct {
TemplateId string `json:"templateid"`
}
type TemplateIds []TemplateId
// Wrapper for template.get: https://www.zabbix.com/documentation/3.2/manual/api/reference/template/get
func (api *API) TemplatesGet(params Params) (res Templates, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
response, err := api.CallWithError("template.get", params)
if err != nil {
return
}
reflector.MapsToStructs2(response.Result.([]interface{}), &res, reflector.Strconv, "json")
return
}
// Wrapper for template.update: https://www.zabbix.com/documentation/4.0/manual/api/reference/template/update
func (api *API) TemplatesUpdate(params Params) (res TemplateIds, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
response, err := api.CallWithError("template.update", params)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
templateids := result["templateids"].([]interface{})
for _, id := range templateids {
res = append(res, TemplateId{TemplateId: id.(string)})
}
return
}
// Gets host template by Id only if there is exactly 1 matching host template.
func (api *API) TemplateGetById(id string) (res *Template, err error) {
templates, err := api.TemplatesGet(Params{"templateids": id})
if err != nil {
return
}
if len(templates) == 1 {
res = &templates[0]
} else {
e := ExpectedOneResult(len(templates))
err = &e
}
return
}
// Wrapper for template.create: https://www.zabbix.com/documentation/2.2/manual/appendix/api/template/create
func (api *API) TemplatesCreate(templates Templates) (err error) {
response, err := api.CallWithError("template.create", templates)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
templateids := result["templateids"].([]interface{})
for i, id := range templateids {
templates[i].TemplateId = id.(string)
}
return
}
// Wrapper for template.delete: https://www.zabbix.com/documentation/2.2/manual/appendix/api/template/delete
// Cleans TemplateId in all templates elements if call succeed.
func (api *API) TemplatesDelete(templates Templates) (err error) {
ids := make([]string, len(templates))
for i, template := range templates {
ids[i] = template.TemplateId
}
err = api.TemplatesDeleteByIds(ids)
if err == nil {
for i := range templates {
templates[i].TemplateId = ""
}
}
return
}
// Wrapper for template.delete: https://www.zabbix.com/documentation/2.2/manual/appendix/api/template/delete
func (api *API) TemplatesDeleteByIds(ids []string) (err error) {
response, err := api.CallWithError("template.delete", ids)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
templateids := result["templateids"].([]interface{})
if len(ids) != len(templateids) {
err = &ExpectedMore{len(ids), len(templateids)}
}
return
}