Skip to content

Commit fe3eb6d

Browse files
committed
Add server console output
1 parent b956c88 commit fe3eb6d

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,19 @@ public function revertServerResize(): array
372372
];
373373
}
374374

375+
public function getConsoleOutput(): array
376+
{
377+
return [
378+
'method' => 'POST',
379+
'path' => 'servers/{id}/action',
380+
'jsonKey' => 'os-getConsoleOutput',
381+
'params' => [
382+
'id' => $this->params->urlId('server'),
383+
'length' => $this->notRequired($this->params->consoleLogLength()),
384+
],
385+
];
386+
}
387+
375388
public function createServerImage(): array
376389
{
377390
return [

src/Compute/v2/Models/Server.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,22 @@ public function revertResize()
246246
$this->execute($this->api->revertServerResize(), ['revertResize' => null, 'id' => $this->id]);
247247
}
248248

249+
/**
250+
* Gets the console output of the server.
251+
*
252+
* @param int $length The number of lines, by default all lines will be returned.
253+
* @return string
254+
*/
255+
public function getConsoleOutput(int $length = -1)
256+
{
257+
$response = $this->execute($this->api->getConsoleOutput(), [
258+
'id' => $this->id,
259+
'length' => $length
260+
]);
261+
262+
return Utils::jsonDecode($response)['output'];
263+
}
264+
249265
/**
250266
* Gets a VNC console for a server.
251267
*

src/Compute/v2/Params.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,15 @@ public function consoleType(): array
499499
];
500500
}
501501

502+
public function consoleLogLength(): array
503+
{
504+
return [
505+
'type' => self::INT_TYPE,
506+
'location' => self::JSON,
507+
'required' => false,
508+
];
509+
}
510+
502511
protected function quotaSetLimit($sentAs, $description): array
503512
{
504513
return [

tests/integration/Compute/v2/CoreTest.php

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

704704
$this->logStep('Create interface attachment for server {serverId}', $replacements);
705705
}
706+
707+
private function getConsoleOutput()
708+
{
709+
$replacements = [
710+
'{serverId}' => $this->serverId
711+
];
712+
713+
require_once $this->sampleFile($replacements, 'servers/get_server_console_output.php');
714+
715+
$this->logStep('Get console output for server {serverId}', $replacements);
716+
}
706717
}
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ public function test_it_reverts_resizes()
209209
$this->assertNull($this->server->revertResize());
210210
}
211211

212+
public function test_it_gets_console_output()
213+
{
214+
$expectedJson = ["os-getConsoleOutput" => ["length" => 3]];
215+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], 'server-get-console-output');
216+
217+
$this->assertEquals("FAKE CONSOLE OUTPUT\nANOTHER\nLAST LINE", $this->server->getConsoleOutput(3));
218+
}
219+
212220
public function test_it_gets_vnc_console()
213221
{
214222
$type = 'novnc';

0 commit comments

Comments
 (0)