Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement resuming and suspending of servers #271

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -217,6 +217,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
26 changes: 26 additions & 0 deletions tests/integration/Compute/v2/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public function runTests()
//$this->changeServerPassword();
$this->stopServer();
$this->startServer();
$this->suspendServer();
$this->resumeServer();
$this->resizeServer();
$this->confirmServerResize();
$this->rebuildServer();
Expand Down Expand Up @@ -446,6 +448,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 @@ -211,6 +211,24 @@ public function test_it_stops()
self::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
Loading