Skip to content

Commit c821655

Browse files
authored
Merge pull request #169 from casperboone/rescue
Add server rescue and unrescue
2 parents 8d58892 + f137498 commit c821655

File tree

9 files changed

+162
-0
lines changed

9 files changed

+162
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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->rescue([
22+
'imageId' => '{imageId}',
23+
'adminPass' => '{adminPass}',
24+
]);
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->unrescue();

src/Compute/v2/Api.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,32 @@ public function rebuildServer(): array
335335
];
336336
}
337337

338+
public function rescueServer(): array
339+
{
340+
return [
341+
'method' => 'POST',
342+
'path' => 'servers/{id}/action',
343+
'jsonKey' => 'rescue',
344+
'params' => [
345+
'id' => $this->params->urlId('server'),
346+
'imageId' => $this->params->rescueImageId(),
347+
'adminPass' => $this->notRequired($this->params->password()),
348+
],
349+
];
350+
}
351+
352+
public function unrescueServer(): array
353+
{
354+
return [
355+
'method' => 'POST',
356+
'path' => 'servers/{id}/action',
357+
'params' => [
358+
'id' => $this->params->urlId('server'),
359+
'unrescue' => $this->params->nullAction(),
360+
],
361+
];
362+
}
363+
338364
public function resizeServer(): array
339365
{
340366
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 rebuild(array $options)
231231
$this->populateFromResponse($response);
232232
}
233233

234+
/**
235+
* Rescues the server.
236+
*
237+
* @param array $options {@see \OpenStack\Compute\v2\Api::rescueServer}
238+
* @return string
239+
*/
240+
public function rescue(array $options): string
241+
{
242+
$options['id'] = $this->id;
243+
$response = $this->execute($this->api->rescueServer(), $options);
244+
245+
return Utils::jsonDecode($response)['adminPass'];
246+
}
247+
248+
/**
249+
* Unrescues the server.
250+
*/
251+
public function unrescue()
252+
{
253+
$this->execute($this->api->unrescueServer(), ['unrescue' => null, 'id' => $this->id]);
254+
}
255+
234256
/**
235257
* Resizes the server to a new flavor. Once this operation is complete and server has transitioned
236258
* to an active state, you will either need to call {@see confirmResize()} or {@see revertResize()}.

src/Compute/v2/Params.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ public function imageId(): array
120120
];
121121
}
122122

123+
public function rescueImageId(): array
124+
{
125+
return [
126+
'type' => self::STRING_TYPE,
127+
'required' => true,
128+
'sentAs' => 'rescue_image_ref',
129+
'description' => 'The image reference to use to rescue your server instance. Specify the image reference by ID or full URL. If you omit an image reference, default is the base image reference',
130+
];
131+
}
132+
123133
public function flavorId(): array
124134
{
125135
return [

tests/integration/Compute/v2/CoreTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public function runTests()
148148
$this->resizeServer();
149149
$this->confirmServerResize();
150150
$this->rebuildServer();
151+
$this->rescueServer();
151152
$this->createServerImage();
152153
$this->rebootServer();
153154

@@ -366,6 +367,26 @@ private function rebuildServer()
366367
$this->logStep('Rebuilt server {serverId}', $replacements);
367368
}
368369

370+
private function rescueServer()
371+
{
372+
$replacements = [
373+
'{serverId}' => $this->serverId,
374+
'{imageId}' => $this->imageId,
375+
'{adminPass}' => $this->adminPass,
376+
];
377+
378+
/** @var $server \OpenStack\Compute\v2\Models\Server */
379+
require_once $this->sampleFile($replacements, 'servers/rescue_server.php');
380+
381+
$server->waitUntil('RESCUE');
382+
383+
require_once $this->sampleFile($replacements, 'servers/unrescue_server.php');
384+
385+
$server->waitUntilActive();
386+
387+
$this->logStep('Rescued server {serverId}', $replacements);
388+
}
389+
369390
private function rebootServer()
370391
{
371392
$replacements = ['{serverId}' => $this->serverId];
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
HTTP/1.1 200 Accepted
2+
Content-Type: application/json
3+
4+
{
5+
"adminPass": "foo"
6+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
HTTP/1.1 202 Accepted
2+
Content-Type: application/json

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,36 @@ public function test_it_rebuilds()
167167
$this->assertEquals($userOptions['name'], $this->server->name);
168168
}
169169

170+
public function test_it_rescues()
171+
{
172+
$userOptions = [
173+
'imageId' => 'newImage',
174+
'adminPass' => 'foo',
175+
];
176+
177+
$expectedJson = [
178+
'rescue' => [
179+
'rescue_image_ref' => $userOptions['imageId'],
180+
'adminPass' => $userOptions['adminPass']
181+
]
182+
];
183+
184+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], 'server-rescue');
185+
186+
$adminPass = $this->server->rescue($userOptions);
187+
188+
$this->assertEquals('foo', $adminPass);
189+
}
190+
191+
public function test_it_unrescues()
192+
{
193+
$expectedJson = ['unrescue' => null];
194+
195+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], 'server-unrescue');
196+
197+
$this->assertNull($this->server->unrescue());
198+
}
199+
170200
public function test_it_starts()
171201
{
172202
$expectedJson = ['os-start' => null];

0 commit comments

Comments
 (0)