Skip to content

Commit 4e06611

Browse files
author
Jamie Hannaford
authored
Merge pull request #75 from haphan/server-action
[rfr] Server action
2 parents b5ace79 + 5e89b13 commit 4e06611

File tree

8 files changed

+149
-6
lines changed

8 files changed

+149
-6
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
$openstack = new OpenStack\OpenStack([
6+
'authUrl' => '{authUrl}',
7+
'region' => '{region}',
8+
'user' => [
9+
'id' => '{userId}',
10+
'password' => '{password}'
11+
],
12+
'scope' => ['project' => ['id' => '{projectId}']]
13+
]);
14+
15+
$compute = $openstack->computeV2(['region' => '{region}']);
16+
17+
$server = $compute->getServer([
18+
'id' => '{serverId}',
19+
]);
20+
21+
$server->start();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
$openstack = new OpenStack\OpenStack([
6+
'authUrl' => '{authUrl}',
7+
'region' => '{region}',
8+
'user' => [
9+
'id' => '{userId}',
10+
'password' => '{password}'
11+
],
12+
'scope' => ['project' => ['id' => '{projectId}']]
13+
]);
14+
15+
$compute = $openstack->computeV2(['region' => '{region}']);
16+
17+
$server = $compute->getServer([
18+
'id' => '{serverId}',
19+
]);
20+
21+
$server->stop();

src/Compute/v2/Api.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,30 @@ public function rebootServer(): array
289289
];
290290
}
291291

292+
public function startServer() : array
293+
{
294+
return [
295+
'method' => 'POST',
296+
'path' => 'servers/{id}/action',
297+
'params' => [
298+
'id' => $this->params->urlId('server'),
299+
'os-start' => $this->params->nullAction()
300+
],
301+
];
302+
}
303+
304+
public function stopServer() : array
305+
{
306+
return [
307+
'method' => 'POST',
308+
'path' => 'servers/{id}/action',
309+
'params' => [
310+
'id' => $this->params->urlId('server'),
311+
'os-stop' => $this->params->nullAction()
312+
],
313+
];
314+
}
315+
292316
public function rebuildServer(): array
293317
{
294318
return [

src/Compute/v2/Models/Server.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,28 @@ public function reboot(string $type = Enum::REBOOT_SOFT)
162162
]);
163163
}
164164

165+
/**
166+
* Starts server
167+
*/
168+
public function start()
169+
{
170+
$this->execute($this->api->startServer(), [
171+
'id' => $this->id,
172+
'os-start' => null
173+
]);
174+
}
175+
176+
/**
177+
* Stops server
178+
*/
179+
public function stop()
180+
{
181+
$this->execute($this->api->stopServer(), [
182+
'id' => $this->id,
183+
'os-stop' => null
184+
]);
185+
}
186+
165187
/**
166188
* Rebuilds the server.
167189
*

tests/integration/Compute/v2/CoreTest.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class CoreTest extends TestCase
2323
const SUBNET = 'phptest_subnet';
2424
const VOLUME = 'phptest_volume';
2525

26+
const IMAGE = 'cirros';
27+
2628
/** @var NetworkService */
2729
private $networkService;
2830

@@ -79,11 +81,13 @@ private function searchImages($name)
7981
foreach ($this->getService()->listImages() as $image) {
8082
if (strpos($image->name, $name) !== false) {
8183
$this->imageId = $image->id;
82-
return;
84+
break;
8385
}
8486
}
8587

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

8993
protected function setUp()
@@ -126,7 +130,7 @@ public function runTests()
126130
// Manually trigger setUp
127131
$this->setUp();
128132

129-
$this->searchImages('cirros');
133+
$this->searchImages(self::IMAGE);
130134

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

139143
// Server actions
140144
//$this->changeServerPassword();
145+
$this->stopServer();
146+
$this->startServer();
141147
$this->resizeServer();
142148
$this->confirmServerResize();
143149
$this->rebuildServer();
@@ -188,10 +194,16 @@ public function runTests()
188194

189195
private function createServer()
190196
{
197+
$flavorId = getenv('OS_FLAVOR');
198+
199+
if (!$flavorId) {
200+
throw new \RuntimeException('OS_FLAVOR env var must be set');
201+
}
202+
191203
$replacements = [
192204
'{serverName}' => $this->randomStr(),
193205
'{imageId}' => $this->imageId,
194-
'{flavorId}' => 1,
206+
'{flavorId}' => $flavorId,
195207
'{networkId}' => $this->network->id
196208
];
197209

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

370+
private function stopServer()
371+
{
372+
$replacements = ['{serverId}' => $this->serverId];
373+
374+
/** @var $server \OpenStack\Compute\v2\Models\Server */
375+
require_once $this->sampleFile($replacements, 'servers/stop_server.php');
376+
377+
$server->waitUntil('SHUTOFF', false);
378+
379+
$this->logStep('Stopped server {serverId}', $replacements);
380+
}
381+
382+
private function startServer()
383+
{
384+
$replacements = ['{serverId}' => $this->serverId];
385+
386+
/** @var $server \OpenStack\Compute\v2\Models\Server */
387+
require_once $this->sampleFile($replacements, 'servers/start_server.php');
388+
389+
$server->waitUntilActive(false);
390+
391+
$this->logStep('Started server {serverId}', $replacements);
392+
}
393+
358394
private function createFlavor()
359395
{
360396
$replacements = [

tests/integration/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
abstract class TestCase extends \PHPUnit_Framework_TestCase implements TestInterface
99
{
10-
private $logger;
10+
protected $logger;
1111
private $startPoint;
1212
private $lastPoint;
1313
private $sampleManager;

tests/integration/run.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
ini_set('display_errors', 1);
3+
error_reporting(E_ALL);
24

35
$rootDir = dirname(dirname(__DIR__));
46

tests/unit/Compute/v2/Models/ServerTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use OpenStack\Compute\v2\Models\Server;
1010
use OpenStack\Test\TestCase;
1111
use OpenStack\Networking\v2\Extensions\SecurityGroups\Models\SecurityGroup;
12-
use Prophecy\Argument;
1312

1413
class ServerTest extends TestCase
1514
{
@@ -138,6 +137,24 @@ public function test_it_rebuilds()
138137
$this->assertEquals($userOptions['name'], $this->server->name);
139138
}
140139

140+
public function test_it_starts()
141+
{
142+
$expectedJson = ['os-start' => null];
143+
144+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], new Response(202));
145+
146+
$this->assertNull($this->server->start());
147+
}
148+
149+
public function test_it_stops()
150+
{
151+
$expectedJson = ['os-stop' => null];
152+
153+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], new Response(202));
154+
155+
$this->assertNull($this->server->stop());
156+
}
157+
141158
public function test_it_resizes()
142159
{
143160
$expectedJson = ['resize' => ['flavorRef' => 'flavorId']];

0 commit comments

Comments
 (0)