/
server.go
210 lines (173 loc) · 5.56 KB
/
server.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package upcloud
import (
"encoding/json"
)
// Constants
const (
ServerStateStarted = "started"
ServerStateStopped = "stopped"
ServerStateMaintenance = "maintenance"
ServerStateError = "error"
VideoModelVGA = "vga"
VideoModelCirrus = "cirrus"
StopTypeSoft = "soft"
StopTypeHard = "hard"
RemoteAccessTypeVNC = "vnc"
RemoteAccessTypeSPICE = "spice"
)
// ServerConfigurations represents a /server_size response
type ServerConfigurations struct {
ServerConfigurations []ServerConfiguration `json:"server_sizes"`
}
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (s *ServerConfigurations) UnmarshalJSON(b []byte) error {
type serverConfigurationWrapper struct {
ServerConfigurations []ServerConfiguration `json:"server_size"`
}
v := struct {
ServerConfigurations serverConfigurationWrapper `json:"server_sizes"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
s.ServerConfigurations = v.ServerConfigurations.ServerConfigurations
return nil
}
// ServerConfiguration represents a server configuration
type ServerConfiguration struct {
CoreNumber int `json:"core_number,string"`
MemoryAmount int `json:"memory_amount,string"`
}
// Servers represents a /server response
type Servers struct {
Servers []Server `json:"servers"`
}
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (s *Servers) UnmarshalJSON(b []byte) error {
type serverWrapper struct {
Servers []Server `json:"server"`
}
v := struct {
Servers serverWrapper `json:"servers"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
s.Servers = v.Servers.Servers
return nil
}
// ServerTagSlice is a slice of string.
// It exists to allow for a custom JSON unmarshaller.
type ServerTagSlice []string
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (t *ServerTagSlice) UnmarshalJSON(b []byte) error {
v := struct {
Tags []string `json:"tag"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
(*t) = v.Tags
return nil
}
// Server represents a server
type Server struct {
CoreNumber int `json:"core_number,string"`
Hostname string `json:"hostname"`
License float64 `json:"license"`
MemoryAmount int `json:"memory_amount,string"`
Plan string `json:"plan"`
Progress int `json:"progress,string"`
State string `json:"state"`
Tags ServerTagSlice `json:"tags"`
Title string `json:"title"`
UUID string `json:"uuid"`
Zone string `json:"zone"`
}
// IPAddressSlice is a slice of IPAddress.
// It exists to allow for a custom JSON unmarshaller.
type IPAddressSlice []IPAddress
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (i *IPAddressSlice) UnmarshalJSON(b []byte) error {
type localIPAddress IPAddress
v := struct {
IPAddresses []localIPAddress `json:"ip_address"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
for _, ip := range v.IPAddresses {
(*i) = append((*i), IPAddress(ip))
}
return nil
}
// ServerStorageDeviceSlice is a slice of ServerStorageDevices.
// It exists to allow for a custom JSON unmarshaller.
type ServerStorageDeviceSlice []ServerStorageDevice
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (s *ServerStorageDeviceSlice) UnmarshalJSON(b []byte) error {
v := struct {
StorageDevices []ServerStorageDevice `json:"storage_device"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
(*s) = v.StorageDevices
return nil
}
// ServerNetworking represents the networking on a server response.
// It is castable to a Networking struct.
type ServerNetworking Networking
// ServerDetails represents details about a server
type ServerDetails struct {
Server
BootOrder string `json:"boot_order"`
// TODO: Convert to boolean
Firewall string `json:"firewall"`
Host int `json:"host"`
IPAddresses IPAddressSlice `json:"ip_addresses"`
Metadata Boolean `json:"metadata"`
NICModel string `json:"nic_model"`
Networking ServerNetworking `json:"networking"`
SimpleBackup string `json:"simple_backup"`
StorageDevices ServerStorageDeviceSlice `json:"storage_devices"`
Timezone string `json:"timezone"`
VideoModel string `json:"video_model"`
RemoteAccessEnabled Boolean `json:"remote_access_enabled"`
RemoteAccessType string `json:"remote_access_type"`
RemoteAccessHost string `json:"remote_access_host"`
RemoteAccessPassword string `json:"remote_access_password"`
RemoteAccessPort int `json:"remote_access_port,string"`
}
func (s *ServerDetails) StorageDevice(storageUUID string) *ServerStorageDevice {
for _, storageDevice := range s.StorageDevices {
if storageDevice.UUID == storageUUID {
return &storageDevice
}
}
return nil
}
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (s *ServerDetails) UnmarshalJSON(b []byte) error {
type localServerDetails ServerDetails
v := struct {
ServerDetails localServerDetails `json:"server"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
(*s) = ServerDetails(v.ServerDetails)
return nil
}