Skip to content

Commit

Permalink
Use serializable transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed Dec 18, 2024
1 parent 75c416d commit 528cbb4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 62 deletions.
129 changes: 77 additions & 52 deletions application/controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Icinga\Module\Kubernetes\Controllers;

use Exception;
use GuzzleHttp\Psr7\ServerRequest;
use Icinga\Application\Config;
use Icinga\Application\Logger;
Expand Down Expand Up @@ -198,79 +199,103 @@ 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'));

$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
*
Expand Down
15 changes: 5 additions & 10 deletions application/forms/PrometheusConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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(
Expand All @@ -41,7 +39,6 @@ protected function assemble(): void
'label' => $this->translate('URL'),
'required' => true,
'disabled' => $this->isLocked(),
'value' => ''
]
);

Expand All @@ -51,7 +48,6 @@ protected function assemble(): void
[
'label' => $this->translate('Username'),
'disabled' => $this->isLocked(),
'value' => ''
]
);

Expand All @@ -61,15 +57,14 @@ protected function assemble(): void
[
'label' => $this->translate('Password'),
'disabled' => $this->isLocked(),
'value' => ''
]
);

$this->addElement(
'submit',
'submit',
[
'label' => $this->translate('Save Changes'),
'label' => $this->translate('Save Changes'),
'disabled' => $this->isLocked()
]
);
Expand Down

0 comments on commit 528cbb4

Please sign in to comment.