forked from AlekSi/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
108 lines (93 loc) · 3.01 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
package zabbix
import (
"github.com/AlekSi/reflector"
)
// https://www.zabbix.com/documentation/2.0/manual/appendix/api/application/definitions
type Application struct {
ApplicationId string `json:"applicationid,omitempty"`
HostId string `json:"hostid"`
Name string `json:"name"`
TemplateId string `json:"templateid,omitempty"`
}
type Applications []Application
// Wrapper for application.get: https://www.zabbix.com/documentation/2.0/manual/appendix/api/application/get
func (api *API) ApplicationsGet(params Params) (res Applications, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
response, err := api.CallWithError("application.get", params)
if err != nil {
return
}
reflector.MapsToStructs2(response.Result.([]interface{}), &res, reflector.Strconv, "json")
return
}
// 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
}
// 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
}
// Wrapper for application.create: https://www.zabbix.com/documentation/2.0/manual/appendix/api/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
}
// Wrapper for application.delete: https://www.zabbix.com/documentation/2.0/manual/appendix/api/application/delete
// Cleans ApplicationId in all apps elements if call succeed.
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
}
// Wrapper for application.delete: https://www.zabbix.com/documentation/2.0/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
}