Volumes

A volume is a detachable block storage device similar to a USB hard drive. You can attach a volume to an instance, and if the volume is of an appropriate volume type, a volume can be attached to multiple instances.

More information can be found in the official documentation.

In order to work with volumes you have to create the service first.

Create

The only attributes that are required when creating a volume are a size in GiB. The simplest example would therefore be this:

$service = $openstack->blockStorageV3();

$volume = $service->createVolume([
    'description' => '{description}',
    'size'        => '{size}',
    'name'        => '{name}',
    'volumeType'  => '{volumeType}',
    'metadata'    => ['{key1}' => '{val1}'],
]);

You can further configure your new volume, however, by following the below sections, which instruct you how to add specific functionality.

Create from image

$service = $openstack->blockStorageV3();

$volume = $service->createVolume([
    'description' => '{description}',
    'size'        => '{size}',
    'name'        => '{name}',
    'imageId'     => '{imageId}',
]);

Create from snapshot

$service = $openstack->blockStorageV3();

$volume = $service->createVolume([
    'description' => '{description}',
    'size'        => '{size}',
    'name'        => '{name}',
    'snapshotId'  => '{snapshotId}',
]);

Create from volume

$service = $openstack->blockStorageV3();

$volume = $service->createVolume([
    'description'    => '{description}',
    'size'           => '{size}',
    'name'           => '{name}',
    'sourceVolumeId' => '{volumeId}',
]);

Read

$service = $openstack->blockStorageV3();

$volume = $service->getVolume('{volumeId}');
$volume->retrieve();

Update

The first step when updating a volume is modifying the attributes you want updated. Only a volume’s name and description can be edited.

$service = $openstack->blockStorageV3();

$volume = $service->getVolume('{volumeId}');

$volume->name = '{newName}';
$volume->description = '{newDescription}';

$volume->update();

Delete

To permanently delete a volume:

$service = $openstack->blockStorageV3();

$volume = $service->getVolume('{volumeId}');
$volume->delete();

List

$service = $openstack->blockStorageV3();

$volumes = $service->listVolumes();

foreach ($volumes as $volume) {
    /** @var \OpenStack\BlockStorage\v2\Models\Volume $volume */
}

Each iteration will return a php:class:Volume instance <OpenStack/BlockStorage/v2/Models/Volume.html>.

By default, PHP generators are used to represent collections of resources in the SDK. The benefit of using generators is that it generally improves performance, since objects are not saved in memory as the iteration cycle goes on; instead, each resource is directly output to the user-defined foreach loop. For all intents and purposes, you interact with generators like any other Traversable object, but to retain collections in memory, you will need to implement your own logic.

Detailed information

By default, only the id, links and name attributes are returned. To return all information for a flavor, you must enable detailed information:

$service = $openstack->blockStorageV3();

foreach ($service->listVolumes(true) as $volume) {
    /** @var $volume \OpenStack\BlockStorage\v2\Models\Volume */
}