1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the DigitalOceanV2 library. |
5
|
|
|
* |
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DigitalOceanV2\Api; |
13
|
|
|
|
14
|
|
|
use DigitalOceanV2\Entity\Action as ActionEntity; |
15
|
|
|
use DigitalOceanV2\Entity\Droplet as DropletEntity; |
16
|
|
|
use DigitalOceanV2\Entity\Image as ImageEntity; |
17
|
|
|
use DigitalOceanV2\Entity\Kernel as KernelEntity; |
18
|
|
|
use DigitalOceanV2\Entity\Upgrade as UpgradeEntity; |
19
|
|
|
use DigitalOceanV2\Exception\HttpException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Yassir Hannoun <[email protected]> |
23
|
|
|
* @author Graham Campbell <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Droplet extends AbstractApi |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param int $per_page |
29
|
|
|
* @param int $page |
30
|
|
|
* @param string|null $tag |
31
|
|
|
* |
32
|
|
|
* @return DropletEntity[] |
33
|
|
|
*/ |
34
|
|
|
public function getAll($per_page = 200, $page = 1, $tag = null) |
35
|
|
|
{ |
36
|
|
|
$url = sprintf('%s/droplets?per_page=%d&page=%d', $this->endpoint, $per_page, $page); |
37
|
|
|
|
38
|
|
|
if (null !== $tag) { |
39
|
|
|
$url .= '&tag_name='.$tag; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$droplets = json_decode($this->adapter->get($url)); |
43
|
|
|
|
44
|
|
|
$this->extractMeta($droplets); |
45
|
|
|
|
46
|
|
|
return array_map(function ($droplet) { |
47
|
|
|
return new DropletEntity($droplet); |
48
|
|
|
}, $droplets->droplets); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param int $id |
53
|
|
|
* |
54
|
|
|
* @return DropletEntity[] |
55
|
|
|
*/ |
56
|
|
|
public function getNeighborsById($id) |
57
|
|
|
{ |
58
|
|
|
$droplets = $this->adapter->get(sprintf('%s/droplets/%d/neighbors', $this->endpoint, $id)); |
59
|
|
|
|
60
|
|
|
$droplets = json_decode($droplets); |
61
|
|
|
|
62
|
|
|
return array_map(function ($droplet) { |
63
|
|
|
return new DropletEntity($droplet); |
64
|
|
|
}, $droplets->droplets); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return DropletEntity[] |
69
|
|
|
*/ |
70
|
|
|
public function getAllNeighbors() |
71
|
|
|
{ |
72
|
|
|
$neighbors = $this->adapter->get(sprintf('%s/reports/droplet_neighbors', $this->endpoint)); |
73
|
|
|
|
74
|
|
|
$neighbors = json_decode($neighbors); |
75
|
|
|
|
76
|
|
|
return array_map(function ($neighbor) { |
77
|
|
|
return new DropletEntity($neighbor); |
78
|
|
|
}, $neighbors->neighbors); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return UpgradeEntity[] |
83
|
|
|
*/ |
84
|
|
|
public function getUpgrades() |
85
|
|
|
{ |
86
|
|
|
$upgrades = $this->adapter->get(sprintf('%s/droplet_upgrades', $this->endpoint)); |
87
|
|
|
|
88
|
|
|
$upgrades = json_decode($upgrades); |
89
|
|
|
|
90
|
|
|
return array_map(function ($upgrade) { |
91
|
|
|
return new UpgradeEntity($upgrade); |
92
|
|
|
}, $upgrades); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param int $id |
97
|
|
|
* |
98
|
|
|
* @throws HttpException |
99
|
|
|
* |
100
|
|
|
* @return DropletEntity |
101
|
|
|
*/ |
102
|
|
|
public function getById($id) |
103
|
|
|
{ |
104
|
|
|
$droplet = $this->adapter->get(sprintf('%s/droplets/%d', $this->endpoint, $id)); |
105
|
|
|
|
106
|
|
|
$droplet = json_decode($droplet); |
107
|
|
|
|
108
|
|
|
return new DropletEntity($droplet->droplet); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array|string $names |
113
|
|
|
* @param string $region |
114
|
|
|
* @param string $size |
115
|
|
|
* @param string|int $image |
116
|
|
|
* @param bool $backups |
117
|
|
|
* @param bool $ipv6 |
118
|
|
|
* @param bool $privateNetworking |
119
|
|
|
* @param int[] $sshKeys |
120
|
|
|
* @param string $userData |
121
|
|
|
* @param bool $monitoring |
122
|
|
|
* @param array $volumes |
123
|
|
|
* @param array $tags |
124
|
|
|
* @param bool $wait |
125
|
|
|
* @param int $waitTimeout |
126
|
|
|
* |
127
|
|
|
* @throws HttpException |
128
|
|
|
* |
129
|
|
|
* @return DropletEntity|null |
130
|
|
|
*/ |
131
|
|
|
public function create($names, $region, $size, $image, $backups = false, $ipv6 = false, $privateNetworking = false, array $sshKeys = [], $userData = '', $monitoring = true, array $volumes = [], array $tags = [], $wait = false, $waitTimeout = 300) |
132
|
|
|
{ |
133
|
|
|
$data = is_array($names) ? ['names' => $names] : ['name' => $names]; |
134
|
|
|
|
135
|
|
|
$data = array_merge($data, [ |
136
|
|
|
'region' => $region, |
137
|
|
|
'size' => $size, |
138
|
|
|
'image' => $image, |
139
|
|
|
'backups' => $backups ? 'true' : 'false', |
140
|
|
|
'ipv6' => $ipv6 ? 'true' : 'false', |
141
|
|
|
'private_networking' => $privateNetworking ? 'true' : 'false', |
142
|
|
|
'monitoring' => $monitoring ? 'true' : 'false', |
143
|
|
|
]); |
144
|
|
|
|
145
|
|
|
if (0 < count($sshKeys)) { |
146
|
|
|
$data['ssh_keys'] = $sshKeys; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (!empty($userData)) { |
150
|
|
|
$data['user_data'] = $userData; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (0 < count($volumes)) { |
154
|
|
|
$data['volumes'] = $volumes; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (0 < count($tags)) { |
158
|
|
|
$data['tags'] = $tags; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$droplet = $this->adapter->post(sprintf('%s/droplets', $this->endpoint), $data); |
162
|
|
|
|
163
|
|
|
$droplet = json_decode($droplet); |
164
|
|
|
|
165
|
|
|
if (is_array($names)) { |
166
|
|
|
return array_map(function ($droplet) use ($wait, $waitTimeout) { |
167
|
|
|
$dropletEntity = new DropletEntity($droplet); |
168
|
|
|
if ($wait) { |
169
|
|
|
return $this->waitForActive($dropletEntity, $waitTimeout); |
170
|
|
|
} |
171
|
|
|
return $dropletEntity; |
172
|
|
|
}, $droplet->droplets); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$dropletEntity = new DropletEntity($droplet->droplet); |
176
|
|
|
|
177
|
|
|
if ($wait) { |
178
|
|
|
return $this->waitForActive($dropletEntity, $waitTimeout); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $dropletEntity; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param int $id |
186
|
|
|
* |
187
|
|
|
* @throws HttpException |
188
|
|
|
*/ |
189
|
|
|
public function delete($id) |
190
|
|
|
{ |
191
|
|
|
$this->adapter->delete(sprintf('%s/droplets/%d', $this->endpoint, $id)); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param int $id |
196
|
|
|
* |
197
|
|
|
* @throws HttpException |
198
|
|
|
* |
199
|
|
|
* @return KernelEntity[] |
200
|
|
|
*/ |
201
|
|
|
public function getAvailableKernels($id) |
202
|
|
|
{ |
203
|
|
|
$kernels = $this->adapter->get(sprintf('%s/droplets/%d/kernels', $this->endpoint, $id)); |
204
|
|
|
|
205
|
|
|
$kernels = json_decode($kernels); |
206
|
|
|
|
207
|
|
|
$this->meta = $this->extractMeta($kernels); |
208
|
|
|
|
209
|
|
|
return array_map(function ($kernel) { |
210
|
|
|
return new KernelEntity($kernel); |
211
|
|
|
}, $kernels->kernels); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param int $id |
216
|
|
|
* |
217
|
|
|
* @return ImageEntity[] |
218
|
|
|
*/ |
219
|
|
|
public function getSnapshots($id) |
220
|
|
|
{ |
221
|
|
|
$snapshots = $this->adapter->get(sprintf('%s/droplets/%d/snapshots?per_page=%d', $this->endpoint, $id, 200)); |
222
|
|
|
|
223
|
|
|
$snapshots = json_decode($snapshots); |
224
|
|
|
|
225
|
|
|
$this->meta = $this->extractMeta($snapshots); |
226
|
|
|
|
227
|
|
|
return array_map(function ($snapshot) { |
228
|
|
|
$snapshot = new ImageEntity($snapshot); |
229
|
|
|
|
230
|
|
|
return $snapshot; |
231
|
|
|
}, $snapshots->snapshots); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param int $id |
236
|
|
|
* |
237
|
|
|
* @return ImageEntity[] |
238
|
|
|
*/ |
239
|
|
|
public function getBackups($id) |
240
|
|
|
{ |
241
|
|
|
$backups = $this->adapter->get(sprintf('%s/droplets/%d/backups?per_page=%d', $this->endpoint, $id, 200)); |
242
|
|
|
|
243
|
|
|
$backups = json_decode($backups); |
244
|
|
|
|
245
|
|
|
$this->meta = $this->extractMeta($backups); |
246
|
|
|
|
247
|
|
|
return array_map(function ($backup) { |
248
|
|
|
return new ImageEntity($backup); |
249
|
|
|
}, $backups->backups); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param int $id |
254
|
|
|
* |
255
|
|
|
* @return ActionEntity[] |
256
|
|
|
*/ |
257
|
|
|
public function getActions($id) |
258
|
|
|
{ |
259
|
|
|
$actions = $this->adapter->get(sprintf('%s/droplets/%d/actions?per_page=%d', $this->endpoint, $id, 200)); |
260
|
|
|
|
261
|
|
|
$actions = json_decode($actions); |
262
|
|
|
|
263
|
|
|
$this->meta = $this->extractMeta($actions); |
264
|
|
|
|
265
|
|
|
return array_map(function ($action) { |
266
|
|
|
return new ActionEntity($action); |
267
|
|
|
}, $actions->actions); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param int $id |
272
|
|
|
* @param int $actionId |
273
|
|
|
* |
274
|
|
|
* @return ActionEntity |
275
|
|
|
*/ |
276
|
|
|
public function getActionById($id, $actionId) |
277
|
|
|
{ |
278
|
|
|
$action = $this->adapter->get(sprintf('%s/droplets/%d/actions/%d', $this->endpoint, $id, $actionId)); |
279
|
|
|
|
280
|
|
|
$action = json_decode($action); |
281
|
|
|
|
282
|
|
|
return new ActionEntity($action->action); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param int $id |
287
|
|
|
* |
288
|
|
|
* @throws HttpException |
289
|
|
|
* |
290
|
|
|
* @return ActionEntity |
291
|
|
|
*/ |
292
|
|
|
public function reboot($id) |
293
|
|
|
{ |
294
|
|
|
return $this->executeAction($id, ['type' => 'reboot']); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param int $id |
299
|
|
|
* |
300
|
|
|
* @throws HttpException |
301
|
|
|
* |
302
|
|
|
* @return ActionEntity |
303
|
|
|
*/ |
304
|
|
|
public function powerCycle($id) |
305
|
|
|
{ |
306
|
|
|
return $this->executeAction($id, ['type' => 'power_cycle']); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param int $id |
311
|
|
|
* |
312
|
|
|
* @throws HttpException |
313
|
|
|
* |
314
|
|
|
* @return ActionEntity |
315
|
|
|
*/ |
316
|
|
|
public function shutdown($id) |
317
|
|
|
{ |
318
|
|
|
return $this->executeAction($id, ['type' => 'shutdown']); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @param int $id |
323
|
|
|
* |
324
|
|
|
* @throws HttpException |
325
|
|
|
* |
326
|
|
|
* @return ActionEntity |
327
|
|
|
*/ |
328
|
|
|
public function powerOff($id) |
329
|
|
|
{ |
330
|
|
|
return $this->executeAction($id, ['type' => 'power_off']); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param int $id |
335
|
|
|
* |
336
|
|
|
* @throws HttpException |
337
|
|
|
* |
338
|
|
|
* @return ActionEntity |
339
|
|
|
*/ |
340
|
|
|
public function powerOn($id) |
341
|
|
|
{ |
342
|
|
|
return $this->executeAction($id, ['type' => 'power_on']); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @param int $id |
347
|
|
|
* |
348
|
|
|
* @throws HttpException |
349
|
|
|
* |
350
|
|
|
* @return ActionEntity |
351
|
|
|
*/ |
352
|
|
|
public function passwordReset($id) |
353
|
|
|
{ |
354
|
|
|
return $this->executeAction($id, ['type' => 'password_reset']); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @param int $id |
359
|
|
|
* @param string $size |
360
|
|
|
* @param bool $disk |
361
|
|
|
* |
362
|
|
|
* @throws HttpException |
363
|
|
|
* |
364
|
|
|
* @return ActionEntity |
365
|
|
|
*/ |
366
|
|
|
public function resize($id, $size, $disk = true) |
367
|
|
|
{ |
368
|
|
|
return $this->executeAction($id, ['type' => 'resize', 'size' => $size, 'disk' => $disk ? 'true' : 'false']); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @param int $id |
373
|
|
|
* @param int $image |
374
|
|
|
* |
375
|
|
|
* @throws HttpException |
376
|
|
|
* |
377
|
|
|
* @return ActionEntity |
378
|
|
|
*/ |
379
|
|
|
public function restore($id, $image) |
380
|
|
|
{ |
381
|
|
|
return $this->executeAction($id, ['type' => 'restore', 'image' => $image]); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @param int $id |
386
|
|
|
* @param int|string $image |
387
|
|
|
* |
388
|
|
|
* @throws HttpException |
389
|
|
|
* |
390
|
|
|
* @return ActionEntity |
391
|
|
|
*/ |
392
|
|
|
public function rebuild($id, $image) |
393
|
|
|
{ |
394
|
|
|
return $this->executeAction($id, ['type' => 'rebuild', 'image' => $image]); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @param int $id |
399
|
|
|
* @param string $name |
400
|
|
|
* |
401
|
|
|
* @throws HttpException |
402
|
|
|
* |
403
|
|
|
* @return ActionEntity |
404
|
|
|
*/ |
405
|
|
|
public function rename($id, $name) |
406
|
|
|
{ |
407
|
|
|
return $this->executeAction($id, ['type' => 'rename', 'name' => $name]); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @param int $id |
412
|
|
|
* @param int $kernel |
413
|
|
|
* |
414
|
|
|
* @throws HttpException |
415
|
|
|
* |
416
|
|
|
* @return ActionEntity |
417
|
|
|
*/ |
418
|
|
|
public function changeKernel($id, $kernel) |
419
|
|
|
{ |
420
|
|
|
return $this->executeAction($id, ['type' => 'change_kernel', 'kernel' => $kernel]); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @param int $id |
425
|
|
|
* |
426
|
|
|
* @throws HttpException |
427
|
|
|
* |
428
|
|
|
* @return ActionEntity |
429
|
|
|
*/ |
430
|
|
|
public function enableIpv6($id) |
431
|
|
|
{ |
432
|
|
|
return $this->executeAction($id, ['type' => 'enable_ipv6']); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* @param int $id |
437
|
|
|
* |
438
|
|
|
* @throws HttpException |
439
|
|
|
* |
440
|
|
|
* @return ActionEntity |
441
|
|
|
*/ |
442
|
|
|
public function enableBackups($id) |
443
|
|
|
{ |
444
|
|
|
return $this->executeAction($id, ['type' => 'enable_backups']); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* @param int $id |
449
|
|
|
* |
450
|
|
|
* @throws HttpException |
451
|
|
|
* |
452
|
|
|
* @return ActionEntity |
453
|
|
|
*/ |
454
|
|
|
public function disableBackups($id) |
455
|
|
|
{ |
456
|
|
|
return $this->executeAction($id, ['type' => 'disable_backups']); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* @param int $id |
461
|
|
|
* |
462
|
|
|
* @throws HttpException |
463
|
|
|
* |
464
|
|
|
* @return ActionEntity |
465
|
|
|
*/ |
466
|
|
|
public function enablePrivateNetworking($id) |
467
|
|
|
{ |
468
|
|
|
return $this->executeAction($id, ['type' => 'enable_private_networking']); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* @param int $id |
473
|
|
|
* @param string $name |
474
|
|
|
* |
475
|
|
|
* @throws HttpException |
476
|
|
|
* |
477
|
|
|
* @return ActionEntity |
478
|
|
|
*/ |
479
|
|
|
public function snapshot($id, $name) |
480
|
|
|
{ |
481
|
|
|
return $this->executeAction($id, ['type' => 'snapshot', 'name' => $name]); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* @param int $id |
486
|
|
|
* @param array $options |
487
|
|
|
* |
488
|
|
|
* @throws HttpException |
489
|
|
|
* |
490
|
|
|
* @return ActionEntity |
491
|
|
|
*/ |
492
|
|
|
private function executeAction($id, array $options) |
493
|
|
|
{ |
494
|
|
|
$action = $this->adapter->post(sprintf('%s/droplets/%d/actions', $this->endpoint, $id), $options); |
495
|
|
|
|
496
|
|
|
$action = json_decode($action); |
497
|
|
|
|
498
|
|
|
return new ActionEntity($action->action); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* @param DropletEntity $droplet |
503
|
|
|
* @param int $waitTimeout |
504
|
|
|
* |
505
|
|
|
* @throws HttpException |
506
|
|
|
* |
507
|
|
|
* @return DropletEntity|null |
508
|
|
|
*/ |
509
|
|
|
public function waitForActive($droplet, $waitTimeout) |
510
|
|
|
{ |
511
|
|
|
$endTime = time() + $waitTimeout; |
512
|
|
|
|
513
|
|
|
while (time() < $endTime) { |
514
|
|
|
sleep(min(20, $endTime - time())); |
515
|
|
|
$droplet = $this->getById($droplet->id); |
516
|
|
|
if ($droplet->status === 'active') { |
517
|
|
|
return $droplet; |
518
|
|
|
} |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
return $droplet; |
522
|
|
|
} |
523
|
|
|
} |
524
|
|
|
|