Skip to content

Commit

Permalink
Merge pull request #75 from haphan/server-action
Browse files Browse the repository at this point in the history
[rfr] Server action
  • Loading branch information
Jamie Hannaford authored Aug 22, 2016
2 parents b5ace79 + 5e89b13 commit 4e06611
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 6 deletions.
21 changes: 21 additions & 0 deletions samples/compute/v2/servers/start_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->start();
21 changes: 21 additions & 0 deletions samples/compute/v2/servers/stop_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->stop();
24 changes: 24 additions & 0 deletions src/Compute/v2/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,30 @@ public function rebootServer(): array
];
}

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

public function stopServer() : array
{
return [
'method' => 'POST',
'path' => 'servers/{id}/action',
'params' => [
'id' => $this->params->urlId('server'),
'os-stop' => $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 @@ -162,6 +162,28 @@ public function reboot(string $type = Enum::REBOOT_SOFT)
]);
}

/**
* Starts server
*/
public function start()
{
$this->execute($this->api->startServer(), [
'id' => $this->id,
'os-start' => null
]);
}

/**
* Stops server
*/
public function stop()
{
$this->execute($this->api->stopServer(), [
'id' => $this->id,
'os-stop' => null
]);
}

/**
* Rebuilds the server.
*
Expand Down
44 changes: 40 additions & 4 deletions tests/integration/Compute/v2/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class CoreTest extends TestCase
const SUBNET = 'phptest_subnet';
const VOLUME = 'phptest_volume';

const IMAGE = 'cirros';

/** @var NetworkService */
private $networkService;

Expand Down Expand Up @@ -79,11 +81,13 @@ private function searchImages($name)
foreach ($this->getService()->listImages() as $image) {
if (strpos($image->name, $name) !== false) {
$this->imageId = $image->id;
return;
break;
}
}

$this->logger->emergency('No image found');
if (!$this->imageId) {
throw new \RuntimeException(sprintf('Unable to find image "%s". Make sure this image is available for integration test.', $name));
}
}

protected function setUp()
Expand Down Expand Up @@ -126,7 +130,7 @@ public function runTests()
// Manually trigger setUp
$this->setUp();

$this->searchImages('cirros');
$this->searchImages(self::IMAGE);

// Servers
$this->createServer();
Expand All @@ -138,6 +142,8 @@ public function runTests()

// Server actions
//$this->changeServerPassword();
$this->stopServer();
$this->startServer();
$this->resizeServer();
$this->confirmServerResize();
$this->rebuildServer();
Expand Down Expand Up @@ -188,10 +194,16 @@ public function runTests()

private function createServer()
{
$flavorId = getenv('OS_FLAVOR');

if (!$flavorId) {
throw new \RuntimeException('OS_FLAVOR env var must be set');
}

$replacements = [
'{serverName}' => $this->randomStr(),
'{imageId}' => $this->imageId,
'{flavorId}' => 1,
'{flavorId}' => $flavorId,
'{networkId}' => $this->network->id
];

Expand Down Expand Up @@ -355,6 +367,30 @@ private function rebootServer()
$this->logStep('Rebooted server {serverId}', $replacements);
}

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

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

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

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

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

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

$server->waitUntilActive(false);

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

private function createFlavor()
{
$replacements = [
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

abstract class TestCase extends \PHPUnit_Framework_TestCase implements TestInterface
{
private $logger;
protected $logger;
private $startPoint;
private $lastPoint;
private $sampleManager;
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/run.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$rootDir = dirname(dirname(__DIR__));

Expand Down
19 changes: 18 additions & 1 deletion tests/unit/Compute/v2/Models/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use OpenStack\Compute\v2\Models\Server;
use OpenStack\Test\TestCase;
use OpenStack\Networking\v2\Extensions\SecurityGroups\Models\SecurityGroup;
use Prophecy\Argument;

class ServerTest extends TestCase
{
Expand Down Expand Up @@ -138,6 +137,24 @@ public function test_it_rebuilds()
$this->assertEquals($userOptions['name'], $this->server->name);
}

public function test_it_starts()
{
$expectedJson = ['os-start' => null];

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

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

public function test_it_stops()
{
$expectedJson = ['os-stop' => null];

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

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

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

0 comments on commit 4e06611

Please sign in to comment.