Completed
Push — master ( 6aea5c...83ffe7 )
by Yassir
01:54
created

Droplet::waitForActive()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 2
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) {
167
                $dropletEntity = new DropletEntity($droplet);
168
                if ($wait) {
0 ignored issues
show
Bug introduced by
The variable $wait does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
169
                  return $this->waitForActive($dropletEntity, $waitTimeout);
0 ignored issues
show
Bug introduced by
The variable $waitTimeout does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
170
                }
171
                return $dropletEntity;
172
            }, $droplet->droplets);
173
        }
174
175
        $dropletEntity = new DropletEntity($droplet->droplet);
176
        if ($wait) {
177
          return $this->waitForActive($dropletEntity, $waitTimeout);
178
        }
179
        return $dropletEntity;
180
    }
181
182
    /**
183
     * @param int $id
184
     *
185
     * @throws HttpException
186
     */
187
    public function delete($id)
188
    {
189
        $this->adapter->delete(sprintf('%s/droplets/%d', $this->endpoint, $id));
190
    }
191
192
    /**
193
     * @param int $id
194
     *
195
     * @throws HttpException
196
     *
197
     * @return KernelEntity[]
198
     */
199
    public function getAvailableKernels($id)
200
    {
201
        $kernels = $this->adapter->get(sprintf('%s/droplets/%d/kernels', $this->endpoint, $id));
202
203
        $kernels = json_decode($kernels);
204
205
        $this->meta = $this->extractMeta($kernels);
206
207
        return array_map(function ($kernel) {
208
            return new KernelEntity($kernel);
209
        }, $kernels->kernels);
210
    }
211
212
    /**
213
     * @param int $id
214
     *
215
     * @return ImageEntity[]
216
     */
217
    public function getSnapshots($id)
218
    {
219
        $snapshots = $this->adapter->get(sprintf('%s/droplets/%d/snapshots?per_page=%d', $this->endpoint, $id, 200));
220
221
        $snapshots = json_decode($snapshots);
222
223
        $this->meta = $this->extractMeta($snapshots);
224
225
        return array_map(function ($snapshot) {
226
            $snapshot = new ImageEntity($snapshot);
227
228
            return $snapshot;
229
        }, $snapshots->snapshots);
230
    }
231
232
    /**
233
     * @param int $id
234
     *
235
     * @return ImageEntity[]
236
     */
237
    public function getBackups($id)
238
    {
239
        $backups = $this->adapter->get(sprintf('%s/droplets/%d/backups?per_page=%d', $this->endpoint, $id, 200));
240
241
        $backups = json_decode($backups);
242
243
        $this->meta = $this->extractMeta($backups);
244
245
        return array_map(function ($backup) {
246
            return new ImageEntity($backup);
247
        }, $backups->backups);
248
    }
249
250
    /**
251
     * @param int $id
252
     *
253
     * @return ActionEntity[]
254
     */
255
    public function getActions($id)
256
    {
257
        $actions = $this->adapter->get(sprintf('%s/droplets/%d/actions?per_page=%d', $this->endpoint, $id, 200));
258
259
        $actions = json_decode($actions);
260
261
        $this->meta = $this->extractMeta($actions);
262
263
        return array_map(function ($action) {
264
            return new ActionEntity($action);
265
        }, $actions->actions);
266
    }
267
268
    /**
269
     * @param int $id
270
     * @param int $actionId
271
     *
272
     * @return ActionEntity
273
     */
274
    public function getActionById($id, $actionId)
275
    {
276
        $action = $this->adapter->get(sprintf('%s/droplets/%d/actions/%d', $this->endpoint, $id, $actionId));
277
278
        $action = json_decode($action);
279
280
        return new ActionEntity($action->action);
281
    }
282
283
    /**
284
     * @param int $id
285
     *
286
     * @throws HttpException
287
     *
288
     * @return ActionEntity
289
     */
290
    public function reboot($id)
291
    {
292
        return $this->executeAction($id, ['type' => 'reboot']);
293
    }
294
295
    /**
296
     * @param int $id
297
     *
298
     * @throws HttpException
299
     *
300
     * @return ActionEntity
301
     */
302
    public function powerCycle($id)
303
    {
304
        return $this->executeAction($id, ['type' => 'power_cycle']);
305
    }
306
307
    /**
308
     * @param int $id
309
     *
310
     * @throws HttpException
311
     *
312
     * @return ActionEntity
313
     */
314
    public function shutdown($id)
315
    {
316
        return $this->executeAction($id, ['type' => 'shutdown']);
317
    }
318
319
    /**
320
     * @param int $id
321
     *
322
     * @throws HttpException
323
     *
324
     * @return ActionEntity
325
     */
326
    public function powerOff($id)
327
    {
328
        return $this->executeAction($id, ['type' => 'power_off']);
329
    }
