-
Notifications
You must be signed in to change notification settings - Fork 71
/
hostgroup_json.go
55 lines (44 loc) · 1.26 KB
/
hostgroup_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
package zabbix
import (
"fmt"
)
// jHostgroup is a private map for the Hostgroup Zabbix API object (see zabbix documentation).
type jHostgroup struct {
GroupID string `json:"groupid"`
Name string `json:"name"`
Flags string `json:"flags"`
Internal string `json:"internal"`
Hosts jHosts `json:"hosts,omitempty"`
}
// Hostgroup returns a native Go Hostgroup struct mapped from the given JSON Hostgroup data.
func (c *jHostgroup) Hostgroup() (*Hostgroup, error) {
hostgroup := &Hostgroup{}
hostgroup.GroupID = c.GroupID
hostgroup.Name = c.Name
hostgroup.Flags = c.Flags
hostgroup.Internal = c.Internal
if len(c.Hosts) > 0 {
if hosts, err := c.Hosts.Hosts(); err == nil {
hostgroup.Hosts = hosts
}
}
return hostgroup, nil
}
// jHostgroups is a slice of jHostgroup structs.
type jHostgroups []jHostgroup
// Hostgroups returns a native Go slice of Hostgroups mapped from the given JSON Hostgroups
// data.
func (c jHostgroups) Hostgroups() ([]Hostgroup, error) {
if c != nil {
hosts := make([]Hostgroup, len(c))
for i, jhost := range c {
host, err := jhost.Hostgroup()
if err != nil {
return nil, fmt.Errorf("Error unmarshalling Hostgroup %d in JSON data: %v", i, err)
}
hosts[i] = *host
}
return hosts, nil
}
return nil, nil
}