Server Groups

Server groups let you express affinity or anti-affinity placement rules for Nova servers.

More information can be found in the official documentation.

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

List

$compute = $openstack->computeV2(['region' => '{region}']);

$serverGroups = $compute->listServerGroups();

foreach ($serverGroups as $serverGroup) {
    /** @var \OpenStack\Compute\v2\Models\ServerGroup $serverGroup */
}

Each iteration will return a ServerGroup instance <OpenStack/Compute/v2/Models/ServerGroup.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.

Admin-only listing across all projects is also available:

$serverGroups = $service->listServerGroups([
    'allProjects' => true,
]);

Create

Use name and policies for the baseline Compute API:

$compute = $openstack->computeV2(['region' => '{region}']);

$serverGroup = $compute->createServerGroup([
    'name'     => '{serverGroupName}',
    'policies' => ['affinity'],
]);

Microversion 2.64+

If the Compute service is created with microversion 2.64 or later, you can use the singular policy field and optional rules object instead:

$compute = $openstack->computeV2([
    'region'       => '{region}',
    'microVersion' => '2.64',
]);

$serverGroup = $compute->createServerGroup([
    'name'   => '{serverGroupName}',
    'policy' => 'anti-affinity',
    'rules'  => [
        'max_server_per_host' => 1,
    ],
]);

When Nova responds with the newer singular policy field, the SDK also exposes that value as the first item in policies for compatibility with the older response shape.

Create A Server In A Group

To place a server into an existing server group, pass the server group UUID through schedulerHints.group when you create the server:

$compute = $openstack->computeV2(['region' => '{region}']);

$server = $compute->createServer([
    'name'           => '{serverName}',
    'imageId'        => '{imageId}',
    'flavorId'       => '{flavorId}',
    'networks'       => [
        ['uuid' => '{networkId}'],
    ],
    'schedulerHints' => [
        'group' => '{serverGroupId}',
    ],
]);

Read

$compute = $openstack->computeV2(['region' => '{region}']);

$serverGroup = $compute->getServerGroup(['id' => '{serverGroupId}']);
$serverGroup->retrieve();

Delete

$compute = $openstack->computeV2(['region' => '{region}']);

$serverGroup = $compute->getServerGroup(['id' => '{serverGroupId}']);
$serverGroup->delete();