/
hosts.go
88 lines (72 loc) · 1.71 KB
/
hosts.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
package upcloud
import (
"encoding/json"
"time"
)
// Hosts represents a GetHosts response
type Hosts struct {
Hosts []Host `json:"hosts"`
}
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (n *Hosts) UnmarshalJSON(b []byte) error {
type localHost Host
type hostWrapper struct {
Hosts []localHost `json:"host"`
}
v := struct {
Hosts hostWrapper `json:"hosts"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
for _, ln := range v.Hosts.Hosts {
n.Hosts = append(n.Hosts, Host(ln))
}
return nil
}
// StatSlice is a slice of Stat structs
// This exsits to support a custom unmarshaller
type StatSlice []Stat
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (t *StatSlice) UnmarshalJSON(b []byte) error {
v := struct {
Networks []Stat `json:"stat"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
(*t) = v.Networks
return nil
}
// Host represents an individual Host in a response
type Host struct {
ID int `json:"id"`
Description string `json:"description"`
Zone string `json:"zone"`
WindowsEnabled Boolean `json:"windows_enabled"`
Stats StatSlice `json:"stats"`
}
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (s *Host) UnmarshalJSON(b []byte) error {
type localHost Host
v := struct {
Host localHost `json:"host"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
(*s) = Host(v.Host)
return nil
}
// Stat represents Host stats in a response
type Stat struct {
Name string `json:"name"`
Timestamp time.Time `json:"timestamp"`
Value float64 `json:"value"`
}