Skip to content

Commit 0cf3f30

Browse files
committed
Get console vnc/serial/rdp/spice methods
1 parent 4e06611 commit 0cf3f30

File tree

9 files changed

+211
-0
lines changed

9 files changed

+211
-0
lines changed

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 mixed
239+
*/
240+
public function getVncConsole($type = Enum::CONSOLE_NOVNC)
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 mixed
252+
*/
253+
public function getRDPConsole($type = Enum::CONSOLE_RDP_HTML5)
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 mixed
265+
*/
266+
public function getSpiceConsole($type = Enum::CONSOLE_SPICE_HTML5)
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 mixed
278+
*/
279+
public function getSerialConsole($type = Enum::CONSOLE_SERIAL)
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
}
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+
}
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": "serial",
7+
"url":"ws://127.0.0.1:6083/?token=f9906a48-b71e-4f18-baca-c987da3ebdb3"
8+
}
9+
}
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": "spice-html5",
7+
"url": "http://127.0.0.1:6082/spice_auto.html?token=a30e5d08-6a20-4043-958f-0852440c6af4"
8+
}
9+
}
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": "novnc",
7+
"url": "http://127.0.0.1:6080/vnc_auto.html?token=191996c3-7b0f-42f3-95a7-f1839f2da6ed"
8+
}
9+
}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,62 @@ public function test_it_reverts_resizes()
179179
$this->assertNull($this->server->revertResize());
180180
}
181181

182+
public function test_it_gets_vnc_console()
183+
{
184+
$type = 'novnc';
185+
$expectedJson = ['os-getVNCConsole' => ['type' => $type]];
186+
187+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], 'server-get-console-vnc');
188+
189+
$response = $this->server->getVncConsole();
190+
191+
$this->assertArrayHasKey('url', $response);
192+
$this->assertArrayHasKey('type', $response);
193+
$this->assertEquals($type, $response['type']);
194+
}
195+
196+
public function test_it_gets_rdp_console()
197+
{
198+
$type = 'rdp-html5';
199+
$expectedJson = ['os-getRDPConsole' => ['type' => $type]];
200+
201+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], 'server-get-console-rdp');
202+
203+
$response = $this->server->getRDPConsole();
204+
205+
$this->assertArrayHasKey('url', $response);
206+
$this->assertArrayHasKey('type', $response);
207+
$this->assertEquals($type, $response['type']);
208+
}
209+
210+
public function test_it_gets_spice_console()
211+
{
212+
$type = 'spice-html5';
213+
$expectedJson = ['os-getSPICEConsole' => ['type' => $type]];
214+
215+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], 'server-get-console-spice');
216+
217+
$response = $this->server->getSpiceConsole();
218+
219+
$this->assertArrayHasKey('url', $response);
220+
$this->assertArrayHasKey('type', $response);
221+
$this->assertEquals($type, $response['type']);
222+
}
223+
224+
public function test_it_gets_serial_console()
225+
{
226+
$type = 'serial';
227+
$expectedJson = ['os-getSerialConsole' => ['type' => $type]];
228+
229+
$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], 'server-get-console-serial');
230+
231+
$response = $this->server->getSerialConsole();
232+
233+
$this->assertArrayHasKey('url', $response);
234+
$this->assertArrayHasKey('type', $response);
235+
$this->assertEquals($type, $response['type']);
236+
}
237+
182238
public function test_it_creates_images()
183239
{
184240
$userData = ['name' => 'newImage', 'metadata' => ['foo' => 'bar']];

0 commit comments

Comments
 (0)