forked from AlekSi/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
host_group.go
112 lines (94 loc) · 2.92 KB
/
host_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package zabbix
import (
"github.com/AlekSi/reflector"
)
type (
InternalType int
)
const (
NotInternal InternalType = 0
Internal InternalType = 1
)
// https://www.zabbix.com/documentation/2.2/manual/appendix/api/hostgroup/definitions
type HostGroup struct {
GroupId string `json:"groupid,omitempty"`
Name string `json:"name"`
Internal InternalType `json:"internal,omitempty"`
}
type HostGroups []HostGroup
type HostGroupId struct {
GroupId string `json:"groupid"`
}
type HostGroupIds []HostGroupId
// Wrapper for hostgroup.get: https://www.zabbix.com/documentation/2.2/manual/appendix/api/hostgroup/get
func (api *API) HostGroupsGet(params Params) (res HostGroups, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
response, err := api.CallWithError("hostgroup.get", params)
if err != nil {
return
}
reflector.MapsToStructs2(response.Result.([]interface{}), &res, reflector.Strconv, "json")
return
}
// Gets host group by Id only if there is exactly 1 matching host group.
func (api *API) HostGroupGetById(id string) (res *HostGroup, err error) {
groups, err := api.HostGroupsGet(Params{"groupids": id})
if err != nil {
return
}
if len(groups) == 1 {
res = &groups[0]
} else {
e := ExpectedOneResult(len(groups))
err = &e
}
return
}
// Wrapper for hostgroup.create: https://www.zabbix.com/documentation/2.2/manual/appendix/api/hostgroup/create
func (api *API) HostGroupsCreate(hostGroups HostGroups) (err error) {
response, err := api.CallWithError("hostgroup.create", hostGroups)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
groupids := result["groupids"].([]interface{})
for i, id := range groupids {
hostGroups[i].GroupId = id.(string)
}
return
}
// Wrapper for hostgroup.update: https://www.zabbix.com/documentation/2.2/manual/api/reference/hostgroup/update
func (api *API) HostGroupsUpdate(hostGroups HostGroups) (err error) {
_, err = api.CallWithError("hostgroup.update", hostGroups)
return
}
// Wrapper for hostgroup.delete: https://www.zabbix.com/documentation/2.2/manual/appendix/api/hostgroup/delete
// Cleans GroupId in all hostGroups elements if call succeed.
func (api *API) HostGroupsDelete(hostGroups HostGroups) (err error) {
ids := make([]string, len(hostGroups))
for i, group := range hostGroups {
ids[i] = group.GroupId
}
err = api.HostGroupsDeleteByIds(ids)
if err == nil {
for i := range hostGroups {
hostGroups[i].GroupId = ""
}
}
return
}
// Wrapper for hostgroup.delete: https://www.zabbix.com/documentation/2.2/manual/appendix/api/hostgroup/delete
func (api *API) HostGroupsDeleteByIds(ids []string) (err error) {
response, err := api.CallWithError("hostgroup.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
}