-
Notifications
You must be signed in to change notification settings - Fork 3
/
servers.go
261 lines (222 loc) · 7.49 KB
/
servers.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package cloudscale
import (
"context"
"fmt"
"net/http"
"reflect"
"time"
)
const serverBasePath = "v1/servers"
const ServerRunning = "running"
const ServerStopped = "stopped"
const ServerRebooted = "rebooted"
type Server struct {
ZonalResource
TaggedResource
HREF string `json:"href"`
UUID string `json:"uuid"`
Name string `json:"name"`
Status string `json:"status"`
Flavor Flavor `json:"flavor"`
Image Image `json:"image"`
Volumes []VolumeStub `json:"volumes"`
Interfaces []Interface `json:"interfaces"`
SSHFingerprints []string `json:"ssh_fingerprints"`
SSHHostKeys []string `json:"ssh_host_keys"`
AntiAfinityWith []ServerStub `json:"anti_affinity_with"`
ServerGroups []ServerGroupStub `json:"server_groups"`
CreatedAt time.Time `json:"created_at"`
}
type ServerStub struct {
HREF string `json:"href"`
UUID string `json:"uuid"`
}
type ServerGroupStub struct {
HREF string `json:"href"`
UUID string `json:"uuid"`
Name string `json:"name"`
}
type Flavor struct {
Slug string `json:"slug"`
Name string `json:"name"`
VCPUCount int `json:"vcpu_count"`
MemoryGB int `json:"memory_gb"`
}
type Image struct {
Slug string `json:"slug"`
Name string `json:"name"`
OperatingSystem string `json:"operating_system"`
}
type VolumeStub struct {
Type string `json:"type"`
DevicePath string `json:"device_path"`
SizeGB int `json:"size_gb"`
UUID string `json:"uuid"`
}
type Interface struct {
Type string `json:"type,omitempty"`
Network NetworkStub `json:"network,omitempty"`
Addresses []Address `json:"addresses,omitempty"`
}
type Address struct {
Version int `json:"version"`
Address string `json:"address"`
PrefixLength int `json:"prefix_length"`
Gateway string `json:"gateway"`
ReversePtr string `json:"reverse_ptr"`
Subnet SubnetStub `json:"subnet"`
}
type ServerRequest struct {
ZonalResourceRequest
TaggedResourceRequest
Name string `json:"name"`
Flavor string `json:"flavor"`
Image string `json:"image"`
Zone string `json:"zone,omitempty"`
VolumeSizeGB int `json:"volume_size_gb,omitempty"`
Volumes *[]ServerVolumeRequest `json:"volumes,omitempty"`
Interfaces *[]InterfaceRequest `json:"interfaces,omitempty"`
BulkVolumeSizeGB int `json:"bulk_volume_size_gb,omitempty"`
SSHKeys []string `json:"ssh_keys"`
Password string `json:"password,omitempty"`
UsePublicNetwork *bool `json:"use_public_network,omitempty"`
UsePrivateNetwork *bool `json:"use_private_network,omitempty"`
UseIPV6 *bool `json:"use_ipv6,omitempty"`
AntiAffinityWith string `json:"anti_affinity_with,omitempty"`
ServerGroups []string `json:"server_groups,omitempty"`
UserData string `json:"user_data,omitempty"`
}
type ServerVolumeRequest struct {
SizeGB int `json:"size_gb,omitempty"`
Type string `json:"type,omitempty"`
}
type InterfaceRequest struct {
Network string `json:"network,omitempty"`
Addresses *[]AddressRequest `json:"addresses,omitempty"`
}
type AddressRequest struct {
Subnet string `json:"subnet,omitempty"`
Address string `json:"address,omitempty"`
}
type ServerService interface {
Create(ctx context.Context, createRequest *ServerRequest) (*Server, error)
Get(ctx context.Context, serverID string) (*Server, error)
Update(ctx context.Context, serverID string, updateRequest *ServerUpdateRequest) error
Delete(ctx context.Context, serverID string) error
List(ctx context.Context, modifiers ...ListRequestModifier) ([]Server, error)
Reboot(ctx context.Context, serverID string) error
Start(ctx context.Context, serverID string) error
Stop(ctx context.Context, serverID string) error
}
type ServerServiceOperations struct {
client *Client
}
func (s ServerServiceOperations) Create(ctx context.Context, createRequest *ServerRequest) (*Server, error) {
path := serverBasePath
req, err := s.client.NewRequest(ctx, http.MethodPost, path, createRequest)
if err != nil {
return nil, err
}
server := new(Server)
err = s.client.Do(ctx, req, server)
if err != nil {
return nil, err
}
return server, nil
}
type ServerUpdateRequest struct {
TaggedResourceRequest
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
Flavor string `json:"flavor,omitempty"`
Interfaces *[]InterfaceRequest `json:"interfaces,omitempty"`
}
func (s ServerServiceOperations) Update(ctx context.Context, serverID string, updateRequest *ServerUpdateRequest) error {
if updateRequest.Status != "" {
err := error(nil)
switch updateRequest.Status {
case ServerRunning:
err = s.Start(ctx, serverID)
case ServerStopped:
err = s.Stop(ctx, serverID)
case ServerRebooted:
err = s.Reboot(ctx, serverID)
default:
return fmt.Errorf("Status Not Supported %s", updateRequest.Status)
}
if err != nil {
return err
}
// Get rid of status
updateRequest.Status = ""
}
emptyRequest := ServerUpdateRequest{}
if !reflect.DeepEqual(emptyRequest, *updateRequest) {
path := fmt.Sprintf("%s/%s", serverBasePath, serverID)
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, updateRequest)
if err != nil {
return err
}
err = s.client.Do(ctx, req, nil)
if err != nil {
return err
}
}
return nil
}
func (s ServerServiceOperations) Get(ctx context.Context, serverID string) (*Server, error) {
path := fmt.Sprintf("%s/%s", serverBasePath, serverID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
server := new(Server)
err = s.client.Do(ctx, req, server)
if err != nil {
return nil, err
}
return server, nil
}
func (s ServerServiceOperations) Delete(ctx context.Context, serverID string) error {
return genericDelete(s.client, ctx, serverBasePath, serverID)
}
func (s ServerServiceOperations) Reboot(ctx context.Context, serverID string) error {
path := fmt.Sprintf("%s/%s/reboot", serverBasePath, serverID)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return err
}
return s.client.Do(ctx, req, nil)
}
func (s ServerServiceOperations) Start(ctx context.Context, serverID string) error {
path := fmt.Sprintf("%s/%s/start", serverBasePath, serverID)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return err
}
return s.client.Do(ctx, req, nil)
}
func (s ServerServiceOperations) Stop(ctx context.Context, serverID string) error {
path := fmt.Sprintf("%s/%s/stop", serverBasePath, serverID)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return err
}
return s.client.Do(ctx, req, nil)
}
func (s ServerServiceOperations) List(ctx context.Context, modifiers ...ListRequestModifier) ([]Server, error) {
path := serverBasePath
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
for _, modifier := range modifiers {
modifier(req)
}
servers := []Server{}
err = s.client.Do(ctx, req, &servers)
if err != nil {
return nil, err
}
return servers, nil
}