forked from dainis/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 18
/
template_group.go
95 lines (83 loc) · 3.02 KB
/
template_group.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
package zabbix
// TemplateGroup represent Zabbix template group object, new in v6.2
// https://www.zabbix.com/documentation/6.2/en/manual/api/reference/templategroup/object
type TemplateGroup struct {
GroupID string `json:"groupid,omitempty"`
Name string `json:"name"`
}
// TemplateGroups is an array of TemplateGroup
type TemplateGroups []TemplateGroup
// TemplateGroupsGet Wrapper for templategroup.get
// https://www.zabbix.com/documentation/6.2/en/manual/api/reference/templategroup/get
func (api *API) TemplateGroupsGet(params Params) (res TemplateGroups, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
err = api.CallWithErrorParse("templategroup.get", params, &res)
return
}
// TemplateGroupGetByID Gets host group by Id only if there is exactly 1 matching host group.
// https://www.zabbix.com/documentation/6.2/en/manual/api/reference/templategroup/get
func (api *API) TemplateGroupGetByID(id string) (res *TemplateGroup, err error) {
groups, err := api.TemplateGroupsGet(Params{"groupids": id})
if err != nil {
return
}
if len(groups) == 1 {
res = &groups[0]
} else {
e := ExpectedOneResult(len(groups))
err = &e
}
return
}
// TemplateGroupsCreate Wrapper for templategroup.create
// https://www.zabbix.com/documentation/6.2/en/manual/api/reference/templategroup/create
func (api *API) TemplateGroupsCreate(TemplateGroups TemplateGroups) (err error) {
response, err := api.CallWithError("templategroup.create", TemplateGroups)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
groupids := result["groupids"].([]interface{})
for i, id := range groupids {
TemplateGroups[i].GroupID = id.(string)
}
return
}
// TemplateGroupsUpdate Wrapper for templategroup.update
// https://www.zabbix.com/documentation/6.2/en/manual/api/reference/templategroup/update
func (api *API) TemplateGroupsUpdate(TemplateGroups TemplateGroups) (err error) {
_, err = api.CallWithError("templategroup.update", TemplateGroups)
return
}
// TemplateGroupsDelete Wrapper for templategroup.delete
// Cleans GroupId in all TemplateGroups elements if call succeed.
// https://www.zabbix.com/documentation/6.2/en/manual/api/reference/templategroup/delete
func (api *API) TemplateGroupsDelete(TemplateGroups TemplateGroups) (err error) {
ids := make([]string, len(TemplateGroups))
for i, group := range TemplateGroups {
ids[i] = group.GroupID
}
err = api.TemplateGroupsDeleteByIds(ids)
if err == nil {
for i := range TemplateGroups {
TemplateGroups[i].GroupID = ""
}
}
return
}
// TemplateGroupsDeleteByIds Wrapper for templategroup.delete
// https://www.zabbix.com/documentation/6.2/en/manual/api/reference/templategroup/delete
func (api *API) TemplateGroupsDeleteByIds(ids []string) (err error) {
response, err := api.CallWithError("templategroup.delete", ids)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
groupids := result["groupids"].([]interface{})
if len(ids) != len(groupids) {
err = &ExpectedMore{len(ids), len(groupids)}
}
return
}