-
-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathDroplet.php
More file actions
357 lines (304 loc) · 9.1 KB
/
Droplet.php
File metadata and controls
357 lines (304 loc) · 9.1 KB
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
<?php
declare(strict_types=1);
/*
* This file is part of the DigitalOcean API library.
*
* (c) Antoine Kirk <contact@sbin.dk>
* (c) Graham Campbell <hello@gjcampbell.co.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DigitalOceanV2\Api;
use DigitalOceanV2\Entity\Action as ActionEntity;
use DigitalOceanV2\Entity\Droplet as DropletEntity;
use DigitalOceanV2\Entity\Image as ImageEntity;
use DigitalOceanV2\Entity\Kernel as KernelEntity;
use DigitalOceanV2\Exception\ExceptionInterface;
/**
* @author Yassir Hannoun <yassir.hannoun@gmail.com>
* @author Graham Campbell <hello@gjcampbell.co.uk>
*/
class Droplet extends AbstractApi
{
/**
* @throws ExceptionInterface
*
* @return DropletEntity[]
*/
public function getAll(?string $tag = null): array
{
$droplets = $this->get('droplets', null === $tag ? [] : ['tag_name' => $tag]);
return \array_map(function ($droplet) {
return new DropletEntity($droplet);
}, $droplets->droplets);
}
/**
* @throws ExceptionInterface
*
* @return DropletEntity[]
*/
public function getNeighborsById(int $id): array
{
$droplets = $this->get(\sprintf('droplets/%d/neighbors', $id));
return \array_map(function ($droplet) {
return new DropletEntity($droplet);
}, $droplets->droplets);
}
/**
* @throws ExceptionInterface
*
* @return DropletEntity[]
*/
public function getAllNeighbors(): array
{
$neighbors = $this->get('reports/droplet_neighbors');
return \array_map(function ($neighbor) {
return new DropletEntity($neighbor);
}, $neighbors->neighbors);
}
/**
* @throws ExceptionInterface
*/
public function getById(int $id): DropletEntity
{
$droplet = $this->get(\sprintf('droplets/%d', $id));
return new DropletEntity($droplet->droplet);
}
/**
* @param int[]|string[] $sshKeys
*
* @throws ExceptionInterface
*
* @return DropletEntity|DropletEntity[]|null
*/
public function create(array|string $names, string $region, string $size, string|int $image, bool $backups = false, bool $ipv6 = false, string|bool $vpcUuid = false, array $sshKeys = [], string $userData = '', bool $monitoring = true, array $volumes = [], array $tags = [], bool $disableAgent = false): DropletEntity|array|null
{
$data = \is_array($names) ? ['names' => $names] : ['name' => $names];
$data = \array_merge($data, [
'region' => $region,
'size' => $size,
'image' => $image,
'backups' => $backups ? 'true' : 'false',
'ipv6' => $ipv6 ? 'true' : 'false',
'monitoring' => $monitoring ? 'true' : 'false',
]);
if ($disableAgent) {
$data['with_droplet_agent'] = 'false';
}
if (0 < \count($sshKeys)) {
$data['ssh_keys'] = $sshKeys;
}
if ('' !== $userData) {
$data['user_data'] = $userData;
}
if (\is_bool($vpcUuid)) {
$data['private_networking'] = $vpcUuid ? 'true' : 'false';
} elseif ('' !== $vpcUuid) {
$data['vpc_uuid'] = $vpcUuid;
}
if (0 < \count($volumes)) {
$data['volumes'] = $volumes;
}
if (0 < \count($tags)) {
$data['tags'] = $tags;
}
$droplet = $this->post('droplets', $data);
if (\is_array($names)) {
return \array_map(function ($droplet) {
return new DropletEntity($droplet);
}, $droplet->droplets);
}
return new DropletEntity($droplet->droplet);
}
/**
* @throws ExceptionInterface
*/
public function remove(int $id): void
{
$this->delete(\sprintf('droplets/%d', $id));
}
/**
* @throws ExceptionInterface
*/
public function removeTagged(string $tag): void
{
$this->delete('droplets', [], [], ['tag_name' => $tag]);
}
/**
* @throws ExceptionInterface
*
* @return KernelEntity[]
*/
public function getAvailableKernels(int $id): array
{
$kernels = $this->get(\sprintf('droplets/%d/kernels', $id));
return \array_map(function ($kernel) {
return new KernelEntity($kernel);
}, $kernels->kernels);
}
/**
* @throws ExceptionInterface
*
* @return ImageEntity[]
*/
public function getSnapshots(int $id): array
{
$snapshots = $this->get(\sprintf('droplets/%d/snapshots', $id));
return \array_map(function ($snapshot) {
return new ImageEntity($snapshot);
}, $snapshots->snapshots);
}
/**
* @throws ExceptionInterface
*
* @return ImageEntity[]
*/
public function getBackups(int $id): array
{
$backups = $this->get(\sprintf('droplets/%d/backups', $id));
return \array_map(function ($backup) {
return new ImageEntity($backup);
}, $backups->backups);
}
/**
* @throws ExceptionInterface
*
* @return ActionEntity[]
*/
public function getActions(int $id): array
{
$actions = $this->get(\sprintf('droplets/%d/actions', $id));
return \array_map(function ($action) {
return new ActionEntity($action);
}, $actions->actions);
}
/**
* @throws ExceptionInterface
*/
public function getActionById(int $id, int $actionId): ActionEntity
{
$action = $this->get(\sprintf('droplets/%d/actions/%d', $id, $actionId));
return new ActionEntity($action->action);
}
/**
* @throws ExceptionInterface
*/
public function reboot(int $id): ActionEntity
{
return $this->executeAction($id, ['type' => 'reboot']);
}
/**
* @throws ExceptionInterface
*/
public function powerCycle(int $id): ActionEntity
{
return $this->executeAction($id, ['type' => 'power_cycle']);
}
/**
* @throws ExceptionInterface
*/
public function shutdown(int $id): ActionEntity
{
return $this->executeAction($id, ['type' => 'shutdown']);
}
/**
* @throws ExceptionInterface
*/
public function powerOff(int $id): ActionEntity
{
return $this->executeAction($id, ['type' => 'power_off']);
}
/**
* @throws ExceptionInterface
*/
public function powerOn(int $id): ActionEntity
{
return $this->executeAction($id, ['type' => 'power_on']);
}
/**
* @throws ExceptionInterface
*/
public function passwordReset(int $id): ActionEntity
{
return $this->executeAction($id, ['type' => 'password_reset']);
}
/**
* @throws ExceptionInterface
*/
public function resize(int $id, string $size, bool $disk = true): ActionEntity
{
return $this->executeAction($id, ['type' => 'resize', 'size' => $size, 'disk' => $disk ? 'true' : 'false']);
}
/**
* @throws ExceptionInterface
*/
public function restore(int $id, int $image): ActionEntity
{
return $this->executeAction($id, ['type' => 'restore', 'image' => $image]);
}
/**
* @throws ExceptionInterface
*/
public function rebuild(int $id, int|string $image): ActionEntity
{
return $this->executeAction($id, ['type' => 'rebuild', 'image' => $image]);
}
/**
* @throws ExceptionInterface
*/
public function rename(int $id, string $name): ActionEntity
{
return $this->executeAction($id, ['type' => 'rename', 'name' => $name]);
}
/**
* @throws ExceptionInterface
*/
public function changeKernel(int $id, int $kernel): ActionEntity
{
return $this->executeAction($id, ['type' => 'change_kernel', 'kernel' => $kernel]);
}
/**
* @throws ExceptionInterface
*/
public function enableIpv6(int $id): ActionEntity
{
return $this->executeAction($id, ['type' => 'enable_ipv6']);
}
/**
* @throws ExceptionInterface
*/
public function enableBackups(int $id): ActionEntity
{
return $this->executeAction($id, ['type' => 'enable_backups']);
}
/**
* @throws ExceptionInterface
*/
public function disableBackups(int $id): ActionEntity
{
return $this->executeAction($id, ['type' => 'disable_backups']);
}
/**
* @throws ExceptionInterface
*/
public function enablePrivateNetworking(int $id): ActionEntity
{
return $this->executeAction($id, ['type' => 'enable_private_networking']);
}
/**
* @throws ExceptionInterface
*/
public function snapshot(int $id, string $name): ActionEntity
{
return $this->executeAction($id, ['type' => 'snapshot', 'name' => $name]);
}
/**
* @throws ExceptionInterface
*/
private function executeAction(int $id, array $options): ActionEntity
{
$action = $this->post(\sprintf('droplets/%d/actions', $id), $options);
return new ActionEntity($action->action);
}
}