330
331
    /**
332
     * @param int $id
333
     *
334
     * @throws HttpException
335
     *
336
     * @return ActionEntity
337
     */
338
    public function powerOn($id)
339
    {
340
        return $this->executeAction($id, ['type' => 'power_on']);
341
    }
342
343
    /**
344
     * @param int $id
345
     *
346
     * @throws HttpException
347
     *
348
     * @return ActionEntity
349
     */
350
    public function passwordReset($id)
351
    {
352
        return $this->executeAction($id, ['type' => 'password_reset']);
353
    }
354
355
    /**
356
     * @param int    $id
357
     * @param string $size
358
     * @param bool   $disk
359
     *
360
     * @throws HttpException
361
     *
362
     * @return ActionEntity
363
     */
364
    public function resize($id, $size, $disk = true)
365
    {
366
        return $this->executeAction($id, ['type' => 'resize', 'size' => $size, 'disk' => $disk ? 'true' : 'false']);
367
    }
368
369
    /**
370
     * @param int $id
371
     * @param int $image
372
     *
373
     * @throws HttpException
374
     *
375
     * @return ActionEntity
376
     */
377
    public function restore($id, $image)
378
    {
379
        return $this->executeAction($id, ['type' => 'restore', 'image' => $image]);
380
    }
381
382
    /**
383
     * @param int        $id
384
     * @param int|string $image
385
     *
386
     * @throws HttpException
387
     *
388
     * @return ActionEntity
389
     */
390
    public function rebuild($id, $image)
391
    {
392
        return $this->executeAction($id, ['type' => 'rebuild', 'image' => $image]);
393
    }
394
395
    /**
396
     * @param int    $id
397
     * @param string $name
398
     *
399
     * @throws HttpException
400
     *
401
     * @return ActionEntity
402
     */
403
    public function rename($id, $name)
404
    {
405
        return $this->executeAction($id, ['type' => 'rename', 'name' => $name]);
406
    }
407
408
    /**
409
     * @param int $id
410
     * @param int $kernel
411
     *
412
     * @throws HttpException
413
     *
414
     * @return ActionEntity
415
     */
416
    public function changeKernel($id, $kernel)
417
    {
418
        return $this->executeAction($id, ['type' => 'change_kernel', 'kernel' => $kernel]);
419
    }
420
421
    /**
422
     * @param int $id
423
     *
424
     * @throws HttpException
425
     *
426
     * @return ActionEntity
427
     */
428
    public function enableIpv6($id)
429
    {
430
        return $this->executeAction($id, ['type' => 'enable_ipv6']);
431
    }
432
433
    /**
434
     * @param int $id
435
     *
436
     * @throws HttpException
437
     *
438
     * @return ActionEntity
439
     */
440
    public function enableBackups($id)
441
    {
442
        return $this->executeAction($id, ['type' => 'enable_backups']);
443
    }
444
445
    /**
446
     * @param int $id
447
     *
448
     * @throws HttpException
449
     *
450
     * @return ActionEntity
451
     */
452
    public function disableBackups($id)
453
    {
454
        return $this->executeAction($id, ['type' => 'disable_backups']);
455
    }
456
457
    /**
458
     * @param int $id
459
     *
460
     * @throws HttpException
461
     *
462
     * @return ActionEntity
463
     */
464
    public function enablePrivateNetworking($id)
465
    {
466
        return $this->executeAction($id, ['type' => 'enable_private_networking']);
467
    }
468
469
    /**
470
     * @param int    $id
471
     * @param string $name
472
     *
473
     * @throws HttpException
474
     *
475
     * @return ActionEntity
476
     */
477
    public function snapshot($id, $name)
478
    {
479
        return $this->executeAction($id, ['type' => 'snapshot', 'name' => $name]);
480
    }
481
482
    /**
483
     * @param int   $id
484
     * @param array $options
485
     *
486
     * @throws HttpException
487
     *
488
     * @return ActionEntity
489
     */
490
    private function executeAction($id, array $options)
491
    {
492
        $action = $this->adapter->post(sprintf('%s/droplets/%d/actions', $this->endpoint, $id), $options);
493
494
        $action = json_decode($action);
495
496
        return new ActionEntity($action->action);
497
    }
498
499
    /**
500
     * @param DropletEntity $droplet
501
     * @param int           $waitTimeout
502
     *
503
     * @throws HttpException
504
     *
505
     * @return DropletEntity|null
506
     */
507
    public function waitForActive($droplet, $waitTimeout)
508
    {
509
      $endTime = time() + $waitTimeout;
510
      while (time() < $endTime) {
511
        sleep(min(20, $endTime - time()));
512
        $droplet = $this->getById($droplet->id);
513
        if ($droplet->status == 'active') {
514
          return $droplet;
515
        }
516
      }
517
      return $droplet;
518
    }
519
}
520