forked from neogan74/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost_json.go
78 lines (69 loc) · 2.19 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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"`
Description string `json:"description,omitempty"`
Status int `json:"status,string"`
ProxyID string `json:"proxy_hostid,omitempty"`
TLSConnect int `json:"tls_connect,string"`
TLSAccept int `json:"tls_accept,string"`
TLSIssuer string `json:"tls_issuer,omitempty"`
TLSSubject string `json:"tls_subject,omitempty"`
TLSPSKIdentity string `json:"tls_psk_identity,omitempty"`
TLSPSK string `json:"tls_psk,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.Description = c.Description
host.Status = strconv.Itoa(c.Status)
host.ProxyID = c.ProxyID
host.TLSConnect = c.TLSConnect
host.TLSAccept = c.TLSAccept
host.TLSIssuer = c.TLSIssuer
host.TLSSubject = c.TLSSubject
host.TLSPSKIdentity = c.TLSPSKIdentity
host.TLSPSK = c.TLSPSK
/*
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
}