Skip to content

Commit a7a8a66

Browse files
author
Jamie Hannaford
authored
Merge pull request #88 from haphan/os-console
[rfr] Get console vnc/serial/rdp/spice methods
2 parents e77d320 + 315b760 commit a7a8a66

File tree

14 files changed

+305
-0
lines changed

14 files changed

+305
-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 array $console */
20+
$console = $server->getRDPConsole();
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 array $console */
20+
$console = $server->getSerialConsole();
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 array $console */
20+
$console = $server->getSpiceConsole();
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 array $console */
20+
$console = $server->getVncConsole();

src/Compute/v2/Api.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,58 @@ public function createServerImage(): array
383383
];
384384
}
385385

386+
public function getVncConsole(): array
387+
{
388+
return [
389+
'method' => 'POST',
390+
'path' => 'servers/{id}/action',
391+
'jsonKey' => 'os-getVNCConsole',
392+
'params' => [
393+
'id' => $this->params->urlId('server'),
394+
'type' => $this->params->consoleType()
395+
]
396+
];
397+
}
398+
399+
public function getSpiceConsole(): array
400+
{
401+
return [
402+
'method' => 'POST',
403+
'path' => 'servers/{id}/action',
404+
'jsonKey' => 'os-getSPICEConsole',
405+
'params' => [
406+
'id' => $this->params->urlId('server'),
407+
'type' => $this->params->consoleType()
408+
]
409+
];
410+
}
411+
412+
public function getSerialConsole(): array
413+
{
414+
return [
415+
'method' => 'POST',
416+
'path' => 'servers/{id}/action',
417+
'jsonKey' => 'os-getSerialConsole',
418+
'params' => [
419+
'id' => $this->params->urlId('server'),
420+
'type' => $this->params->consoleType()
421+
]
422+
];
423+
}
424+
425+
public function getRDPConsole(): array
426+
{
427+
return [
428+
'method' => 'POST',
429+
'path' => 'servers/{id}/action',
430+
'jsonKey' => 'os-getRDPConsole',
431+
'params' => [
432+
'id' => $this->params->urlId('server'),
433+
'type' => $this->params->consoleType()
434+
]
435+
];
436+
}
437+
386438
public function getAddresses(): array
387439
{
388440
return [

src/Compute/v2/Enum.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ abstract class Enum
1111
{
1212
const REBOOT_SOFT = 'SOFT';
1313
const REBOOT_HARD = 'HARD';
14+
const CONSOLE_NOVNC = 'novnc';
15+
const CONSOLE_XVPNC = 'xvpvnc';
16+
const CONSOLE_RDP_HTML5 = 'rdp-html5';
17+
const CONSOLE_SPICE_HTML5 = 'spice-html5';
18+
const CONSOLE_SERIAL = 'serial';
1419
}

src/Compute/v2/Models/Server.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,59 @@ public function revertResize()
229229
$this->execute($this->api->revertServerResize(), ['revertResize' => null, 'id' => $this->id]);
230230
}
231231

232+
/**
233+
* Gets a VNC console for a server.
234+
*
235+
* @param string $type The type of VNC console: novnc|xvpvnc.
236+
* Defaults to novnc.
237+
*
238+
* @return array
239+
*/
240+
public function getVncConsole($type = Enum::CONSOLE_NOVNC): array
241+
{
242+
$response = $this->execute($this->api->getVncConsole(), ['id' => $this->id, 'type' => $type]);
243+
return Utils::jsonDecode($response)['console'];
244+
}
245+
246+
/**
247+
* Gets a RDP console for a server.
248+
*
249+
* @param string $type The type of VNC console: rdp-html5 (default).
250+
*
251+
* @return array
252+
*/
253+
public function getRDPConsole($type = Enum::CONSOLE_RDP_HTML5): array
254+
{
255+
$response = $this->execute($this->api->getRDPConsole(), ['id' => $this->id, 'type' => $type]);
256+
return Utils::jsonDecode($response)['console'];
257+
}
258+
259+
/**
260+
* Gets a Spice console for a server.
261+
*
262+
* @param string $type The type of VNC console: spice-html5.
263+
*
264+
* @return array
265+
*/
266+
public function getSpiceConsole($type = Enum::CONSOLE_SPICE_HTML5): array
267+
{
268+
$response = $this->execute($this->api->getSpiceConsole(), ['id' => $this->id, 'type' => $type]);
269+
return Utils::jsonDecode($response)['console'];
270+
}
271+
272+
/**
273+
* Gets a serial console for a server.
274+
*
275+
* @param string $type The type of VNC console: serial.
276+
*
277+
* @return array
278+
*/
279+
public function getSerialConsole($type = Enum::CONSOLE_SERIAL): array
280+
{
281+
$response = $this->execute($this->api->getSerialConsole(), ['id' => $this->id, 'type' => $type]);
282+
return Utils::jsonDecode($response)['console'];
283+
}
284+
232285
/**
233286
* Creates an image for the current server.
234287
*

src/Compute/v2/Params.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,13 @@ public function attachmentId(): array
445445
'required' => true,
446446
];
447447
}
448+
449+
public function consoleType(): array
450+
{
451+
return [
452+
'type' => self::STRING_TYPE,
453+
'location' => self::JSON,
454+
'required' => true
455+
];
456+
}
448457
}

tests/integration/Compute/v2/CoreTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ public function runTests()
180180
// Limits
181181
$this->getLimits();
182182
$this->getHypervisorsStatistics();
183+
184+
// Console
185+
$this->getVncConsole();
183186
} finally {
184187
// Teardown
185188
$this->deleteServer();
@@ -647,4 +650,15 @@ private function detachVolumeFromServer()
647650

648651
$this->logStep('Detached volume attachments for server {serverId}', $replacements);
649652
}
653+
654+
private function getVncConsole()
655+
{
656+
$replacements = [
657+
'{serverId}' => $this->serverId
658+
];
659+
660+
require_once $this->sampleFile($replacements, 'servers/get_server_vnc_console.php');
661+
662+
$this->logStep('Get VNC console for server {serverId}', $replacements);
663+
}
650664
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
HTTP/1.1 200 Accepted
2+
Content-Type: application/json
3+
4+
{
5+
"console": {
6+
"type": "rdp-html5",
7+
"url": "http://127.0.0.1:6083/?token=191996c3-7b0f-42f3-95a7-f1839f2da6ed"
8+
}
9+
}

0 commit comments

Comments
 (0)