Skip to content

Commit 8ed20b6

Browse files
committed
Add server rescue and unrescue
1 parent 50b4657 commit 8ed20b6

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->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
@@ -214,6 +214,28 @@ public function rebuild(array $options)
214214
$this->populateFromResponse($response);
215215
}
216216

217+
/**
218+
* Rescues the server.
219+
*
220+
* @param array $options {@see \OpenStack\Compute\v2\Api::rescueServer}
221+
* @return string
222+
*/
223+
public function rescue(array $options): string
224+
{
225+
$options['id'] = $this->id;
226+
$response = $this->execute($this->api->rescueServer(), $options);
227+
228+
return Utils::jsonDecode($response)['adminPass'];
229+
}
230+
231+
/**
232+
* Unrescues the server.
233+
*/
234+
public function unrescue()
235+
{
236+
$this->execute($this->api->unrescueServer(), ['unrescue' => null, 'id' => $this->id]);
237+
}
238+
217239
/**
218240
* Resizes the server to a new flavor. Once this operation is complete and server has transitioned
219241
* 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

@@ -363,6 +364,26 @@ private function rebuildServer()
363364
$this->logStep('Rebuilt server {serverId}', $replacements);
364365
}
365366

367+
private function rescueServer()
368+
{
369+
$replacements = [
370+
'{serverId}' => $this->serverId,
371+
'{imageId}' => $this->imageId,
372+
'{adminPass}' => $this->adminPass,
373+
];
374+
375+
/** @var $server \OpenStack\Compute\v2\Models\Server */
376+
require_once $this->sampleFile($replacements, 'servers/rescue_server.php');
377+
378+
$server->waitUntil('RESCUE');
379+
380+
require_once $this->sampleFile($replacements, 'servers/unrescue_server.php');
381+
382+
$server->waitUntilActive();
383+
384+
$this->logStep('Rescued server {serverId}', $replacements);
385+
}
386+
366387
private function rebootServer()
367388
{
368389
$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)