forked from dainis/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 18
/
template.go
110 lines (96 loc) · 3.41 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
package zabbix
// Template represent Zabbix Template type returned from Zabbix API
// https://www.zabbix.com/documentation/3.2/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"`
Groups HostGroupIDs `json:"groups"`
UserMacros Macros `json:"macros,omitempty"`
LinkedTemplates Templates `json:"templates,omitempty"`
TemplatesClear Templates `json:"templates_clear,omitempty"`
LinkedHosts Hosts `json:"hosts,omitempty"`
}
// Templates is an Array of Template structs.
type Templates []Template
// TemplateID use with host creation
type TemplateID struct {
TemplateID string `json:"templateid"`
}
// TemplateIDs is an Array of TemplateID structs.
type TemplateIDs []TemplateID
// TemplatesGet 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"
}
err = api.CallWithErrorParse("template.get", params, &res)
return
}
// TemplateGetByID Gets template by Id only if there is exactly 1 matching template.
func (api *API) TemplateGetByID(id string) (template *Template, err error) {
templates, err := api.TemplatesGet(Params{"templateids": id})
if err != nil {
return
}
if len(templates) == 1 {
template = &templates[0]
} else {
e := ExpectedOneResult(len(templates))
err = &e
}
return
}
// TemplatesCreate Wrapper for template.create
// https://www.zabbix.com/documentation/3.2/manual/api/reference/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
}
// TemplatesUpdate Wrapper for template.update
// https://www.zabbix.com/documentation/3.2/manual/api/reference/template/update
func (api *API) TemplatesUpdate(templates Templates) (err error) {
_, err = api.CallWithError("template.update", templates)
return
}
// TemplatesDelete Wrapper for template.delete
// Cleans ApplicationID in all apps elements if call succeed.
// https://www.zabbix.com/documentation/3.2/manual/api/reference/template/delete
func (api *API) TemplatesDelete(templates Templates) (err error) {
templatesIds := make([]string, len(templates))
for i, template := range templates {
templatesIds[i] = template.TemplateID
}
err = api.TemplatesDeleteByIds(templatesIds)
if err == nil {
for i := range templates {
templates[i].TemplateID = ""
}
}
return
}
// TemplatesDeleteByIds Wrapper for template.delete
// Use template's id to delete the template
// https://www.zabbix.com/documentation/3.2/manual/api/reference/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
}