1
|
|
|
<?php namespace PCextreme\Cloudstack\Util; |
2
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
4
|
|
|
use PCextreme\Cloudstack\Exception\ClientException; |
5
|
|
|
|
6
|
|
|
trait UrlHelpersTrait |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Generate Console URL for specified username owning the virtual machine. |
10
|
|
|
* |
11
|
|
|
* @param string $username |
12
|
|
|
* @param string $domainId |
13
|
|
|
* @param string $virtualMachineId |
14
|
|
|
* @return string |
15
|
|
|
* @throws \InvalidArgumentEception |
16
|
|
|
*/ |
17
|
|
|
public function consoleUrl($username, $domainId, $virtualMachineId) |
18
|
|
|
{ |
19
|
|
|
if (is_null($this->urlConsole)) { |
|
|
|
|
20
|
|
|
throw new InvalidArgumentException( |
21
|
|
|
'Required options not defined: urlConsole' |
22
|
|
|
); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// Prepare session. |
26
|
|
|
// Using the SSO (Single Sign On) key we can generate a sessionkey used for the console url. |
27
|
|
|
$command = 'login'; |
28
|
|
|
$params = [ |
29
|
|
|
'command' => $command, |
30
|
|
|
'username' => $username, |
31
|
|
|
'domainid' => $domainId, |
32
|
|
|
'response' => 'json', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
$base = $this->urlApi; |
|
|
|
|
36
|
|
|
$method = $this->getCommandMethod($command); |
|
|
|
|
37
|
|
|
$query = $this->enableSso()->getCommandQuery($params); |
|
|
|
|
38
|
|
|
$url = $this->appendQuery($base, $query); |
|
|
|
|
39
|
|
|
$request = $this->getRequest($method, $url); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$login = $this->getResponse($request); |
|
|
|
|
42
|
|
|
|
43
|
|
|
// Prepare a signed request for the Console url. |
44
|
|
|
// Effectively this will be the console url, it won't be requested at the Cloudstack API. |
45
|
|
|
$params = [ |
46
|
|
|
'cmd' => 'access', |
47
|
|
|
'vm' => $virtualMachineId, |
48
|
|
|
'userid' => $login['loginresponse']['userid'], |
49
|
|
|
'sessionkey' => $login['loginresponse']['sessionkey'], |
50
|
|
|
'timestamp' => round(microtime(true) * 1000), |
51
|
|
|
'apikey' => $this->apiKey, |
|
|
|
|
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
$base = $this->urlConsole; |
55
|
|
|
$query = $this->getCommandQuery($params); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return $this->appendQuery($base, $query); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: