Skip to content

Commit 5aa9715

Browse files
committed
Compute instance: attach/detach volume, list volume attachments
1 parent 68dc58a commit 5aa9715

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

src/Compute/v2/Api.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,4 +510,41 @@ public function listSecurityGroupByServer(): array
510510
];
511511
}
512512

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+
513550
}

src/Compute/v2/Models/Server.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use OpenCloud\Common\Resource\Updateable;
1111
use OpenCloud\Common\Resource\OperatorResource;
1212
use OpenCloud\Common\Transport\Utils;
13+
use OpenStack\BlockStorage\v2\Models\Volume;
14+
use OpenStack\BlockStorage\v2\Models\VolumeAttachment;
1315
use OpenStack\Compute\v2\Enum;
1416
use OpenStack\Networking\v2\Extensions\SecurityGroups\Models\SecurityGroup;
1517
use Psr\Http\Message\ResponseInterface;
@@ -338,4 +340,32 @@ public function listSecurityGroups()
338340
{
339341
return $this->model(SecurityGroup::class)->enumerate($this->api->listSecurityGroupByServer(), ['id' => $this->id]);
340342
}
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+
}
341371
}

src/Compute/v2/Params.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,4 +429,21 @@ public function flavorSwap(): array
429429
];
430430
}
431431

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+
432449
}

0 commit comments

Comments
 (0)