forked from PCextreme/cloudstack-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiListCommand.php
More file actions
181 lines (150 loc) · 5.24 KB
/
ApiListCommand.php
File metadata and controls
181 lines (150 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace PCextreme\Cloudstack\Console;
use PCextreme\Cloudstack\Client;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
class ApiListCommand extends Command
{
/**
* Configures the current command.
*
* @return void
*/
protected function configure()
{
$this->setName('api:list')
->setDescription('Generate API list cache.')
->setHelp("This will generate the API list cache file usign the 'listApis' command.");
}
/**
* Executes the current command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$urlApi = $this->askUrlApi($input, $output);
$apiKey = $this->askApiKey($input, $output);
$secretKey = $this->askSecretKey($input, $output);
$output->writeLn('');
$output->writeLn("<info>Processing API list. Please Wait...</info>");
$client = new Client([
'urlApi' => $urlApi,
'apiKey' => $apiKey,
'secretKey' => $secretKey,
]);
$command = 'listApis';
$method = $client->getCommandMethod($command);
$url = $client->getCommandUrl($command, []);
$request = $client->getRequest($method, $url, []);
$list = $client->getResponse($request);
// We expect an array from the getResponse method, when this returns
// with a string we most likely got a server error.
if (is_string($list)) {
throw new \RuntimeException(sprintf(
"Invalid API response, received: %s",
$list
));
}
$this->processList($output, $list);
}
/**
* Ask for URL API.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string
*/
protected function askUrlApi(InputInterface $input, OutputInterface $output)
{
$question = new Question(
'Enter target API url [default: https://api.auroracompute.eu/ams]: ',
'https://api.auroracompute.eu/ams'
);
return $this->getHelper('question')->ask($input, $output, $question);
}
/**
* Ask for API key.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string
*/
protected function askApiKey(InputInterface $input, OutputInterface $output)
{
$question = (new Question('Enter API key: '))
->setValidator(function ($answer) {
if (is_null($answer)) {
throw new \InvalidArgumentException("API key can't be null.");
}
return $answer;
})
->setMaxAttempts(2);
return $this->getHelper('question')->ask($input, $output, $question);
}
/**
* Ask for secret key.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string
*/
protected function askSecretKey(InputInterface $input, OutputInterface $output)
{
$question = (new Question('Enter secret key: '))
->setValidator(function ($answer) {
if (is_null($answer)) {
throw new \InvalidArgumentException("Secret key can't be null.");
}
return $answer;
})
->setMaxAttempts(2);
return $this->getHelper('question')->ask($input, $output, $question);
}
/**
* Dump cache file of APIs list.
*
* @param OutputInterface $output
* @param array $list
* @return void
*/
protected function processList(OutputInterface $output, array $list = [])
{
if (empty($list)) {
throw new \RuntimeException("API list is empty.");
}
$progress = new ProgressBar($output, $list['listapisresponse']['count']);
$progress->start();
$commands = [];
foreach ($list['listapisresponse']['api'] as $api) {
$commandName = $api['name'];
$command = [
'description' => $api['description'],
'isasync' => $api['isasync'],
];
$commandParams = [];
foreach ($api['params'] as $apiParam) {
$paramName = $apiParam['name'];
$param = [
'description' => $apiParam['description'],
'required' => $apiParam['required'],
'type' => $apiParam['type'],
];
$commandParams[$paramName] = $param;
}
$command['params'] = $commandParams;
$commands[$commandName] = $command;
$progress->advance();
}
$listFile = "<?php\n\n";
$listFile .= "// api_list.php @generated by api:list command\n\n";
$listFile .= "return ".var_export($commands, true).";\n";
file_put_contents(__DIR__ . '/../../cache/api_list.php', $listFile);
$progress->finish();
}
}