-
Notifications
You must be signed in to change notification settings - Fork 1
/
scalet.go
298 lines (256 loc) · 7.73 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
package vscale
import (
"fmt"
)
const (
scaletsBasePath = "/v1/scalets"
tasksBasePath = "/v1/tasks"
)
type ScaletService interface {
List() (*[]Scalet, *Response, error)
GetByID(int) (*Scalet, *Response, error)
Create(*ScaletCreateRequest) (*Scalet, *Response, error)
Restart(int) (*Scalet, *Response, error)
Rebuild(*ScaletRebuildRequest) (*Scalet, *Response, error)
Halt(int) (*Scalet, *Response, error)
Start(int) (*Scalet, *Response, error)
UpdatePlan(*ScaletUpdatePlanRequest) (*Scalet, *Response, error)
Delete(int) (*Scalet, *Response, error)
AddSSHKeyToScalet(*SSHKeyAppendRequest) (*Scalet, *Response, error)
Tasks() (*[]ScaletTask, *Response, error)
}
type ScaletServiceOp struct {
client *Client
}
var _ ScaletService = &ScaletServiceOp{}
type Scalet struct {
Name string `json:"name,omitempty"`
Hostname string `json:"hostname,omitempty"`
Locked bool `json:"locked,omitempty"`
Location string `json:"location,omitempty"`
Rplan string `json:"rplan,omitempty"`
Active bool `json:"active,omitempty"`
Keys []SSHKey `json:"keys,omitempty"`
PublicAddress *ScaletAddress `json:"public_address,omitempty"`
Status string `json:"status,omitempty"`
MadeFrom string `json:"made_from,omitempty"`
CTID int `json:"ctid,omitempty"`
PrivateAddress *ScaletAddress `json:"private_address,omitempty"`
}
type ScaletAddress struct {
Address string `json:"address,omitempty"`
Netmask string `json:"netmask,omitempty"`
Gateway string `json:"gateway,omitempty"`
}
type ScaletCreateRequest struct {
MakeFrom string `json:"make_from"`
Rplan string `json:"rplan"`
DoStart bool `json:"do_start"`
Name string `json:"name"`
Keys []int `json:"keys"`
Password string `json:"password"`
Location string `json:"location"`
}
type ScaletRebuildRequest struct {
ID int
Password string `json:"password"`
}
type ScaletUpdatePlanRequest struct {
ID int
Rplan string `json:"rplan"`
}
type SSHKeyAppendRequest struct {
CTID int
Keys []int `json:"keys"`
}
type ScaletTask struct {
ID string `json:"id,omitempty"`
Location string `json:"location,omitempty"`
InsertDate string `json:"d_insert,omitempty"`
StartDate string `json:"d_start,omitempty"`
EndDate string `json:"d_end,omitempty"`
Done bool `json:"done,omitempty"`
ScaletId int `json:"scalet,omitempty"`
Error bool `json:"error,omitempty"`
Method string `json:"method,omitempty"`
}
func (s Scalet) String() string {
return Stringify(s)
}
func (s ScaletServiceOp) List() (*[]Scalet, *Response, error) {
path := scaletsBasePath
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
scalets := &[]Scalet{}
resp, err := s.client.Do(req, scalets)
if err != nil {
return nil, nil, err
}
return scalets, resp, err
}
func (s ScaletServiceOp) GetByID(ctid int) (*Scalet, *Response, error) {
if ctid < 1 {
return nil, nil, NewArgError("scaletID", "cannot be less than 1")
}
path := fmt.Sprintf("%s/%d", scaletsBasePath, ctid)
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
scalet := &Scalet{}
resp, err := s.client.Do(req, scalet)
if err != nil {
return nil, nil, err
}
return scalet, resp, err
}
func (s ScaletServiceOp) Create(createRequest *ScaletCreateRequest) (*Scalet, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}
req, err := s.client.NewRequest("POST", scaletsBasePath, createRequest)
if err != nil {
return nil, nil, err
}
scalet := new(Scalet)
resp, err := s.client.Do(req, scalet)
if err != nil {
return nil, nil, err
}
return scalet, resp, err
}
func (s ScaletServiceOp) Restart(ctid int) (*Scalet, *Response, error) {
if ctid < 1 {
return nil, nil, NewArgError("scaletID", "cannot be less than 1")
}
path := fmt.Sprintf("%s/%d/restart", scaletsBasePath, ctid)
req, err := s.client.NewRequest("PATCH", path, nil)
if err != nil {
return nil, nil, err
}
scalet := &Scalet{}
resp, err := s.client.Do(req, scalet)
if err != nil {
return nil, nil, err
}
return scalet, resp, err
}
func (s *ScaletServiceOp) Rebuild(rebuildRequest *ScaletRebuildRequest) (*Scalet, *Response, error) {
if rebuildRequest == nil {
return nil, nil, NewArgError("rebuildRequest", "cannot be nil")
}
path := fmt.Sprintf("%s/%d/rebuild", scaletsBasePath, rebuildRequest.ID)
req, err := s.client.NewRequest("PATCH", path, rebuildRequest)
if err != nil {
return nil, nil, err
}
scalet := &Scalet{}
resp, err := s.client.Do(req, scalet)
if err != nil {
return nil, nil, err
}
return scalet, resp, err
}
func (s *ScaletServiceOp) Halt(ctid int) (*Scalet, *Response, error) {
if ctid < 1 {
return nil, nil, NewArgError("scaletID", "cannot be less than 1")
}
path := fmt.Sprintf("%s/%d/stop", scaletsBasePath, ctid)
req, err := s.client.NewRequest("PATCH", path, nil)
if err != nil {
return nil, nil, err
}
scalet := &Scalet{}
resp, err := s.client.Do(req, scalet)
if err != nil {
return nil, nil, err
}
return scalet, resp, err
}
func (s *ScaletServiceOp) Start(ctid int) (*Scalet, *Response, error) {
if ctid < 1 {
return nil, nil, NewArgError("scaletID", "cannot be less than 1")
}
path := fmt.Sprintf("%s/%d/start", scaletsBasePath, ctid)
req, err := s.client.NewRequest("PATCH", path, nil)
if err != nil {
return nil, nil, err
}
scalet := &Scalet{}
resp, err := s.client.Do(req, scalet)
if err != nil {
return nil, nil, err
}
return scalet, resp, err
}
func (s *ScaletServiceOp) UpdatePlan(rplanUpdateRequest *ScaletUpdatePlanRequest) (*Scalet, *Response, error) {
if rplanUpdateRequest == nil {
return nil, nil, NewArgError("rplanUpdateRequest", "cannot be nil")
}
if rplanUpdateRequest.ID < 1 {
return nil, nil, NewArgError("rplanUpdateRequest.ID", "cannot be less than 1")
}
if rplanUpdateRequest.Rplan == "" {
return nil, nil, NewArgError("rplanUpdateRequest.rplan", "cannot be empty")
}
path := fmt.Sprintf("%s/%d/upgrade", scaletsBasePath, rplanUpdateRequest.ID)
req, err := s.client.NewRequest("POST", path, rplanUpdateRequest)
if err != nil {
return nil, nil, err
}
scalet := new(Scalet)
resp, err := s.client.Do(req, scalet)
if err != nil {
return nil, nil, err
}
return scalet, resp, err
}
func (s *ScaletServiceOp) Delete(ctid int) (*Scalet, *Response, error) {
if ctid < 1 {
return nil, nil, NewArgError("scaletID", "cannot be less than 1")
}
path := fmt.Sprintf("%s/%d", scaletsBasePath, ctid)
req, err := s.client.NewRequest("DELETE", path, nil)
if err != nil {
return nil, nil, err
}
scalet := &Scalet{}
resp, err := s.client.Do(req, scalet)
if err != nil {
return nil, nil, err
}
return scalet, resp, err
}
func (s *ScaletServiceOp) Tasks() (*[]ScaletTask, *Response, error) {
req, err := s.client.NewRequest("GET", tasksBasePath, nil)
if err != nil {
return nil, nil, err
}
tasks := &[]ScaletTask{}
resp, err := s.client.Do(req, tasks)
if err != nil {
return nil, nil, err
}
return tasks, resp, err
}
func (s *ScaletServiceOp) AddSSHKeyToScalet(appendSSHKeyRequest *SSHKeyAppendRequest) (*Scalet, *Response, error) {
if appendSSHKeyRequest == nil {
return nil, nil, NewArgError("appendSSHKeyRequest", "cannot be nil")
}
if appendSSHKeyRequest.CTID < 1 {
return nil, nil, NewArgError("appendSSHKeyRequest.CTID", "cannot be less than 1")
}
path := fmt.Sprintf("%s/scalets/%d", sshBaseUrl, appendSSHKeyRequest.CTID)
req, err := s.client.NewRequest("PATCH", path, appendSSHKeyRequest)
if err != nil {
return nil, nil, err
}
scalet := new(Scalet)
resp, err := s.client.Do(req, scalet)
if err != nil {
return nil, nil, err
}
return scalet, resp, err
}