diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php index 52a2a385..67a642a2 100644 --- a/application/controllers/ConfigController.php +++ b/application/controllers/ConfigController.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Kubernetes\Controllers; +use Exception; use GuzzleHttp\Psr7\ServerRequest; use Icinga\Application\Config; use Icinga\Application\Logger; @@ -198,72 +199,84 @@ public function prometheusAction() $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; - } + $data[str_replace('.', '_', $pair->key)] = $pair->value; } $form = (new PrometheusConfigForm()) ->populate($data) - ->on(PrometheusConfigForm::ON_SUCCESS, function ($form) use ($db, $data) { + ->on(PrometheusConfigForm::ON_SUCCESS, function ($form) use ($db, $dbConfig) { if ($form->isLocked()) { Notification::error($this->translate('Prometheus configuration is locked')); return; } - if (isset($data['prometheus_url'])) { - $db->update('config', - ['value' => $form->getValue('prometheus_url')], - [$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_URL] - ); - } else { - $db->insert('config', - [ - $db->quoteIdentifier('key') => self::PROMETHEUS_URL, - 'value' => $form->getValue('prometheus_url') - ] - ); - } + try { + $db->exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE"); + $db->beginTransaction(); - 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') - ] - ); - } + $data = []; - 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') - ] - ); - } + foreach ($dbConfig as $pair) { + $data[$pair->key] = ['value' => $pair->value, 'locked' => $pair->locked]; + } - $this->redirectNow('__REFRESH__'); + if ($data[self::PROMETHEUS_URL]['locked'] !== 'y') { + if (isset($data[self::PROMETHEUS_URL])) { + $db->update('config', + ['value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_URL))], + [$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_URL] + ); + } else { + $db->insert('config', + [ + $db->quoteIdentifier('key') => self::PROMETHEUS_URL, + 'value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_URL)) + ] + ); + } + } + + if ($data[self::PROMETHEUS_USERNAME]['locked'] !== 'y') { + if (isset($data[self::PROMETHEUS_USERNAME])) { + $db->update('config', + ['value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_USERNAME))], + [$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_USERNAME] + ); + } else { + $db->insert('config', + [ + $db->quoteIdentifier('key') => self::PROMETHEUS_USERNAME, + 'value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_USERNAME)) + ] + ); + } + } + + if ($data[self::PROMETHEUS_PASSWORD]['locked'] !== 'y') { + if (isset($data[self::PROMETHEUS_PASSWORD])) { + $db->update('config', + ['value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_PASSWORD))], + [$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_PASSWORD] + ); + } else { + $db->insert('config', + [ + $db->quoteIdentifier('key') => self::PROMETHEUS_PASSWORD, + 'value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_PASSWORD)) + ] + ); + } + } + + $db->commitTransaction(); + } catch (Exception $_) { + $db->rollBackTransaction(); + Notification::error($this->translate('Failed to store new configuration')); + return; + } Notification::success($this->translate('New configuration has successfully been stored')); + $this->redirectNow('__REFRESH__'); })->handleRequest($this->getServerRequest()); $this->mergeTabs($this->Module()->getConfigTabs()->activate('prometheus')); @@ -271,6 +284,18 @@ public function prometheusAction() $this->addContent($form); } + /** + * Convert database field name to form field name + * + * @param string $field + * + * @return string + */ + protected function fieldForForm(string $field): string + { + return str_replace('.', '_', $field); + } + /** * Merge tabs with other tabs contained in this tab panel * diff --git a/application/forms/PrometheusConfigForm.php b/application/forms/PrometheusConfigForm.php index 4ab78a36..4b9c87bd 100644 --- a/application/forms/PrometheusConfigForm.php +++ b/application/forms/PrometheusConfigForm.php @@ -4,15 +4,13 @@ namespace Icinga\Module\Kubernetes\Forms; -use Icinga\Data\ResourceFactory; +use Icinga\Module\Kubernetes\Common\Database; use Icinga\Module\Kubernetes\Controllers\ConfigController; +use Icinga\Module\Kubernetes\Model\Config; use ipl\Html\Attributes; use ipl\Html\Html; -use ipl\Html\HtmlElement; -use ipl\Web\Compat\CompatForm; -use Icinga\Module\Kubernetes\Model\Config; use ipl\Stdlib\Filter; -use Icinga\Module\Kubernetes\Common\Database; +use ipl\Web\Compat\CompatForm; class PrometheusConfigForm extends CompatForm { @@ -22,7 +20,7 @@ protected function assemble(): void $this->addHtml( Html::tag('div', Attributes::create(['class' => 'control-group']), [ Html::tag( - 'div', + 'div', Attributes::create(['class' => 'control-label-group']), ), Html::tag( @@ -41,7 +39,6 @@ protected function assemble(): void 'label' => $this->translate('URL'), 'required' => true, 'disabled' => $this->isLocked(), - 'value' => '' ] ); @@ -51,7 +48,6 @@ protected function assemble(): void [ 'label' => $this->translate('Username'), 'disabled' => $this->isLocked(), - 'value' => '' ] ); @@ -61,7 +57,6 @@ protected function assemble(): void [ 'label' => $this->translate('Password'), 'disabled' => $this->isLocked(), - 'value' => '' ] ); @@ -69,7 +64,7 @@ protected function assemble(): void 'submit', 'submit', [ - 'label' => $this->translate('Save Changes'), + 'label' => $this->translate('Save Changes'), 'disabled' => $this->isLocked() ] );