Volumes

List volumes

Show auth code
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
    'authUrl' => '{authUrl}',
    'region'  => '{region}',
    'user'    => ['id' => '{userId}', 'password' => '{password}'],
    'scope'   => ['project' => ['id' => '{projectId}']]
]);
$service = $openstack->blockStorageV2();

$volumes = $service->listVolumes();

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

To see all the required and optional parameters for this operation, along with their types and descriptions, view the reference documentation.

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, like so:

Show auth code
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
    'authUrl' => '{authUrl}',
    'region'  => '{region}',
    'user'    => ['id' => '{userId}', 'password' => '{password}'],
    'scope'   => ['project' => ['id' => '{projectId}']]
]);
$service = $openstack->blockStorageV2();

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

Create volume

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

Show auth code
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
    'authUrl' => '{authUrl}',
    'region'  => '{region}',
    'user'    => ['id' => '{userId}', 'password' => '{password}'],
    'scope'   => ['project' => ['id' => '{projectId}']]
]);
$service = $openstack->blockStorageV2();

$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.

To see all the required and optional parameters for this operation, along with their types and descriptions, view the reference documentation.

Create from image

Show auth code
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
    'authUrl' => '{authUrl}',
    'region'  => '{region}',
    'user'    => ['id' => '{userId}', 'password' => '{password}'],
    'scope'   => ['project' => ['id' => '{projectId}']]
]);
$service = $openstack->blockStorageV2();

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

Create from snapshot

Show auth code
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
    'authUrl' => '{authUrl}',
    'region'  => '{region}',
    'user'    => ['id' => '{userId}', 'password' => '{password}'],
    'scope'   => ['project' => ['id' => '{projectId}']]
]);
$service = $openstack->blockStorageV2();

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

Create from source volume

Show auth code
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
    'authUrl' => '{authUrl}',
    'region'  => '{region}',
    'user'    => ['id' => '{userId}', 'password' => '{password}'],
    'scope'   => ['project' => ['id' => '{projectId}']]
]);
$service = $openstack->blockStorageV2();

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

Retrieve volume details

When retrieving a volume, sometimes you only want to operate on it - say to update or delete it. If this is the case, then there is no need to perform an initial GET request to the API:

Show auth code
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
    'authUrl' => '{authUrl}',
    'region'  => '{region}',
    'user'    => ['id' => '{userId}', 'password' => '{password}'],
    'scope'   => ['project' => ['id' => '{projectId}']]
]);
$service = $openstack->blockStorageV2();

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

If, however, you do want to retrieve all the details of a remote volume from the API, you just call:

$volume->retrieve();

which will update the state of the local object. This gives you an element of control over your app’s performance.

To see all the required and optional parameters for this operation, along with their types and descriptions, view the reference documentation.

Update volume

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

Show auth code
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
    'authUrl' => '{authUrl}',
    'region'  => '{region}',
    'user'    => ['id' => '{userId}', 'password' => '{password}'],
    'scope'   => ['project' => ['id' => '{projectId}']]
]);
$service = $openstack->blockStorageV2();

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

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

$volume->update();

To see all the required and optional parameters for this operation, along with their types and descriptions, view the reference documentation.

Delete volume

To permanently delete a volume:

Show auth code
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
    'authUrl' => '{authUrl}',
    'region'  => '{region}',
    'user'    => ['id' => '{userId}', 'password' => '{password}'],
    'scope'   => ['project' => ['id' => '{projectId}']]
]);
$service = $openstack->blockStorageV2();

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

To see all the required and optional parameters for this operation, along with their types and descriptions, view the reference documentation.