1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Compute\v2\Models; |
4
|
|
|
|
5
|
|
|
use OpenStack\Common\Resource\HasWaiterTrait; |
6
|
|
|
use OpenStack\Common\Resource\Creatable; |
7
|
|
|
use OpenStack\Common\Resource\Deletable; |
8
|
|
|
use OpenStack\Common\Resource\Listable; |
9
|
|
|
use OpenStack\Common\Resource\Retrievable; |
10
|
|
|
use OpenStack\Common\Resource\Updateable; |
11
|
|
|
use OpenStack\Common\Resource\OperatorResource; |
12
|
|
|
use OpenStack\Common\Transport\Utils; |
13
|
|
|
use OpenStack\BlockStorage\v2\Models\VolumeAttachment; |
14
|
|
|
use OpenStack\Networking\v2\Models\InterfaceAttachment; |
15
|
|
|
use OpenStack\Compute\v2\Enum; |
16
|
|
|
use OpenStack\Networking\v2\Extensions\SecurityGroups\Models\SecurityGroup; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @property \OpenStack\Compute\v2\Api $api |
21
|
|
|
*/ |
22
|
|
|
class Server extends OperatorResource implements |
23
|
|
|
Creatable, |
24
|
|
|
Updateable, |
25
|
|
|
Deletable, |
26
|
|
|
Retrievable, |
27
|
|
|
Listable |
28
|
|
|
{ |
29
|
|
|
use HasWaiterTrait; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
public $id; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
public $ipv4; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
public $ipv6; |
39
|
|
|
|
40
|
|
|
/** @var array */ |
41
|
|
|
public $addresses; |
42
|
|
|
|
43
|
|
|
/** @var \DateTimeImmutable */ |
44
|
|
|
public $created; |
45
|
|
|
|
46
|
|
|
/** @var \DateTimeImmutable */ |
47
|
|
|
public $updated; |
48
|
|
|
|
49
|
|
|
/** @var Flavor */ |
50
|
|
|
public $flavor; |
51
|
|
|
|
52
|
|
|
/** @var string */ |
53
|
|
|
public $hostId; |
54
|
|
|
|
55
|
|
|
/** @var string */ |
56
|
|
|
public $hypervisorHostname; |
57
|
|
|
|
58
|
|
|
/** @var Image */ |
59
|
|
|
public $image; |
60
|
|
|
|
61
|
|
|
/** @var array */ |
62
|
|
|
public $links; |
63
|
|
|
|
64
|
|
|
/** @var array */ |
65
|
|
|
public $metadata; |
66
|
|
|
|
67
|
|
|
/** @var string */ |
68
|
|
|
public $name; |
69
|
|
|
|
70
|
|
|
/** @var string */ |
71
|
|
|
public $progress; |
72
|
|
|
|
73
|
|
|
/** @var string */ |
74
|
|
|
public $status; |
75
|
|
|
|
76
|
|
|
/** @var string */ |
77
|
|
|
public $tenantId; |
78
|
|
|
|
79
|
|
|
/** @var string */ |
80
|
|
|
public $userId; |
81
|
|
|
|
82
|
|
|
/** @var string */ |
83
|
|
|
public $adminPass; |
84
|
|
|
|
85
|
|
|
/** @var string */ |
86
|
|
|
public $taskState; |
87
|
|
|
|
88
|
|
|
protected $resourceKey = 'server'; |
89
|
|
|
protected $resourcesKey = 'servers'; |
90
|
|
|
protected $markerKey = 'id'; |
91
|
|
|
|
92
|
|
|
protected $aliases = [ |
93
|
|
|
'block_device_mapping_v2' => 'blockDeviceMapping', |
94
|
|
|
'accessIPv4' => 'ipv4', |
95
|
|
|
'accessIPv6' => 'ipv6', |
96
|
|
|
'tenant_id' => 'tenantId', |
97
|
|
|
'user_id' => 'userId', |
98
|
|
|
'security_groups' => 'securityGroups', |
99
|
|
|
'OS-EXT-STS:task_state' => 'taskState', |
100
|
|
|
'OS-EXT-SRV-ATTR:hypervisor_hostname' => 'hypervisorHostname', |
101
|
2 |
|
]; |
102
|
|
|
|
103
|
2 |
|
/** |
104
|
2 |
|
* {@inheritDoc} |
105
|
|
|
* |
106
|
|
|
* @param array $userOptions {@see \OpenStack\Compute\v2\Api::postServer} |
107
|
|
|
*/ |
108
|
|
|
public function create(array $userOptions): Creatable |
109
|
|
|
{ |
110
|
1 |
|
if (!isset($userOptions['imageId']) && !isset($userOptions['blockDeviceMapping'][0]['uuid'])) { |
111
|
|
|
throw new \RuntimeException('imageId or blockDeviceMapping.uuid must be set.'); |
112
|
1 |
|
} |
113
|
|
|
|
114
|
1 |
|
$response = $this->execute($this->api->postServer(), $userOptions); |
115
|
|
|
return $this->populateFromResponse($response); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritDoc} |
120
|
1 |
|
*/ |
121
|
|
|
public function update() |
122
|
1 |
|
{ |
123
|
1 |
|
$response = $this->execute($this->api->putServer(), $this->getAttrs(['id', 'name', 'ipv4', 'ipv6'])); |
124
|
|
|
$this->populateFromResponse($response); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
1 |
|
* {@inheritDoc} |
129
|
|
|
*/ |
130
|
1 |
|
public function delete() |
131
|
|
|
{ |
132
|
1 |
|
$this->execute($this->api->deleteServer(), $this->getAttrs(['id'])); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritDoc} |
137
|
|
|
*/ |
138
|
|
|
public function retrieve() |
139
|
|
|
{ |
140
|
1 |
|
$response = $this->execute($this->api->getServer(), $this->getAttrs(['id'])); |
141
|
|
|
$this->populateFromResponse($response); |
142
|
1 |
|
} |
143
|
1 |
|
|
144
|
|
|
/** |
145
|
1 |
|
* Changes the root password for a server. |
146
|
1 |
|
* |
147
|
|
|
* @param string $newPassword The new root password |
148
|
|
|
*/ |
149
|
|
|
public function changePassword(string $newPassword) |
150
|
|
|
{ |
151
|
|
|
$this->execute($this->api->changeServerPassword(), [ |
152
|
|
|
'id' => $this->id, |
153
|
2 |
|
'password' => $newPassword, |
154
|
|
|
]); |
155
|
2 |
|
} |
156
|
1 |
|
|
157
|
|
|
/** |
158
|
|
|
* Reboots the server. |
159
|
1 |
|
* |
160
|
1 |
|
* @param string $type The type of reboot that will be performed. Either SOFT or HARD is supported. |
161
|
1 |
|
*/ |
162
|
1 |
|
public function reboot(string $type = Enum::REBOOT_SOFT) |
163
|
1 |
|
{ |
164
|
|
|
if (!in_array($type, ['SOFT', 'HARD'])) { |
165
|
|
|
throw new \RuntimeException('Reboot type must either be SOFT or HARD'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$this->execute($this->api->rebootServer(), [ |
169
|
|
|
'id' => $this->id, |
170
|
1 |
|
'type' => $type, |
171
|
|
|
]); |
172
|
1 |
|
} |
173
|
1 |
|
|
174
|
|
|
/** |
175
|
1 |
|
* Starts server |
176
|
1 |
|
*/ |
177
|
|
|
public function start() |
178
|
|
|
{ |
179
|
|
|
$this->execute($this->api->startServer(), [ |
180
|
|
|
'id' => $this->id, |
181
|
|
|
'os-start' => null |
182
|
|
|
]); |
183
|
|
|
} |
184
|
1 |
|
|
185
|
|
|
/** |
186
|
1 |
|
* Stops server |
187
|
1 |
|
*/ |
188
|
1 |
|
public function stop() |
189
|
1 |
|
{ |
190
|
|
|
$this->execute($this->api->stopServer(), [ |
191
|
1 |
|
'id' => $this->id, |
192
|
1 |
|
'os-stop' => null |
193
|
|
|
]); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
1 |
|
* Suspend server |
198
|
|
|
*/ |
199
|
1 |
|
public function suspend() |
200
|
1 |
|
{ |
201
|
|
|
$this->execute($this->api->suspendServer(), [ |
202
|
|
|
'id' => $this->id, |
203
|
|
|
'suspend' => null |
204
|
|
|
]); |
205
|
1 |
|
} |
206
|
|
|
|
207
|
1 |
|
/** |
208
|
1 |
|
* Resume server |
209
|
|
|
*/ |
210
|
|
|
public function resume() |
211
|
|
|
{ |
212
|
|
|
$this->execute($this->api->resumeServer(), [ |
213
|
|
|
'id' => $this->id, |
214
|
|
|
'resume' => null |
215
|
1 |
|
]); |
216
|
|
|
} |
217
|
1 |
|
|
218
|
1 |
|
/** |
219
|
1 |
|
* Rebuilds the server. |
220
|
|
|
* |
221
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::rebuildServer} |
222
|
|
|
*/ |
223
|
|
|
public function rebuild(array $options) |
224
|
|
|
{ |
225
|
|
|
$options['id'] = $this->id; |
226
|
|
|
$response = $this->execute($this->api->rebuildServer(), $options); |
227
|
|
|
|
228
|
2 |
|
$this->populateFromResponse($response); |
229
|
|
|
} |
230
|
2 |
|
|
231
|
|
|
/** |
232
|
2 |
|
* Resizes the server to a new flavor. Once this operation is complete and server has transitioned |
233
|
2 |
|
* to an active state, you will either need to call {@see confirmResize()} or {@see revertResize()}. |
234
|
2 |
|
* |
235
|
|
|
* @param string $flavorId The UUID of the new flavor your server will be based on. |
236
|
|
|
*/ |
237
|
|
|
public function resize(string $flavorId) |
238
|
|
|
{ |
239
|
|
|
$response = $this->execute($this->api->resizeServer(), [ |
240
|
|
|
'id' => $this->id, |
241
|
|
|
'flavorId' => $flavorId, |
242
|
1 |
|
]); |
243
|
|
|
|
244
|
1 |
|
$this->populateFromResponse($response); |
245
|
1 |
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Confirms a previous resize operation. |
249
|
|
|
*/ |
250
|
|
|
public function confirmResize() |
251
|
|
|
{ |
252
|
|
|
$this->execute($this->api->confirmServerResize(), ['confirmResize' => null, 'id' => $this->id]); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
1 |
|
* Reverts a previous resize operation. |
257
|
|
|
*/ |
258
|
1 |
|
public function revertResize() |
259
|
1 |
|
{ |
260
|
|
|
$this->execute($this->api->revertServerResize(), ['revertResize' => null, 'id' => $this->id]); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Gets a VNC console for a server. |
265
|
|
|
* |
266
|
|
|
* @param string $type The type of VNC console: novnc|xvpvnc. |
267
|
|
|
* Defaults to novnc. |
268
|
|
|
* |
269
|
|
|
* @return array |
270
|
|
|
*/ |
271
|
1 |
|
public function getVncConsole($type = Enum::CONSOLE_NOVNC): array |
272
|
|
|
{ |
273
|
1 |
|
$response = $this->execute($this->api->getVncConsole(), ['id' => $this->id, 'type' => $type]); |
274
|
1 |
|
return Utils::jsonDecode($response)['console']; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Gets a RDP console for a server. |
279
|
|
|
* |
280
|
|
|
* @param string $type The type of VNC console: rdp-html5 (default). |
281
|
|
|
* |
282
|
|
|
* @return array |
283
|
|
|
*/ |
284
|
1 |
|
public function getRDPConsole($type = Enum::CONSOLE_RDP_HTML5): array |
285
|
|
|
{ |
286
|
1 |
|
$response = $this->execute($this->api->getRDPConsole(), ['id' => $this->id, 'type' => $type]); |
287
|
1 |
|
return Utils::jsonDecode($response)['console']; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Gets a Spice console for a server. |
292
|
|
|
* |
293
|
|
|
* @param string $type The type of VNC console: spice-html5. |
294
|
|
|
* |
295
|
1 |
|
* @return array |
296
|
|
|
*/ |
297
|
1 |
|
public function getSpiceConsole($type = Enum::CONSOLE_SPICE_HTML5): array |
298
|
1 |
|
{ |
299
|
|
|
$response = $this->execute($this->api->getSpiceConsole(), ['id' => $this->id, 'type' => $type]); |
300
|
|
|
return Utils::jsonDecode($response)['console']; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Gets a serial console for a server. |
305
|
|
|
* |
306
|
|
|
* @param string $type The type of VNC console: serial. |
307
|
|
|
* |
308
|
|
|
* @return array |
309
|
|
|
*/ |
310
|
|
|
public function getSerialConsole($type = Enum::CONSOLE_SERIAL): array |
311
|
|
|
{ |
312
|
|
|
$response = $this->execute($this->api->getSerialConsole(), ['id' => $this->id, 'type' => $type]); |
313
|
|
|
return Utils::jsonDecode($response)['console']; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Creates an image for the current server. |
318
|
|
|
* |
319
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::createServerImage} |
320
|
|
|
*/ |
321
|
|
|
public function createImage(array $options) |
322
|
|
|
{ |
323
|
|
|
$options['id'] = $this->id; |
324
|
|
|
$this->execute($this->api->createServerImage(), $options); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Iterates over all the IP addresses for this server. |
329
|
|
|
* |
330
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getAddressesByNetwork} |
331
|
|
|
* |
332
|
|
|
* @return array An array containing to two keys: "public" and "private" |
333
|
|
|
*/ |
334
|
|
|
public function listAddresses(array $options = []): array |
335
|
|
|
{ |
336
|
|
|
$options['id'] = $this->id; |
337
|
|
|
|
338
|
|
|
$data = (isset($options['networkLabel'])) ? $this->api->getAddressesByNetwork() : $this->api->getAddresses(); |
339
|
|
|
$response = $this->execute($data, $options); |
340
|
|
|
return Utils::jsonDecode($response)['addresses']; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Returns Generator for InterfaceAttachment |
345
|
|
|
* |
346
|
|
|
* @return \Generator |
347
|
|
|
*/ |
348
|
|
|
public function listInterfaceAttachments(array $options = []): \Generator |
|
|
|
|
349
|
|
|
{ |
350
|
|
|
return $this->model(InterfaceAttachment::class)->enumerate($this->api->getInterfaceAttachments(), ['id' => $this->id]); |
|
|
|
|
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Retrieves metadata from the API. |
355
|
|
|
* |
356
|
|
|
* @return array |
357
|
|
|
*/ |
358
|
|
|
public function getMetadata(): array |
359
|
|
|
{ |
360
|
|
|
$response = $this->execute($this->api->getServerMetadata(), ['id' => $this->id]); |
361
|
|
|
return $this->parseMetadata($response); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Resets all the metadata for this server with the values provided. All existing metadata keys |
366
|
|
|
* will either be replaced or removed. |
367
|
|
|
* |
368
|
|
|
* @param array $metadata {@see \OpenStack\Compute\v2\Api::putServerMetadata} |
369
|
|
|
*/ |
370
|
|
|
public function resetMetadata(array $metadata) |
371
|
|
|
{ |
372
|
|
|
$response = $this->execute($this->api->putServerMetadata(), ['id' => $this->id, 'metadata' => $metadata]); |
373
|
|
|
$this->metadata = $this->parseMetadata($response); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Merges the existing metadata for the server with the values provided. Any existing keys |
378
|
|
|
* referenced in the user options will be replaced with the user's new values. All other |
379
|
|
|
* existing keys will remain unaffected. |
380
|
|
|
* |
381
|
|
|
* @param array $metadata {@see \OpenStack\Compute\v2\Api::postServerMetadata} |
382
|
|
|
* |
383
|
|
|
* @return array |
384
|
|
|
*/ |
385
|
|
|
public function mergeMetadata(array $metadata) |
386
|
|
|
{ |
387
|
|
|
$response = $this->execute($this->api->postServerMetadata(), ['id' => $this->id, 'metadata' => $metadata]); |
388
|
|
|
$this->metadata = $this->parseMetadata($response); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Retrieve the value for a specific metadata key. |
393
|
|
|
* |
394
|
|
|
* @param string $key {@see \OpenStack\Compute\v2\Api::getServerMetadataKey} |
395
|
|
|
* |
396
|
|
|
* @return mixed |
397
|
|
|
*/ |
398
|
|
View Code Duplication |
public function getMetadataItem(string $key) |
|
|
|
|
399
|
|
|
{ |
400
|
|
|
$response = $this->execute($this->api->getServerMetadataKey(), ['id' => $this->id, 'key' => $key]); |
401
|
|
|
$value = $this->parseMetadata($response)[$key]; |
402
|
|
|
$this->metadata[$key] = $value; |
403
|
|
|
return $value; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Remove a specific metadata key. |
408
|
|
|
* |
409
|
|
|
* @param string $key {@see \OpenStack\Compute\v2\Api::deleteServerMetadataKey} |
410
|
|
|
*/ |
411
|
|
View Code Duplication |
public function deleteMetadataItem(string $key) |
|
|
|
|
412
|
|
|
{ |
413
|
|
|
if (isset($this->metadata[$key])) { |
414
|
|
|
unset($this->metadata[$key]); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
$this->execute($this->api->deleteServerMetadataKey(), ['id' => $this->id, 'key' => $key]); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Add security group to a server (addSecurityGroup action) |
423
|
|
|
* |
424
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::postSecurityGroup} |
425
|
|
|
* |
426
|
|
|
* @return SecurityGroup |
427
|
|
|
*/ |
428
|
|
|
public function addSecurityGroup(array $options) : SecurityGroup |
429
|
|
|
{ |
430
|
|
|
$options['id'] = $this->id; |
431
|
|
|
|
432
|
|
|
$response = $this->execute($this->api->postSecurityGroup(), $options); |
433
|
|
|
|
434
|
|
|
return $this->model(SecurityGroup::class)->populateFromResponse($response); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Add security group to a server (addSecurityGroup action) |
439
|
|
|
* |
440
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::deleteSecurityGroup} |
441
|
|
|
*/ |
442
|
|
|
public function removeSecurityGroup(array $options) |
443
|
|
|
{ |
444
|
|
|
$options['id'] = $this->id; |
445
|
|
|
$this->execute($this->api->deleteSecurityGroup(), $options); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
public function parseMetadata(ResponseInterface $response): array |
449
|
|
|
{ |
450
|
|
|
return Utils::jsonDecode($response)['metadata']; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Returns Generator for SecurityGroups |
455
|
|
|
* |
456
|
|
|
* @return \Generator |
457
|
|
|
*/ |
458
|
|
|
public function listSecurityGroups(): \Generator |
459
|
|
|
{ |
460
|
|
|
return $this->model(SecurityGroup::class)->enumerate($this->api->getSecurityGroups(), ['id' => $this->id]); |
|
|
|
|
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Returns Generator for VolumeAttachment |
466
|
|
|
* |
467
|
|
|
* @return \Generator |
468
|
|
|
*/ |
469
|
|
|
public function listVolumeAttachments(): \Generator |
470
|
|
|
{ |
471
|
|
|
return $this->model(VolumeAttachment::class)->enumerate($this->api->getVolumeAttachments(), ['id' => $this->id]); |
|
|
|
|
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Attach a volume and returns volume that was attached |
476
|
|
|
* |
477
|
|
|
* @param $volumeId |
478
|
|
|
* |
479
|
|
|
* @return VolumeAttachment |
480
|
|
|
*/ |
481
|
|
|
public function attachVolume(string $volumeId): VolumeAttachment |
482
|
|
|
{ |
483
|
|
|
$response = $this->execute($this->api->postVolumeAttachments(), ['id' => $this->id, 'volumeId' => $volumeId]); |
484
|
|
|
|
485
|
|
|
return $this->model(VolumeAttachment::class)->populateFromResponse($response); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Detach a volume |
490
|
|
|
* |
491
|
|
|
* @param $attachmentId |
492
|
|
|
* |
493
|
|
|
* @return void |
494
|
|
|
*/ |
495
|
|
|
public function detachVolume(string $attachmentId) |
496
|
|
|
{ |
497
|
|
|
$this->execute($this->api->deleteVolumeAttachments(), ['id' => $this->id, 'attachmentId' => $attachmentId]); |
498
|
|
|
} |
499
|
|
|
} |
500
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.