-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathInfluxDbConnectionForm.php
269 lines (234 loc) · 8.05 KB
/
InfluxDbConnectionForm.php
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace Icinga\Module\Vspheredb\Web\Form;
use gipfl\Translation\TranslationHelper;
use gipfl\Web\Form;
use gipfl\Web\Form\Element\TextWithActionButton;
use Icinga\Module\Vspheredb\Daemon\RemoteClient;
use ipl\Html\FormElement\SelectElement;
use React\EventLoop\LoopInterface;
use function Clue\React\Block\await;
class InfluxDbConnectionForm extends Form
{
use TranslationHelper;
const INFLUXDB_MIN_SUPPORTED_VERSION = '1.6.0';
/** @var LoopInterface */
protected $loop;
protected $detectedApiVersion;
protected $influxDbVersion;
protected $baseUrlElement;
/**
* @var RemoteClient
*/
protected $client;
protected $checkedNow = false;
public function __construct(LoopInterface $loop, RemoteClient $client)
{
$this->loop = $loop;
$this->client = $client;
}
public function assemble()
{
$this->addHidden('checked_url', ['ignore' => true]);
$this->addHidden('checked_api_version', ['ignore' => true]);
$this->baseUrlElement = new TextWithActionButton('base_url', [
'label' => $this->translate('Base URL'),
'description' => $this->translate('InfluxDB base URL, like http://influxdb.example.com:8086'),
'required' => true,
], [
'label' => $this->translate('Verify'),
'title' => $this->translate('Attempt to establish a connection to your InfluxDB instance')
]);
$this->baseUrlElement->addToForm($this);
$this->addElement('select', 'api_version', [
'label' => $this->translate('API Version'),
'class' => 'autosubmit',
'description' => $this->translate(
'InfluxDB API version, autodetect should work fine'
),
'options' => [
null => $this->translate('Autodetect'),
'v1' => 'v1',
'v2' => 'v2',
],
]);
$this->appendVersionInformation($this->getDetectedApiVersion(), $this->getInfluxDbVersion());
$this->addCredentials();
}
protected function addCredentials()
{
if ($this->getApiVersion() === 'v2') {
$this->addV2Credentials();
} else {
$this->addV1Credentials();
}
$this->validateCredentials();
return $this;
}
protected function validateCredentials()
{
if (!$this->checkedNow) {
return;
}
$username = $this->getValue('username');
if (empty($username)) {
return;
}
try {
if ($this->tryCredentials(
$this->getValue('base_url'),
$this->getValue('apiVersion'),
$username,
$this->getValue('password')
)) {
$this->getElement('password')->getAttributes()->add('class', 'validated');
}
} catch (\Exception $e) {
$this->getElement('password')->addMessage($this->getExceptionMessageWithoutPhpFile($e));
}
}
protected function getExceptionMessageWithoutPhpFile(\Exception $e)
{
return preg_replace('/\sin\s.+?\.php\(\d+\)/', '', $e->getMessage());
}
protected function remoteRequest($request, $params = [])
{
return await($this->client->request($request, $params), $this->loop, 5);
}
protected function getDetectedApiVersion()
{
if ($this->detectedApiVersion === null) {
$this->detectedApiVersion = $this->getApiVersionForVersionString(
$this->getInfluxDbVersion()
);
}
return $this->detectedApiVersion;
}
protected function getApiVersion()
{
return $this->getValue('api_version', $this->getDetectedApiVersion());
}
protected function getInfluxDbVersion()
{
if ($this->influxDbVersion === null) {
$element = $this->getUrlElement();
if ($element->getButton()->hasBeenPressed()) {
$this->influxDbVersion = $this->detectInfluxDbVersion($this->getValue('base_url'));
} elseif ($this->autodetectIsUpToDate()) {
$this->markUrlAsValidated();
$this->influxDbVersion = $this->getValue('checked_api_version');
}
}
return $this->influxDbVersion;
}
/**
* @return TextWithActionButton
*/
protected function getUrlElement()
{
return $this->baseUrlElement;
}
protected function markUrlAsValidated()
{
$this
->getUrlElement()
->getElement()
->addAttributes(['class' => 'validated']);
return $this;
}
protected function autodetectIsUpToDate()
{
$baseUrl = $this->getValue('base_url');
return $baseUrl && $this->getValue('checked_url') === $baseUrl;
}
protected function appendVersionInformation($apiVersion, $detectedVersion)
{
if (empty($apiVersion) || empty($detectedVersion)) {
return;
}
$element = $this->getElement('api_version');
assert($element instanceof SelectElement);
$autoOption = $element->getOption(null);
$autoOption->setLabel(\sprintf(
$this->translate('Autodetect: %s API, Version is %s'),
$apiVersion,
$detectedVersion
));
$selectedOption = $element->getOption($apiVersion);
$selectedOption->setLabel(\sprintf(
$this->translate('%s (detected %s)'),
$apiVersion,
$detectedVersion
));
// $element->setValue($apiVersion);
}
protected function addV1Credentials()
{
$this->addElement('text', 'username', [
'label' => $this->translate('Username'),
]);
$this->addElement('password', 'password', [
'label' => $this->translate('Password'),
'required' => $this->hasElementValue('username'),
]);
}
protected function addV2Credentials()
{
$this->addElement('text', 'username', [
'label' => $this->translate('Organisation'),
'required' => true,
]);
$this->addElement('text', 'password', [
'label' => $this->translate('Token'),
// 'description' => $this->translate('InfluxDB Token (InfluxDB -> Data -> Tokens'),
'required' => true,
]);
}
protected function detectInfluxDbVersion($baseUrl)
{
if ($this->getValue('base_url') === null) {
return null;
}
try {
$version = $this->remoteRequest('influxdb.discoverVersion', [
'baseUrl' => $baseUrl,
]);
$version = ltrim($version, 'v');
if ($this->versionIsFine($version)) {
$this->checkedNow = true;
$this->setCheckedApiVersionFor($baseUrl, $version);
$this->markUrlAsValidated();
return $version;
} else {
throw new \Exception("Version $version is not supported");
}
} catch (\Exception $e) {
$this->triggerElementError('base_url', $e->getMessage());
return false;
}
}
protected function tryCredentials($baseUrl, $apiVersion, $username, $password)
{
return $this->remoteRequest('influxdb.testConnection', [
'baseUrl' => $baseUrl,
'apiVersion' => $apiVersion,
'username' => $username,
'password' => $password,
]);
}
protected function setCheckedApiVersionFor($baseUrl, $version)
{
$this->getElement('checked_url')->setValue($baseUrl);
$this->setElementValue('checked_api_version', $version);
}
protected function versionIsFine($version)
{
return \version_compare($version, static::INFLUXDB_MIN_SUPPORTED_VERSION, 'ge');
}
protected function getApiVersionForVersionString($version)
{
if ($version === null) {
return null;
}
return \version_compare($version, '1.999.999', 'gt') ? 'v2' : 'v1';
}
}