Skip to content

Commit

Permalink
Implement resuming and suspending of servers
Browse files Browse the repository at this point in the history
  • Loading branch information
mzur committed Dec 18, 2018
1 parent 3eba351 commit 4a3549b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
21 changes: 21 additions & 0 deletions samples/compute/v2/servers/resume_server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

require 'vendor/autoload.php';

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

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

$server = $compute->getServer([
'id' => '{serverId}',
]);

$server->resume();
21 changes: 21 additions & 0 deletions samples/compute/v2/servers/suspend_server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

require 'vendor/autoload.php';

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

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

$server = $compute->getServer([
'id' => '{serverId}',
]);

$server->suspend();
24 changes: 24 additions & 0 deletions src/Compute/v2/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,30 @@ public function stopServer(): array
];
}

public function resumeServer(): array
{
return [
'method' => 'POST',
'path' => 'servers/{id}/action',
'params' => [
'id' => $this->params->urlId('server'),
'resume' => $this->params->nullAction(),
],
];
}

public function suspendServer(): array
{
return [
'method' => 'POST',
'path' => 'servers/{id}/action',
'params' => [
'id' => $this->params->urlId('server'),
'suspend' => $this->params->nullAction(),
],
];
}

public function rebuildServer(): array
{
return [
Expand Down
22 changes: 22 additions & 0 deletions src/Compute/v2/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,28 @@ public function stop()
]);
}

/**
* Resumes server.
*/
public function resume()
{
$this->execute($this->api->resumeServer(), [
'id' => $this->id,
'resume' => null,
]);
}

/**
* Suspends server.
*/
public function suspend()
{
$this->execute($this->api->suspendServer(), [
'id' => $this->id,
'suspend' => null,
]);
}

/**
* Rebuilds the server.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/Compute/v2/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,30 @@ private function startServer()
$this->logStep('Started server {serverId}', $replacements);
}

private function suspendServer()
{
$replacements = ['{serverId}' => $this->serverId];

/** @var $server \OpenStack\Compute\v2\Models\Server */
require_once $this->sampleFile($replacements, 'servers/suspend_server.php');

$server->waitUntil('SUSPENDED', false);

$this->logStep('Suspended server {serverId}', $replacements);
}

private function resumeServer()
{
$replacements = ['{serverId}' => $this->serverId];

/** @var $server \OpenStack\Compute\v2\Models\Server */
require_once $this->sampleFile($replacements, 'servers/resume_server.php');

$server->waitUntilActive(false);

$this->logStep('Resumed server {serverId}', $replacements);
}

private function createFlavor()
{
$replacements = [
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/Compute/v2/Models/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ public function test_it_stops()
$this->assertNull($this->server->stop());
}

public function test_it_resumes()
{
$expectedJson = ['resume' => null];

$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], new Response(202));

$this->assertNull($this->server->resume());
}

public function test_it_suspends()
{
$expectedJson = ['suspend' => null];

$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], new Response(202));

$this->assertNull($this->server->suspend());
}

public function test_it_resizes()
{
$expectedJson = ['resize' => ['flavorRef' => 'flavorId']];
Expand Down

0 comments on commit 4a3549b

Please sign in to comment.