Skip to content

Commit 4a3549b

Browse files
committed
Implement resuming and suspending of servers
1 parent 3eba351 commit 4a3549b

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
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->resume();
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->suspend();

src/Compute/v2/Api.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,30 @@ public function stopServer(): array
332332
];
333333
}
334334

335+
public function resumeServer(): array
336+
{
337+
return [
338+
'method' => 'POST',
339+
'path' => 'servers/{id}/action',
340+
'params' => [
341+
'id' => $this->params->urlId('server'),
342+
'resume' => $this->params->nullAction(),
343+
],
344+
];
345+
}
346+
347+
public function suspendServer(): array
348+
{
349+
return [
350+
'method' => 'POST',
351+
'path' => 'servers/{id}/action',
352+
'params' => [
353+
'id' => $this->params->urlId('server'),
354+
'suspend' => $this->params->nullAction(),
355+
],
356+
];
357+
}
358+
335359
public function rebuildServer(): array
336360
{
337361
return [

src/Compute/v2/Models/Server.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,28 @@ public function stop()
231231
]);
232232
}
233233

234+
/**
235+
* Resumes server.
236+
*/
237+
public function resume()
238+
{
239+
$this->execute($this->api->resumeServer(), [
240+
'id' => $this->id,
241+
'resume' => null,
242+
]);
243+
}
244+
245+
/**
246+
* Suspends server.
247+
*/
248+
public function suspend()
249+
{
250+
$this->execute($this->api->suspendServer(), [
251+
'id' => $this->id,
252+
'suspend' => null,
253+
]);
254+
}
255+
234256
/**
235257
* Rebuilds the server.
236258
*

tests/integration/Compute/v2/CoreTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,30 @@ private function startServer()
423423
$this->logStep('Started server {serverId}', $replacements);
424424
}
425425

426+
private function suspendServer()
427+
{
428+
$replacements = ['{serverId}' => $this->serverId];
429+
430+
/** @var $server \OpenStack\Compute\v2\Models\Server */
431+
require_once $this->sampleFile($replacements, 'servers/suspend_server.php');
432+
433+
$server->waitUntil('SUSPENDED', false);
434+
435+
$this->logStep('Suspended server {serverId}', $replacements);
436+
}
437+
438+
private function resumeServer()
439+
{
440+
$replacements = ['{serverId}' => $this->serverId];
441+
442+
/** @var $server \OpenStack\Compute\v2\Models\Server */
443+
require_once $this->sampleFile($replacements, 'servers/resume_server.php');
444+
445+
$server->waitUntilActive(false);
446+
447+
$this->logStep('Resumed server {serverId}', $replacements);
448+
}
449+
426450
private function createFlavor()
427451
{
428452
$replacements = [

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,24 @@ public function test_it_stops()
215215
$this->assertNull($this->server->stop());
216216
}
217217

218+
public function test_it_resumes()
219+
{
220+
$expectedJson = ['resume' => null];
221+
222+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], new Response(202));
223+
224+
$this->assertNull($this->server->resume());
225+
}
226+
227+
public function test_it_suspends()
228+
{
229+
$expectedJson = ['suspend' => null];
230+
231+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], new Response(202));
232+
233+
$this->assertNull($this->server->suspend());
234+
}
235+
218236
public function test_it_resizes()
219237
{
220238
$expectedJson = ['resize' => ['flavorRef' => 'flavorId']];

0 commit comments

Comments
 (0)