forked from dainis/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 18
/
application.go
109 lines (95 loc) · 3.1 KB
/
application.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
package zabbix
// Application represent Zabbix application object
// https://www.zabbix.com/documentation/3.2/manual/api/reference/application/object
type Application struct {
ApplicationID string `json:"applicationid,omitempty"`
HostID string `json:"hostid"`
Name string `json:"name"`
TemplateID string `json:"templateid,omitempty"`
}
// Applications is an array of Application
type Applications []Application
// ApplicationsGet Wrapper for application.get
// https://www.zabbix.com/documentation/3.2/manual/api/reference/application/get
func (api *API) ApplicationsGet(params Params) (res Applications, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
err = api.CallWithErrorParse("application.get", params, &res)
if err != nil {
return
}
return
}
// ApplicationGetByID Gets application by Id only if there is exactly 1 matching application.
func (api *API) ApplicationGetByID(id string) (res *Application, err error) {
apps, err := api.ApplicationsGet(Params{"applicationids": id})
if err != nil {
return
}
if len(apps) == 1 {
res = &apps[0]
} else {
e := ExpectedOneResult(len(apps))
err = &e
}
return
}
// ApplicationGetByHostIDAndName Gets application by host Id and name only if there is exactly 1 matching application.
func (api *API) ApplicationGetByHostIDAndName(hostID, name string) (res *Application, err error) {
apps, err := api.ApplicationsGet(Params{"hostids": hostID, "filter": map[string]string{"name": name}})
if err != nil {
return
}
if len(apps) == 1 {
res = &apps[0]
} else {
e := ExpectedOneResult(len(apps))
err = &e
}
return
}
// ApplicationsCreate Wrapper for application.create
// https://www.zabbix.com/documentation/3.2/manual/api/reference/application/create
func (api *API) ApplicationsCreate(apps Applications) (err error) {
response, err := api.CallWithError("application.create", apps)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
applicationids := result["applicationids"].([]interface{})
for i, id := range applicationids {
apps[i].ApplicationID = id.(string)
}
return
}
// ApplicationsDelete Wrapper for application.delete:
// Cleans ApplicationID in all apps elements if call succeed.
// https://www.zabbix.com/documentation/2.2/manual/appendix/api/application/delete
func (api *API) ApplicationsDelete(apps Applications) (err error) {
ids := make([]string, len(apps))
for i, app := range apps {
ids[i] = app.ApplicationID
}
err = api.ApplicationsDeleteByIds(ids)
if err == nil {
for i := range apps {
apps[i].ApplicationID = ""
}
}
return
}
// ApplicationsDeleteByIds Wrapper for application.delete
// https://www.zabbix.com/documentation/2.2/manual/appendix/api/application/delete
func (api *API) ApplicationsDeleteByIds(ids []string) (err error) {
response, err := api.CallWithError("application.delete", ids)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
applicationids := result["applicationids"].([]interface{})
if len(ids) != len(applicationids) {
err = &ExpectedMore{len(ids), len(applicationids)}
}
return
}