File tree Expand file tree Collapse file tree 6 files changed +167
-0
lines changed
samples/compute/v2/servers Expand file tree Collapse file tree 6 files changed +167
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use OpenStack \BlockStorage \v2 \Models \VolumeAttachment ;
4
+ use OpenStack \Networking \v2 \Extensions \SecurityGroups \Models \SecurityGroup ;
5
+ use OpenStack \Networking \v2 \Extensions \SecurityGroups \Models \SecurityGroupRule ;
6
+
7
+ require 'vendor/autoload.php ' ;
8
+
9
+ $ openstack = new OpenStack \OpenStack ([
10
+ 'authUrl ' => '{authUrl} ' ,
11
+ 'region ' => '{region} ' ,
12
+ 'user ' => [
13
+ 'id ' => '{userId} ' ,
14
+ 'password ' => '{password} '
15
+ ],
16
+ 'scope ' => ['project ' => ['id ' => '{projectId} ' ]]
17
+ ]);
18
+
19
+ $ compute = $ openstack ->computeV2 (['region ' => '{region} ' ]);
20
+
21
+ $ server = $ compute ->getServer (['id ' => 'uuid ' ]);
22
+
23
+ /**@var VolumeAttachment $volumeAttachment*/
24
+ $ volumeAttachment = $ server ->attachVolume ('{volume_uuid} ' );
25
+
26
+
27
+ //Must detach by volumeAttachment id
28
+ $ server ->detachVolume ($ volumeAttachment ->id );
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use OpenStack \BlockStorage \v2 \Models \VolumeAttachment ;
4
+ use OpenStack \Networking \v2 \Extensions \SecurityGroups \Models \SecurityGroup ;
5
+ use OpenStack \Networking \v2 \Extensions \SecurityGroups \Models \SecurityGroupRule ;
6
+
7
+ require 'vendor/autoload.php ' ;
8
+
9
+ $ openstack = new OpenStack \OpenStack ([
10
+ 'authUrl ' => '{authUrl} ' ,
11
+ 'region ' => '{region} ' ,
12
+ 'user ' => [
13
+ 'id ' => '{userId} ' ,
14
+ 'password ' => '{password} '
15
+ ],
16
+ 'scope ' => ['project ' => ['id ' => '{projectId} ' ]]
17
+ ]);
18
+
19
+ $ compute = $ openstack ->computeV2 (['region ' => '{region} ' ]);
20
+
21
+ $ server = $ compute ->getServer (['id ' => 'uuid ' ]);
22
+
23
+ foreach ($ server ->listVolumeAttachments () as $ volumeAttachment )
24
+ {
25
+ /**@var VolumeAttachment $volumeAttachment*/
26
+ print_r ($ volumeAttachment );
27
+ }
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types=1 );
2
+ namespace OpenStack \BlockStorage \v2 \Models ;
3
+
4
+ use OpenCloud \Common \Resource \OperatorResource ;
5
+
6
+ /**
7
+ * @property \OpenStack\BlockStorage\v2\Api $api
8
+ */
9
+ class VolumeAttachment extends OperatorResource
10
+ {
11
+ /** @var string */
12
+ public $ id ;
13
+
14
+ /** @var int */
15
+ public $ device ;
16
+
17
+ /** @var string */
18
+ public $ serverId ;
19
+
20
+ /** @var string */
21
+ public $ volumeId ;
22
+
23
+ protected $ resourceKey = 'volumeAttachment ' ;
24
+ protected $ resourcesKey = 'volumeAttachments ' ;
25
+
26
+ protected $ aliases = [
27
+ ];
28
+ }
Original file line number Diff line number Diff line change @@ -510,4 +510,41 @@ public function listSecurityGroupByServer(): array
510
510
];
511
511
}
512
512
513
+ public function listVolumeAttachments (): array
514
+ {
515
+ return [
516
+ 'method ' => 'GET ' ,
517
+ 'path ' => 'servers/{id}/os-volume_attachments ' ,
518
+ 'jsonKey ' => 'volumeAttachments ' ,
519
+ 'params ' => [
520
+ 'id ' => $ this ->params ->urlId ('server ' )
521
+ ]
522
+ ];
523
+ }
524
+
525
+ public function attachVolume (): array
526
+ {
527
+ return [
528
+ 'method ' => 'POST ' ,
529
+ 'path ' => 'servers/{id}/os-volume_attachments ' ,
530
+ 'jsonKey ' => 'volumeAttachment ' ,
531
+ 'params ' => [
532
+ 'id ' => $ this ->params ->urlId ('server ' ),
533
+ 'volumeId ' => $ this ->params ->volumeId ()
534
+ ]
535
+ ];
536
+ }
537
+
538
+ public function detachVolume (): array
539
+ {
540
+ return [
541
+ 'method ' => 'DELETE ' ,
542
+ 'path ' => 'servers/{id}/os-volume_attachments/{attachmentId} ' ,
543
+ 'params ' => [
544
+ 'id ' => $ this ->params ->urlId ('server ' ),
545
+ 'attachmentId ' => $ this ->params ->attachmentId ()
546
+ ]
547
+ ];
548
+ }
549
+
513
550
}
Original file line number Diff line number Diff line change 10
10
use OpenCloud \Common \Resource \Updateable ;
11
11
use OpenCloud \Common \Resource \OperatorResource ;
12
12
use OpenCloud \Common \Transport \Utils ;
13
+ use OpenStack \BlockStorage \v2 \Models \Volume ;
14
+ use OpenStack \BlockStorage \v2 \Models \VolumeAttachment ;
13
15
use OpenStack \Compute \v2 \Enum ;
14
16
use OpenStack \Networking \v2 \Extensions \SecurityGroups \Models \SecurityGroup ;
15
17
use Psr \Http \Message \ResponseInterface ;
@@ -338,4 +340,32 @@ public function listSecurityGroups()
338
340
{
339
341
return $ this ->model (SecurityGroup::class)->enumerate ($ this ->api ->listSecurityGroupByServer (), ['id ' => $ this ->id ]);
340
342
}
343
+
344
+
345
+ /**
346
+ * Returns Generator for SecurityGroups
347
+ *
348
+ * @return \Generator
349
+ */
350
+ public function listVolumeAttachments ()
351
+ {
352
+ return $ this ->model (VolumeAttachment::class)->enumerate ($ this ->api ->listVolumeAttachments (),['id ' => $ this ->id ]);
353
+ }
354
+
355
+ /**
356
+ * @param $volumeId
357
+ *
358
+ * @return VolumeAttachment
359
+ */
360
+ public function attachVolume ($ volumeId )
361
+ {
362
+ $ response = $ this ->execute ($ this ->api ->attachVolume (), ['id ' => $ this ->id , 'volumeId ' => $ volumeId ]);
363
+
364
+ return $ this ->model (VolumeAttachment::class)->populateFromResponse ($ response );
365
+ }
366
+
367
+ public function detachVolume ($ attachmentId )
368
+ {
369
+ return $ this ->execute ($ this ->api ->detachVolume (), ['id ' => $ this ->id , 'attachmentId ' => $ attachmentId ]);
370
+ }
341
371
}
Original file line number Diff line number Diff line change @@ -429,4 +429,21 @@ public function flavorSwap(): array
429
429
];
430
430
}
431
431
432
+ public function volumeId (): array
433
+ {
434
+ return [
435
+ 'type ' => self ::STRING_TYPE ,
436
+ 'location ' => self ::JSON ,
437
+ ];
438
+ }
439
+
440
+ public function attachmentId (): array
441
+ {
442
+ return [
443
+ 'type ' => self ::STRING_TYPE ,
444
+ 'location ' => self ::URL ,
445
+ 'required ' => true ,
446
+ ];
447
+ }
448
+
432
449
}
You can’t perform that action at this time.
0 commit comments