forked from zabbix-tools/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 2
/
host_json.go
60 lines (51 loc) · 1.38 KB
/
host_json.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
package zabbix
import (
"fmt"
//"strconv"
)
// jHost is a private map for the Zabbix API Host object.
// See: https://www.zabbix.com/documentation/2.2/manual/api/reference/host/object
type jHost struct {
HostID string `json:"hostid"`
Hostname string `json:"host"`
Flags int `json:"flags,string,omitempty"`
Name string `json:"name,omitempty"`
Macros []HostMacro `json:"macros,omitempty"`
Groups []Hostgroup `json:"groups,omitempty"`
}
// Host returns a native Go Host struct mapped from the given JSON Host data.
func (c *jHost) Host() (*Host, error) {
//var err error
host := &Host{}
host.HostID = c.HostID
host.Hostname = c.Hostname
host.DisplayName = c.Name
host.Macros = c.Macros
host.Groups = c.Groups
/*
host.Source, err = strconv.Atoi(c.Flags)
if err != nil {
return nil, fmt.Errorf("Error parsing Host Flags: %v", err)
}
*/
host.Source = c.Flags
return host, nil
}
// jHosts is a slice of jHost structs.
type jHosts []jHost
// Hosts returns a native Go slice of Hosts mapped from the given JSON Hosts
// data.
func (c jHosts) Hosts() ([]Host, error) {
if c != nil {
hosts := make([]Host, len(c))
for i, jhost := range c {
host, err := jhost.Host()
if err != nil {
return nil, fmt.Errorf("Error unmarshalling Host %d in JSON data: %v", i, err)
}
hosts[i] = *host
}
return hosts, nil
}
return nil, nil
}