forked from selectel-vds/go-vscale
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscalet.go
357 lines (255 loc) · 9.15 KB
/
scalet.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package vscale_api_go
import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"net/http"
"strconv"
)
type ScaletService struct {
client Client
}
type Scalet struct {
Hostname string `json:"hostname,omitempty"`
Locked bool `json:"locked,omitempty"`
Location string `json:"location,omitempty"`
Rplan string `json:"rplan,omitempty"`
Name string `json:"name,omitempty"`
Active bool `json:"active,omitempty"`
Keys []Keys `json:"keys,omitempty"`
PublicAddresses PublicAddress `json:"public_address,omitempty"`
Status string `json:"status,omitempty"`
MadeFrom string `json:"made_from,omitempty"`
CTID int64 `json:"ctid,omitempty"`
}
type Backup struct {
ID string `json:"id,omitempty"`
Template string `json:"template,omitempty"`
Active bool `json:"active"`
Name string `json:"name"`
Scalet int64 `json:"scalet"`
Status string `json:"status"`
Size float64 `json:"size"`
Locked bool `json:"locked"`
Location string `json:"location"`
Created string `json:"created"`
}
type Keys struct {
Name string `json:"name,omitempty"`
ID int64 `json:"id,omitempty"`
}
type PublicAddress struct {
Netmask string `json:"netmask,omitempty"`
Gateway string `json:"gateway,omitempty"`
Address string `json:"address,omitempty"`
}
type Task struct {
Location string `json:"location,omitempty"`
DInsert string `json:"d_insert,omitempty"`
ID string `json:"id,omitempty"`
Done bool `json:"done,omitempty"`
Scalet int64 `json:"scalet,omitempty"`
Error bool `json:"error,omitempty"`
DStart string `json:"d_start,omitempty"`
Method string `json:"method,omitempty"`
DEnd string `json:"d_end,omitempty"`
}
func (s *ScaletService) List() (*[]Scalet, *http.Response, error) {
scalets := new([]Scalet)
res, err := s.client.ExecuteRequest("GET", "scalets", []byte{}, scalets)
return scalets, res, err
}
func (s *ScaletService) Create(hostname, makeFrom, rplan, name, password, location string, doStart bool,
keys []int64, wait bool) (*Scalet, *http.Response, error) {
scalet := new(Scalet)
conn := new(websocket.Conn)
var wsserr error
if wait {
conn, wsserr = s.client.WSSConn()
defer conn.Close()
}
body := struct {
Hostname string `json:"hostname,omitempty"`
MakeFrom string `json:"make_from,omitempty"`
Rplan string `json:"rplan,omitempty"`
DoStart bool `json:"do_start,omitempty"`
Name string `json:"name,omitempty"`
Keys []int64 `json:"keys,omitempty"`
Password string `json:"password,omitempty"`
Location string `json:"location,omitempty"`
}{hostname, makeFrom, rplan, doStart, name, keys, password, location}
b, _ := json.Marshal(body)
res, err := s.client.ExecuteRequest("POST", "scalets", b, scalet)
if wait && wsserr == nil && res.Header.Get("VSCALE-TASK-ID") != "" {
_, err := s.client.WaitTask(conn, res.Header.Get("VSCALE-TASK-ID"))
return scalet, res, err
}
return scalet, res, err
}
func (s *ScaletService) CreateWithoutPassword(hostname, makeFrom, rplan, name, location string, doStart bool,
keys []int64, wait bool) (*Scalet, *http.Response, error) {
scalet := new(Scalet)
conn := new(websocket.Conn)
var wsserr error
if wait {
conn, wsserr = s.client.WSSConn()
defer conn.Close()
}
body := struct {
Hostname string `json:"hostname,omitempty"`
MakeFrom string `json:"make_from,omitempty"`
Rplan string `json:"rplan,omitempty"`
DoStart bool `json:"do_start,omitempty"`
Name string `json:"name,omitempty"`
Keys []int64 `json:"keys,omitempty"`
Location string `json:"location,omitempty"`
}{hostname, makeFrom, rplan, doStart, name, keys, location}
b, _ := json.Marshal(body)
res, err := s.client.ExecuteRequest("POST", "scalets", b, scalet)
if wait && wsserr == nil && res.Header.Get("VSCALE-TASK-ID") != "" {
_, err := s.client.WaitTask(conn, res.Header.Get("VSCALE-TASK-ID"))
return scalet, res, err
}
return scalet, res, err
}
func (s *ScaletService) Remove(CTID int64, wait bool) (*Scalet, *http.Response, error) {
scalet := new(Scalet)
conn := new(websocket.Conn)
var wsserr error
if wait {
conn, wsserr = s.client.WSSConn()
defer conn.Close()
}
res, err := s.client.ExecuteRequest("DELETE", fmt.Sprint("scalets/", strconv.FormatInt(CTID, 10)), []byte{}, scalet)
if wait && wsserr == nil && res.Header.Get("VSCALE-TASK-ID") != "" {
_, err := s.client.WaitTask(conn, res.Header.Get("VSCALE-TASK-ID"))
return scalet, res, err
}
return scalet, res, err
}
func (s *ScaletService) Get(CTID int64) (*Scalet, *http.Response, error) {
scalet := new(Scalet)
res, err := s.client.ExecuteRequest("GET", fmt.Sprint("scalets/", strconv.FormatInt(CTID, 10)), []byte{}, scalet)
return scalet, res, err
}
func (s *ScaletService) Restart(CTID int64, wait bool) (*Scalet, *http.Response, error) {
scalet := new(Scalet)
conn := new(websocket.Conn)
var wsserr error
if wait {
conn, wsserr = s.client.WSSConn()
defer conn.Close()
}
res, err := s.client.ExecuteRequest("PATCH", fmt.Sprint("scalets/", strconv.FormatInt(CTID, 10), "/restart"), []byte{}, scalet)
if wait && wsserr == nil && res.Header.Get("VSCALE-TASK-ID") != "" {
_, err := s.client.WaitTask(conn, res.Header.Get("VSCALE-TASK-ID"))
return scalet, res, err
}
return scalet, res, err
}
func (s *ScaletService) Rebuild(CTID int64, wait bool) (*Scalet, *http.Response, error) {
scalet := new(Scalet)
conn := new(websocket.Conn)
var wsserr error
if wait {
conn, wsserr = s.client.WSSConn()
defer conn.Close()
}
res, err := s.client.ExecuteRequest("PATCH", fmt.Sprint("scalets/", strconv.FormatInt(CTID, 10), "/rebuild"), []byte{}, scalet)
if wait && wsserr == nil && res.Header.Get("VSCALE-TASK-ID") != "" {
_, err := s.client.WaitTask(conn, res.Header.Get("VSCALE-TASK-ID"))
return scalet, res, err
}
return scalet, res, err
}
func (s *ScaletService) Stop(CTID int64, wait bool) (*Scalet, *http.Response, error) {
scalet := new(Scalet)
conn := new(websocket.Conn)
var wsserr error
if wait {
conn, wsserr = s.client.WSSConn()
defer conn.Close()
}
res, err := s.client.ExecuteRequest("PATCH", fmt.Sprint("scalets/", strconv.FormatInt(CTID, 10), "/stop"), []byte{}, scalet)
if wait && wsserr == nil && res.Header.Get("VSCALE-TASK-ID") != "" {
_, err := s.client.WaitTask(conn, res.Header.Get("VSCALE-TASK-ID"))
return scalet, res, err
}
return scalet, res, err
}
func (s *ScaletService) Start(CTID int64, wait bool) (*Scalet, *http.Response, error) {
scalet := new(Scalet)
conn := new(websocket.Conn)
var wsserr error
if wait {
conn, wsserr = s.client.WSSConn()
defer conn.Close()
}
res, err := s.client.ExecuteRequest("PATCH", fmt.Sprint("scalets/", strconv.FormatInt(CTID, 10), "/start"), []byte{}, scalet)
if wait && wsserr == nil && res.Header.Get("VSCALE-TASK-ID") != "" {
_, err := s.client.WaitTask(conn, res.Header.Get("VSCALE-TASK-ID"))
return scalet, res, err
}
return scalet, res, err
}
func (s *ScaletService) Upgrade(CTID int64, rplan string, wait bool) (*Scalet, *http.Response, error) {
scalet := new(Scalet)
body := struct {
Rplan string `json:"rplan,omitempty"`
}{rplan}
b, _ := json.Marshal(body)
conn := new(websocket.Conn)
var wsserr error
if wait {
conn, wsserr = s.client.WSSConn()
defer conn.Close()
}
res, err := s.client.ExecuteRequest("POST", fmt.Sprint("scalets/", strconv.FormatInt(CTID, 10), "/upgrade"), b, scalet)
if wait && wsserr == nil && res.Header.Get("VSCALE-TASK-ID") != "" {
_, err := s.client.WaitTask(conn, res.Header.Get("VSCALE-TASK-ID"))
return scalet, res, err
}
return scalet, res, err
}
func (s *ScaletService) Tasks() (*[]Task, *http.Response, error) {
tasks := new([]Task)
res, err := s.client.ExecuteRequest("GET", "tasks", []byte{}, tasks)
return tasks, res, err
}
func (s *ScaletService) AddSSHKeys(CTID int64, keys []int64) (*Scalet, *http.Response, error) {
scalet := new(Scalet)
body := struct {
Keys []int64 `json:"keys,omitempty"`
}{keys}
b, _ := json.Marshal(body)
res, err := s.client.ExecuteRequest("PATCH", fmt.Sprint("scalets/", strconv.FormatInt(CTID, 10)), b, scalet)
return scalet, res, err
}
func (s *ScaletService) Backup(CTID int64, name string) (*Backup, *http.Response, error) {
backup := new(Backup)
body := struct {
Name string `json:"name,omitempty"`
}{name}
b, _ := json.Marshal(body)
res, err := s.client.ExecuteRequest("POST", fmt.Sprintf("scalets/%d/backup", CTID), b, backup)
return backup, res, err
}
func (s *ScaletService) Restore(CTID int64, makeFrom string, wait bool) (*Scalet, *http.Response, error) {
scalet := new(Scalet)
conn := new(websocket.Conn)
var wsserr error
if wait {
conn, wsserr = s.client.WSSConn()
defer conn.Close()
}
body := struct {
MakeFrom string `json:"make_from,omitempty"`
}{makeFrom}
b, _ := json.Marshal(body)
res, err := s.client.ExecuteRequest("PATCH", fmt.Sprintf("scalets/%d/rebuild", CTID), b, scalet)
if wait && wsserr == nil && res.Header.Get("VSCALE-TASK-ID") != "" {
_, err := s.client.WaitTask(conn, res.Header.Get("VSCALE-TASK-ID"))
return scalet, res, err
}
return scalet, res, err
}