diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php index e13a8818..52a2a385 100644 --- a/application/controllers/ConfigController.php +++ b/application/controllers/ConfigController.php @@ -28,6 +28,12 @@ class ConfigController extends Controller { protected bool $disableDefaultAutoRefresh = true; + public const PROMETHEUS_URL = 'prometheus.url'; + + public const PROMETHEUS_USERNAME = 'prometheus.username'; + + public const PROMETHEUS_PASSWORD = 'prometheus.password'; + public function init() { $this->assertPermission('config/modules'); @@ -181,34 +187,81 @@ function (NotificationsConfigForm $form) use ($kconfig, $sourceForm) { public function prometheusAction() { $db = Database::connection(); - $dbConfig = KConfig::on($db)->filter(Filter::equal('key', 'prometheus.url'))->first(); + $dbConfig = KConfig::on($db)->filter( + Filter::any( + Filter::equal('key', self::PROMETHEUS_URL), + Filter::equal('key', self::PROMETHEUS_USERNAME), + Filter::equal('key', self::PROMETHEUS_PASSWORD) + ) + ); + + $data = []; + + foreach ($dbConfig as $pair) { + switch ($pair->key) { + case self::PROMETHEUS_URL: + $data['prometheus_url'] = $pair->value; + break; + case self::PROMETHEUS_USERNAME: + $data['prometheus_username'] = $pair->value; + break; + case self::PROMETHEUS_PASSWORD: + $data['prometheus_password'] = $pair->value; + break; + } + } -// $config = Config::module('kubernetes'); $form = (new PrometheusConfigForm()) - ->populate(['prometheus_url' => $dbConfig->value]) - ->on(PrometheusConfigForm::ON_SUCCESS, function ($form) use ($db, $dbConfig) { + ->populate($data) + ->on(PrometheusConfigForm::ON_SUCCESS, function ($form) use ($db, $data) { if ($form->isLocked()) { Notification::error($this->translate('Prometheus configuration is locked')); return; } -// $config->setSection('prometheus', $form->getValues()); -// $config->saveIni(); - - if ($dbConfig) { + if (isset($data['prometheus_url'])) { $db->update('config', ['value' => $form->getValue('prometheus_url')], - [$db->quoteIdentifier('key') . ' = ?' => 'prometheus.url'] + [$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_URL] ); } else { $db->insert('config', [ - $db->quoteIdentifier('key') => 'prometheus.url', + $db->quoteIdentifier('key') => self::PROMETHEUS_URL, 'value' => $form->getValue('prometheus_url') ] ); } + if (isset($data['prometheus_username'])) { + $db->update('config', + ['value' => $form->getValue('prometheus_username')], + [$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_USERNAME] + ); + } else { + $db->insert('config', + [ + $db->quoteIdentifier('key') => self::PROMETHEUS_USERNAME, + 'value' => $form->getValue('prometheus_username') + ] + ); + } + + if (isset($data['prometheus_password'])) { + $db->update('config', + ['value' => $form->getValue('prometheus_password')], + [$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_PASSWORD] + ); + } else { + $db->insert('config', + [ + $db->quoteIdentifier('key') => self::PROMETHEUS_PASSWORD, + 'value' => $form->getValue('prometheus_password') + ] + ); + } + + $this->redirectNow('__REFRESH__'); Notification::success($this->translate('New configuration has successfully been stored')); })->handleRequest($this->getServerRequest()); diff --git a/application/forms/PrometheusConfigForm.php b/application/forms/PrometheusConfigForm.php index e68522ce..4ab78a36 100644 --- a/application/forms/PrometheusConfigForm.php +++ b/application/forms/PrometheusConfigForm.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Kubernetes\Forms; use Icinga\Data\ResourceFactory; +use Icinga\Module\Kubernetes\Controllers\ConfigController; use ipl\Html\Attributes; use ipl\Html\Html; use ipl\Html\HtmlElement; @@ -44,6 +45,26 @@ protected function assemble(): void ] ); + $this->addElement( + 'text', + 'prometheus_username', + [ + 'label' => $this->translate('Username'), + 'disabled' => $this->isLocked(), + 'value' => '' + ] + ); + + $this->addElement( + 'password', + 'prometheus_password', + [ + 'label' => $this->translate('Password'), + 'disabled' => $this->isLocked(), + 'value' => '' + ] + ); + $this->addElement( 'submit', 'submit', @@ -57,9 +78,11 @@ protected function assemble(): void public function isLocked(): bool { $config = Config::on(Database::connection()); - $config->filter(Filter::equal('key', 'prometheus.locked')); + $config->filter(Filter::equal('key', ConfigController::PROMETHEUS_URL)); + + $temp = $config->first(); - if (isset($config->first()->value) && $config->first()->value === 'true') { + if (isset($config->first()->locked) && $config->first()->locked === 'y') { return true; }