Skip to content

Commit

Permalink
Fix sort_key and sort_dir parameters for BlockStorage/v2/listSnapshot…
Browse files Browse the repository at this point in the history
…s and Images/v2/listImages
  • Loading branch information
k0ka authored Dec 20, 2023
1 parent 3c85b6f commit 5712c30
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 11 deletions.
22 changes: 22 additions & 0 deletions doc/services/block-storage/v2/snapshots.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
Snapshots
=========

List volumes
------------

.. sample:: BlockStorage/v2/snapshots/list.php
.. refdoc:: OpenStack/BlockStorage/v2/Service.html#method_listSnapshots

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

.. include:: /common/generators.rst

List volumes sorted
~~~~~~~~~~~~~~~~~~~

Possible values for sort_key are:
* display_name

Possible values for sort_dir are:
* asc
* desc

.. sample:: BlockStorage/v2/snapshots/list_sorted.php
12 changes: 12 additions & 0 deletions doc/services/images/v2/images.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ List images

.. include:: /common/generators.rst

List images sorted
-----------

Possible values for sort_key are:
* name

Possible values for sort_dir are:
* asc
* desc

.. sample:: Images/v2/images/list_sorted.php

Show image details
------------------

Expand Down
18 changes: 18 additions & 0 deletions samples/BlockStorage/v2/snapshots/list_sorted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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();

$snapshots = $service->listSnapshots(false, ['sortKey' => '{sortKey}', 'sortDir' => '{sortDir}']);

foreach ($snapshots as $snapshot) {
/** @var $snapshot \OpenStack\BlockStorage\v2\Models\Snapshot */
}
1 change: 1 addition & 0 deletions samples/Images/v2/images/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
->listImages();

foreach ($images as $image) {
/** @var \OpenStack\Images\v2\Models\Image $image */
}
16 changes: 16 additions & 0 deletions samples/Images/v2/images/list_sorted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require 'vendor/autoload.php';

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

$images = $openstack->imagesV2()->listImages(['sortKey' => '{sortKey}', 'sortDir' => '{sortDir}']);

foreach ($images as $image) {
/** @var \OpenStack\Images\v2\Models\Image $image */
}
2 changes: 2 additions & 0 deletions src/Common/Api/AbstractParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function sortDir(): array
return [
'type' => self::STRING_TYPE,
'location' => self::QUERY,
'sentAs' => 'sort_dir',
'description' => 'Sorts by one or more sets of attribute and sort direction combinations.',
'enum' => ['asc', 'desc'],
];
Expand All @@ -95,6 +96,7 @@ public function sortKey(): array
return [
'type' => self::STRING_TYPE,
'location' => self::QUERY,
'sentAs' => 'sort_key',
'description' => 'Sorts by one or more sets of attribute and sort direction combinations.',
];
}
Expand Down
80 changes: 70 additions & 10 deletions tests/integration/BlockStorage/v2/CoreTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace OpenStack\integration\BlockStorage\v2;
namespace OpenStack\Integration\BlockStorage\v2;

use OpenStack\BlockStorage\v2\Models\Snapshot;
use OpenStack\BlockStorage\v2\Models\Volume;
Expand Down Expand Up @@ -34,22 +34,24 @@ public function runTests()
$this->volumeTypes();
$this->logger->info('-> Snapshots');
$this->snapshots();
$this->logger->info('-> Snapshot list');
$this->snapshotList();

$this->outputTimeTaken();
}

public function volumes()
{
$this->logStep('-> Volumes tests');
$this->logStep('Creating volume type');
$volumeType = $this->getService()->createVolumeType(['name' => $this->randomStr()]);

$replacements = [
'{description}' => $this->randomStr(),
"'{size}'" => 1,
'{name}' => $this->randomStr(),
'{volumeType}' => $volumeType->id,
'{key1}' => $this->randomStr(),
'{val1}' => $this->randomStr(),
"'{size}'" => 1,
'{name}' => $this->randomStr(),
'{volumeType}' => $volumeType->id,
'{key1}' => $this->randomStr(),
'{val1}' => $this->randomStr(),
];

$this->logStep('Creating volume');
Expand All @@ -70,7 +72,7 @@ public function volumes()

$replacements += [
'{newName}' => $this->randomStr(),
'{newDescription}' => $this->randomStr()
'{newDescription}' => $this->randomStr(),
];

$this->logStep('Updating volume');
Expand All @@ -82,6 +84,7 @@ public function volumes()
/** @var \Generator $volumes */
require_once $this->sampleFile($replacements, 'volumes/list.php');

$volume = $this->getService()->getVolume($volumeId);
$volume->waitUntil('available');

$this->logStep('Deleting volume');
Expand Down Expand Up @@ -135,8 +138,8 @@ public function snapshots()
$volume->waitUntil('available', 60);

$replacements = [
'{volumeId}' => $volume->id,
'{name}' => $this->randomStr(),
'{volumeId}' => $volume->id,
'{name}' => $this->randomStr(),
'{description}' => $this->randomStr(),
];

Expand Down Expand Up @@ -183,11 +186,68 @@ public function snapshots()

$snapshot->waitUntil('available', 60);

$this->logStep('Listing snapshots');
require_once $this->sampleFile($replacements, 'snapshots/list.php');

$this->logStep('Deleting snapshot');
require_once $this->sampleFile($replacements, 'snapshots/delete.php');
$snapshot->waitUntilDeleted();

$this->logStep('Deleting volume');
$volume->delete();
}

