Images
A collection of files for a specific operating system (OS) that you use to create or rebuild a server. OpenStack provides pre-built images. You can also create custom images, or snapshots, from servers that you have launched. Custom images can be used for data backups or as “gold” images for additional servers.
More information can be found in the official documentation.
In order to work with images you have to create the service first.
Create
The only required attribute when creating a new image is name
.
$service = $openstack->imagesV2();
$image = $service->createImage([
'name' => '{name}',
'tags' => ['{tag1}', '{tag2}'],
'containerFormat' => '{containerFormat}',
'diskFormat' => '{diskFormat}',
'visibility' => '{visibility}',
'minDisk' => 10,
'protected' => false,
'minRam' => 10,
]);
$image->uploadData(\GuzzleHttp\Psr7\Utils::streamFor('fake-image.img'));
Read
$service = $openstack->imagesV2();
$image = $service->getImage('{imageId}');
$image->retrieve();
Update
$service = $openstack->imagesV2();
$image = $service->getImage('{imageId}');
$image->update([
'minDisk' => 1,
'minRam' => 1,
'name' => '{name}',
'protected' => false,
'visibility' => '{visibility}',
]);
Delete
$service = $openstack->imagesV2();
$image = $service->getImage('{imageId}');
$image->delete();
List
$service = $openstack->imagesV2();
$images = $service->listImages();
foreach ($images as $image) {
/** @var \OpenStack\Images\v2\Models\Image $image */
}
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.
List images sorted
Possible values for sort_key are:
name
Possible values for sort_dir are:
asc
desc
$service = $openstack->imagesV2();
$images = $service->listImages(['sortKey' => '{sortKey}', 'sortDir' => '{sortDir}']);
foreach ($images as $image) {
/** @var \OpenStack\Images\v2\Models\Image $image */
}
Reactivate
$service = $openstack->imagesV2();
$image = $service->getImage('{imageId}');
$image->reactivate();
Deactivate
If you try to download a deactivated image, a Forbidden error is returned.
$service = $openstack->imagesV2();
$image = $service->getImage('{imageId}');
$image->deactivate();
Upload binary data
Before you can store binary image data, you must meet the following preconditions:
The image must exist.
You must set the disk and container formats in the image.
The image status must be
queued
.Your image storage quota must be sufficient.
The size of the data that you want to store must not exceed the size that the Image service allows.
$service = $openstack->imagesV2();
$image = $service->getImage('{imageId}');
$stream = function_exists('\GuzzleHttp\Psr7\stream_for')
? \GuzzleHttp\Psr7\stream_for(fopen('{fileName}', 'r'))
: \GuzzleHttp\Psr7\Utils::streamFor(fopen('{fileName}', 'r'));
$image->uploadData($stream);
Download binary data
$service = $openstack->imagesV2();
$image = $service->getImage('{imageId}');
$stream = $image->downloadData();