diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php index fb1848f4..782b6ca7 100644 --- a/application/controllers/ConfigController.php +++ b/application/controllers/ConfigController.php @@ -6,10 +6,14 @@ use Icinga\Application\Config; use Icinga\Module\Kubernetes\Forms\DatabaseConfigForm; +use Icinga\Module\Kubernetes\Forms\PrometheusConfigForm; use Icinga\Module\Kubernetes\Web\Controller; +use Icinga\Module\Kubernetes\Common\Database; +use Icinga\Module\Kubernetes\Model\Config as KConfig; use Icinga\Web\Notification; use Icinga\Web\Widget\Tab; use Icinga\Web\Widget\Tabs; +use ipl\Stdlib\Filter; class ConfigController extends Controller { @@ -37,6 +41,46 @@ public function databaseAction() $this->addContent($form); } + public function prometheusAction() + { + $db = Database::connection(); + $dbConfig = KConfig::on($db)->filter(Filter::equal('key', 'prometheus.url'))->first(); + +// $config = Config::module('kubernetes'); + $form = (new PrometheusConfigForm()) + ->populate(['prometheus_url' => $dbConfig->value]) + ->on(PrometheusConfigForm::ON_SUCCESS, function ($form) use ($db, $dbConfig) { + if ($form->isLocked()) { + Notification::error($this->translate('Prometheus configuration is locked')); + return; + } + +// $config->setSection('prometheus', $form->getValues()); +// $config->saveIni(); + + if ($dbConfig) { + $db->update('config', + ['value' => $form->getValue('prometheus_url')], + [$db->quoteIdentifier('key') . ' = ?' => 'prometheus.url'] + ); + } else { + $db->insert('config', + [ + $db->quoteIdentifier('key') => 'prometheus.url', + 'value' => $form->getValue('prometheus_url') + ] + ); + } + + + Notification::success($this->translate('New configuration has successfully been stored')); + })->handleRequest($this->getServerRequest()); + + $this->mergeTabs($this->Module()->getConfigTabs()->activate('prometheus')); + + $this->addContent($form); + } + /** * Merge tabs with other tabs contained in this tab panel * diff --git a/application/forms/PrometheusConfigForm.php b/application/forms/PrometheusConfigForm.php new file mode 100644 index 00000000..776f9db7 --- /dev/null +++ b/application/forms/PrometheusConfigForm.php @@ -0,0 +1,68 @@ +isLocked()) { + $this->addHtml( + Html::tag('div', Attributes::create(['class' => 'control-group']), [ + Html::tag( + 'div', + Attributes::create(['class' => 'control-label-group']), + ), + Html::tag( + 'p', + Attributes::create(), + "Prometheus configuration is provided via YAML." + ) + ]) + ); + } + + $this->addElement( + 'text', + 'prometheus_url', + [ + 'label' => $this->translate('URL'), + 'required' => true, + 'disabled' => $this->isLocked(), + 'value' => '' + ] + ); + + $this->addElement( + 'submit', + 'submit', + [ + 'label' => $this->translate('Save Changes'), + 'disabled' => $this->isLocked() + ] + ); + } + + public function isLocked(): bool + { + $config = Config::on(Database::connection()); + $config->filter(Filter::equal('key', 'prometheus.locked')); + + if ($config->first()->value === 'true') { + return true; + } + + return false; + } +} diff --git a/configuration.php b/configuration.php index 917836ec..754b52ce 100644 --- a/configuration.php +++ b/configuration.php @@ -177,6 +177,14 @@ ] ); +$this->provideConfigTab( + 'prometheus', + [ + 'title' => $this->translate('Prometheus'), + 'label' => $this->translate('Prometheus'), + 'url' => 'config/prometheus' + ] +); $this->provideJsFile('vendor/chart.umd.js');