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