Skip to content

Commit 328705b

Browse files
authored
Merge pull request #145 from casperboone/console_output
Add server console output
2 parents 0073c76 + f85eb66 commit 328705b

File tree

7 files changed

+113
-0
lines changed

7 files changed

+113
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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(['id' => '{serverId}']);
18+
19+
/** @var string $consoleOutput */
20+
$consoleOutput = $server->getConsoleOutput();

src/Compute/v2/Api.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,31 @@ public function revertServerResize(): array
398398
];
399399
}
400400

401+
public function getConsoleOutput(): array
402+
{
403+
return [
404+
'method' => 'POST',
405+
'path' => 'servers/{id}/action',
406+
'jsonKey' => 'os-getConsoleOutput',
407+
'params' => [
408+
'id' => $this->params->urlId('server'),
409+
'length' => $this->notRequired($this->params->consoleLogLength()),
410+
],
411+
];
412+
}
413+
414+
public function getAllConsoleOutput(): array
415+
{
416+
return [
417+
'method' => 'POST',
418+
'path' => 'servers/{id}/action',
419+
'params' => [
420+
'id' => $this->params->urlId('server'),
421+
'os-getConsoleOutput' => $this->params->emptyObject(),
422+
],
423+
];
424+
}
425+
401426
public function createServerImage(): array
402427
{
403428
return [

src/Compute/v2/Models/Server.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,25 @@ public function revertResize()
285285
$this->execute($this->api->revertServerResize(), ['revertResize' => null, 'id' => $this->id]);
286286
}
287287

288+
/**
289+
* Gets the console output of the server.
290+
*
291+
* @param int $length The number of lines, by default all lines will be returned.
292+
* @return string
293+
*/
294+
public function getConsoleOutput(int $length = -1): string
295+
{
296+
$definition = $length == -1 ? $this->api->getAllConsoleOutput() : $this->api->getConsoleOutput();
297+
298+
$response = $this->execute($definition, [
299+
'os-getConsoleOutput' => new \stdClass(),
300+
'id' => $this->id,
301+
'length' => $length,
302+
]);
303+
304+
return Utils::jsonDecode($response)['output'];
305+
}
306+
288307
/**
289308
* Gets a VNC console for a server.
290309
*

src/Compute/v2/Params.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,22 @@ public function consoleType(): array
519519
];
520520
}
521521

522+
public function consoleLogLength(): array
523+
{
524+
return [
525+
'type' => self::INT_TYPE,
526+
'location' => self::JSON,
527+
'required' => false,
528+
];
529+
}
530+
531+
public function emptyObject(): array
532+
{
533+
return [
534+
'type' => self::OBJECT_TYPE,
535+
];
536+
}
537+
522538
protected function quotaSetLimit($sentAs, $description): array
523539
{
524540
return [

tests/integration/Compute/v2/CoreTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,4 +724,15 @@ private function createInterfaceAttachment()
724724

725725
$this->logStep('Create interface attachment for server {serverId}', $replacements);
726726
}
727+
728+
private function getConsoleOutput()
729+
{
730+
$replacements = [
731+
'{serverId}' => $this->serverId
732+
];
733+
734+
require_once $this->sampleFile($replacements, 'servers/get_server_console_output.php');
735+
736+
$this->logStep('Get console output for server {serverId}', $replacements);
737+
}
727738
}
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+
"output": "FAKE CONSOLE OUTPUT\nANOTHER\nLAST LINE"
6+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,22 @@ public function test_it_reverts_resizes()
239239
$this->assertNull($this->server->revertResize());
240240
}
241241

242+
public function test_it_gets_console_output()
243+
{
244+
$expectedJson = ["os-getConsoleOutput" => ["length" => 3]];
245+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], 'server-get-console-output');
246+
247+
$this->assertEquals("FAKE CONSOLE OUTPUT\nANOTHER\nLAST LINE", $this->server->getConsoleOutput(3));
248+
}
249+
250+
public function test_it_gets_all_console_output()
251+
{
252+
$expectedJson = ["os-getConsoleOutput" => new \stdClass()];
253+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], 'server-get-console-output');
254+
255+
$this->assertEquals("FAKE CONSOLE OUTPUT\nANOTHER\nLAST LINE", $this->server->getConsoleOutput());
256+
}
257+
242258
public function test_it_gets_vnc_console()
243259
{
244260
$type = 'novnc';

0 commit comments

Comments
 (0)