public function snapshotList()
{
$this->logStep('Creating volume');
$volume = $this->getService()->createVolume(['name' => $this->randomStr(), 'size' => 1]);
$volume->waitUntil('available', 60);

$names = ['b' . $this->randomStr(), 'a' . $this->randomStr(), 'd' . $this->randomStr(), 'c' . $this->randomStr()];
$createdSnapshots = [];
foreach ($names as $name) {
$this->logStep('Creating snapshot ' . $name);
$snapshot = $this->getService()->createSnapshot([
'volumeId' => $volume->id,
'name' => $name,
]);

self::assertInstanceOf(Snapshot::class, $snapshot);

$createdSnapshots[] = $snapshot;
$snapshot->waitUntil('available', 60);
}

try {
$replacements = [
'{sortKey}' => 'display_name',
'{sortDir}' => 'asc',
];

$this->logStep('Listing snapshots');
require_once $this->sampleFile($replacements, 'snapshots/list.php');

$this->logStep('Listing snapshots sorted asc');
/** @var Snapshot $snapshot */
require_once $this->sampleFile($replacements, 'snapshots/list_sorted.php');
self::assertInstanceOf(Snapshot::class, $snapshot);
self::assertEquals($names[2], $snapshot->name);

$this->logStep('Listing snapshots sorted desc');
$replacements['{sortDir}'] = 'desc';
/** @var Snapshot $snapshot */
require_once $this->sampleFile($replacements, 'snapshots/list_sorted.php');
self::assertInstanceOf(Snapshot::class, $snapshot);
self::assertEquals($names[1], $snapshot->name);
} finally {
foreach ($createdSnapshots as $snapshot) {
$this->logStep('Deleting snapshot ' . $snapshot->name);
$snapshot->delete();
$snapshot->waitUntilDeleted();
}

$this->logStep('Deleting volume');
$volume->delete();
}
}
}
67 changes: 66 additions & 1 deletion tests/integration/Images/v2/CoreTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
<?php

namespace OpenStack\integration\Images\v2;
namespace OpenStack\Integration\Images\v2;

use OpenStack\BlockStorage\v2\Models\Snapshot;
use OpenStack\Images\v2\Models\Image;
use OpenStack\Images\v2\Models\Member;
use OpenStack\Integration\TestCase;
use OpenStack\Integration\Utils;

class CoreTest extends TestCase
{
private $service;

private function getService(): \OpenStack\Images\v2\Service
{
if (null === $this->service) {
$this->service = Utils::getOpenStack()->imagesV2();
}

return $this->service;
}

public function runTests()
{
$this->startTimer();

$this->logger->info('-> Images');
$this->images();

$this->logger->info('-> Members');
$this->members();

$this->logger->info('-> Image list');
$this->imageList();

$this->outputTimeTaken();
}

Expand Down Expand Up @@ -97,4 +116,50 @@ public function members()
/** @var Image $image */
require_once $this->sampleFile($replacements, 'images/delete.php');
}

public function imageList()
{
$this->logStep('Creating image');

$postfix = $this->randomStr();
$names = ['b' . $postfix, 'a' . $postfix, 'd' . $postfix, 'c' . $postfix];
$createdImages = [];
foreach ($names as $name) {
$this->logStep("Creating image $name");
$image = $this->getService()->createImage([
'name' => $name,
]);

self::assertInstanceOf(Image::class, $image);
$createdImages[] = $image;
}


$this->logStep('Listing images sorted asc');

$replacements = [
'{sortKey}' => 'name',
'{sortDir}' => 'asc',
];

/** @var \OpenStack\Images\v2\Models\Image $image */
require_once $this->sampleFile($replacements, 'images/list_sorted.php');
self::assertInstanceOf(Image::class, $image);
self::assertEquals($names[2], $image->name);


$this->logStep('Listing images sorted desc');

$replacements['{sortDir}'] = 'desc';
/** @var \OpenStack\Images\v2\Models\Image $image */
require_once $this->sampleFile($replacements, 'images/list_sorted.php');
self::assertInstanceOf(Image::class, $image);
self::assertEquals($names[1], $image->name);

foreach ($createdImages as $image) {
$this->logStep("Deleting image $image->name");
$image->delete();
}
}

}

0 comments on commit 5712c30

Please sign in to comment.