1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PCextreme\Cloudstack\Util; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use PCextreme\Cloudstack\Exception\ClientException; |
9
|
|
|
|
10
|
|
|
trait UrlHelpersTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Generate Client URL for specified username. |
14
|
|
|
* |
15
|
|
|
* @param string $username |
16
|
|
|
* @param string $domainId |
17
|
|
|
* @return string |
18
|
|
|
* @throws InvalidArgumentException |
19
|
|
|
*/ |
20
|
|
|
public function clientUrl(string $username, string $domainId) : string |
21
|
|
|
{ |
22
|
|
|
if (is_null($this->urlClient)) { |
|
|
|
|
23
|
|
|
throw new InvalidArgumentException( |
24
|
|
|
'Required options not defined: urlClient' |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// Prepare session. |
29
|
|
|
// Using the SSO (Single Sign On) key we can generate a sessionkey used for the console url. |
30
|
|
|
$command = 'login'; |
31
|
|
|
$params = [ |
32
|
|
|
'command' => $command, |
33
|
|
|
'username' => $username, |
34
|
|
|
'domainid' => $domainId, |
35
|
|
|
'timestamp' => round(microtime(true) * 1000), |
36
|
|
|
'response' => 'json', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
$method = $this->getCommandMethod($command); |
|
|
|
|
40
|
|
|
$query = $this->enableSso()->getCommandQuery($params); |
|
|
|
|
41
|
|
|
|
42
|
|
|
return $this->urlClient.'?loginUrl='.urlencode($query); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Generate Console URL for specified username owning the virtual machine. |
47
|
|
|
* |
48
|
|
|
* @param string $username |
49
|
|
|
* @param string $domainId |
50
|
|
|
* @param string $virtualMachineId |
51
|
|
|
* @return string |
52
|
|
|
* @throws InvalidArgumentException |
53
|
|
|
*/ |
54
|
|
|
public function consoleUrl(string $username, string $domainId, string $virtualMachineId) : string |
55
|
|
|
{ |
56
|
|
|
if (is_null($this->urlConsole)) { |
|
|
|
|
57
|
|
|
throw new InvalidArgumentException( |
58
|
|
|
'Required options not defined: urlConsole' |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Prepare session. |
63
|
|
|
// Using the SSO (Single Sign On) key we can generate a sessionkey used for the console url. |
64
|
|
|
$command = 'login'; |
65
|
|
|
$params = [ |
66
|
|
|
'command' => $command, |
67
|
|
|
'username' => $username, |
68
|
|
|
'domainid' => $domainId, |
69
|
|
|
'timestamp' => round(microtime(true) * 1000), |
70
|
|
|
'response' => 'json', |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
$base = $this->urlApi; |
|
|
|
|
74
|
|
|
$method = $this->getCommandMethod($command); |
|
|
|
|
75
|
|
|
$query = $this->enableSso()->getCommandQuery($params); |
|
|
|
|
76
|
|
|
$url = $this->appendQuery($base, $query); |
|
|
|
|
77
|
|
|
$request = $this->getRequest($method, $url); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$login = $this->getResponse($request); |
|
|
|
|
80
|
|
|
|
81
|
|
|
// Prepare a signed request for the Console url. |
82
|
|
|
// Effectively this will be the console url, it won't be requested at the Cloudstack API. |
83
|
|
|
$params = [ |
84
|
|
|
'cmd' => 'access', |
85
|
|
|
'vm' => $virtualMachineId, |
86
|
|
|
'userid' => $login['loginresponse']['userid'], |
87
|
|
|
'sessionkey' => $login['loginresponse']['sessionkey'], |
88
|
|
|
'timestamp' => round(microtime(true) * 1000), |
89
|
|
|
'apikey' => $this->apiKey, |
|
|
|
|
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$base = $this->urlConsole; |
93
|
|
|
$query = $this->getCommandQuery($params); |
|
|
|
|
94
|
|
|
|
95
|
|
|
return $this->appendQuery($base, $query); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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: