forked from claranet/go-zabbix-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_group.go
116 lines (100 loc) · 3.32 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
113
114
115
116
package zabbix
type (
// InternalType (readonly) Whether the group is used internally by the system. An internal group cannot be deleted.
// see "internal" in https://www.zabbix.com/documentation/3.2/manual/api/reference/hostgroup/object
InternalType int
)
const (
// NotInternal (default) not internal
NotInternal InternalType = 0
// Internal internal
Internal InternalType = 1
)
// HostGroup represent Zabbix host group object
// https://www.zabbix.com/documentation/3.2/manual/api/reference/hostgroup/object
type HostGroup struct {
GroupID string `json:"groupid,omitempty"`
Name string `json:"name"`
Internal InternalType `json:"internal,omitempty,string"`
}
// HostGroups is an array of HostGroup
type HostGroups []HostGroup
// HostGroupID represent Zabbix GroupID
type HostGroupID struct {
GroupID string `json:"groupid"`
}
// HostGroupIDs is an array of HostGroupId
type HostGroupIDs []HostGroupID
// HostGroupsGet Wrapper for hostgroup.get
// https://www.zabbix.com/documentation/3.2/manual/api/reference/hostgroup/get
func (api *API) HostGroupsGet(params Params) (res HostGroups, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
err = api.CallWithErrorParse("hostgroup.get", params, &res)
return
}
// HostGroupGetByID 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
}
// HostGroupsCreate Wrapper for hostgroup.create
// https://www.zabbix.com/documentation/3.2/manual/api/reference/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
}
// HostGroupsUpdate Wrapper for hostgroup.update
// https://www.zabbix.com/documentation/3.2/manual/api/reference/hostgroup/update
func (api *API) HostGroupsUpdate(hostGroups HostGroups) (err error) {
_, err = api.CallWithError("hostgroup.update", hostGroups)
return
}
// HostGroupsDelete Wrapper for hostgroup.delete
// Cleans GroupId in all hostGroups elements if call succeed.
// https://www.zabbix.com/documentation/3.2/manual/api/reference/hostgroup/delete
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
}
// HostGroupsDeleteByIds Wrapper for hostgroup.delete
// https://www.zabbix.com/documentation/3.2/manual/api/reference/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
}