forked from parkervcp/crocgodyl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_servers.go
372 lines (312 loc) · 9.37 KB
/
app_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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package crocgodyl
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"time"
)
type AppServer struct {
ID int `json:"id"`
ExternalID string `json:"external_id"`
UUID string `json:"uuid"`
Identifier string `json:"identifier"`
Name string `json:"name"`
Description string `json:"description"`
Status string `json:"status,omitempty"`
Suspended bool `json:"suspended"`
Limits Limits `json:"limits"`
FeatureLimits FeatureLimits `json:"feature_limits"`
User int `json:"user"`
Node int `json:"node"`
Allocation int `json:"allocation"`
Nest int `json:"nest"`
Egg int `json:"egg"`
Container struct {
StartupCommand string `json:"startup_command"`
Image string `json:"image"`
Installed int `json:"installed"`
Environment map[string]interface{} `json:"environment"`
} `json:"container"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
func (s *AppServer) BuildDescriptor() *ServerBuildDescriptor {
return &ServerBuildDescriptor{
Allocation: s.Allocation,
OOMDisabled: s.Limits.OOMDisabled,
Limits: s.Limits,
AddAllocations: []int{},
RemoveAllocations: []int{},
FeatureLimits: s.FeatureLimits,
}
}
func (s *AppServer) DetailsDescriptor() *ServerDetailsDescriptor {
return &ServerDetailsDescriptor{
ExternalID: s.ExternalID,
Name: s.Name,
User: s.User,
Description: s.Description,
}
}
func (s *AppServer) StartupDescriptor() *ServerStartupDescriptor {
return &ServerStartupDescriptor{
Startup: s.Container.StartupCommand,
Environment: s.Container.Environment,
Egg: s.Egg,
Image: s.Container.Image,
}
}
func (a *Application) GetServers() ([]*AppServer, error) {
req := a.newRequest("GET", "/servers", nil)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Data []struct {
Attributes *AppServer `json:"attributes"`
} `json:"data"`
PaginationMeta
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
servers := make([]*AppServer, 0, len(model.Data))
for _, s := range model.Data {
servers = append(servers, s.Attributes)
}
allServers := servers
for model.PaginationMeta.Meta.Pagination.CurrentPage <= model.PaginationMeta.Meta.Pagination.TotalPages {
req = a.newRequest("GET", fmt.Sprintf("/servers?page=%d", model.PaginationMeta.Meta.Pagination.CurrentPage + 1), nil)
res, err = a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err = validate(res)
if err != nil {
return nil, err
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
for _, s := range model.Data {
allServers = append(allServers, s.Attributes)
}
}
return allServers, nil
}
func (a *Application) GetServer(id int) (*AppServer, error) {
req := a.newRequest("GET", fmt.Sprintf("/servers/%d", id), nil)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Attributes AppServer `json:"attributes"`
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
return &model.Attributes, nil
}
func (a *Application) GetServerExternal(id string) (*AppServer, error) {
req := a.newRequest("GET", fmt.Sprintf("/servers/external/%s", id), nil)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Attributes AppServer `json:"attributes"`
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
return &model.Attributes, nil
}
type AllocationDescriptor struct {
Default int `json:"default"`
Additional []int `json:"additional,omitempty"`
}
type DeployDescriptor struct {
Locations []int `json:"locations"`
DedicatedIP bool `json:"dedicated_ip"`
PortRange []string `json:"port_range"`
}
type CreateServerDescriptor struct {
ExternalID string `json:"external_id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
User int `json:"user"`
Egg int `json:"egg"`
DockerImage string `json:"docker_image"`
Startup string `json:"startup"`
Environment map[string]interface{} `json:"environment"`
SkipScripts bool `json:"skip_scripts,omitempty"`
OOMDisabled bool `json:"oom_disabled"`
Limits *Limits `json:"limits"`
FeatureLimtis FeatureLimits `json:"feature_limits"`
Allocation *AllocationDescriptor `json:"allocation,omitempty"`
Deploy *DeployDescriptor `json:"deploy,omitempty"`
StartOnCompletion bool `json:"start_on_completion,omitempty"`
}
func (a *Application) CreateServer(fields CreateServerDescriptor) (*AppServer, error) {
if fields.Allocation == nil && fields.Deploy == nil {
return nil, errors.New("the allocation object or deploy object must be specified")
}
data, _ := json.Marshal(fields)
body := bytes.Buffer{}
body.Write(data)
req := a.newRequest("POST", "/servers", &body)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Attributes AppServer `json:"attributes"`
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
return &model.Attributes, nil
}
type ServerBuildDescriptor struct {
Allocation int `json:"allocation,omitempty"`
OOMDisabled bool `json:"oom_disabled,omitempty"`
Limits Limits `json:"limits,omitempty"`
AddAllocations []int `json:"add_allocations,omitempty"`
RemoveAllocations []int `json:"remove_allocations,omitempty"`
FeatureLimits FeatureLimits `json:"feature_limits,omitempty"`
}
func (a *Application) UpdateServerBuild(id int, fields ServerBuildDescriptor) (*AppServer, error) {
data, _ := json.Marshal(fields)
if len(data) == 2 {
return nil, errors.New("no build fields specified")
}
body := bytes.Buffer{}
body.Write(data)
req := a.newRequest("PATCH", fmt.Sprintf("/servers/%d/build", id), &body)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Attributes AppServer `json:"attributes"`
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
return &model.Attributes, nil
}
type ServerDetailsDescriptor struct {
ExternalID string `json:"external_id,omitempty"`
Name string `json:"name,omitempty"`
User int `json:"user,omitempty"`
Description string `json:"description,omitempty"`
}
func (a *Application) UpdateServerDetails(id int, fields ServerDetailsDescriptor) (*AppServer, error) {
data, _ := json.Marshal(fields)
if len(data) == 2 {
return nil, errors.New("no details fields specified")
}
body := bytes.Buffer{}
body.Write(data)
req := a.newRequest("PATCH", fmt.Sprintf("/servers/%d/details", id), &body)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Attributes AppServer `json:"attributes"`
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
return &model.Attributes, nil
}
type ServerStartupDescriptor struct {
Startup string `json:"startup"`
Environment map[string]interface{} `json:"environment"`
Egg int `json:"egg,omitempty"`
Image string `json:"image"`
SkipScripts bool `json:"skip_scripts"`
}
func (a *Application) UpdateServerStartup(id int, fields ServerStartupDescriptor) (*AppServer, error) {
data, _ := json.Marshal(fields)
if len(data) == 2 {
return nil, errors.New("no startup fields specified")
}
body := bytes.Buffer{}
body.Write(data)
req := a.newRequest("PATCH", fmt.Sprintf("/servers/%d/startup", id), &body)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Attributes AppServer `json:"attributes"`
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
return &model.Attributes, nil
}
func (a *Application) SuspendServer(id int) error {
req := a.newRequest("POST", fmt.Sprintf("/servers/%d/suspend", id), nil)
res, err := a.Http.Do(req)
if err != nil {
return err
}
_, err = validate(res)
return err
}
func (a *Application) UnsuspendServer(id int) error {
req := a.newRequest("POST", fmt.Sprintf("/servers/%d/unsuspend", id), nil)
res, err := a.Http.Do(req)
if err != nil {
return err
}
_, err = validate(res)
return err
}
func (a *Application) DeleteServer(id int, force bool) error {
url := fmt.Sprintf("/servers/%d", id)
if force {
url += "/force"
}
req := a.newRequest("DELETE", url, nil)
res, err := a.Http.Do(req)
if err != nil {
return err
}
_, err = validate(res)
return err
}