Introduce CleanupNodeForm action

This helps to clean orphaned (missing) nodes
This commit is contained in:
Sukhwinder Dhillon 2022-09-27 15:53:51 +02:00
parent 1aba349be5
commit 21c1949b07
2 changed files with 117 additions and 1 deletions

View file

@ -120,7 +120,7 @@ class ProcessController extends Controller
$missing = array_slice($missing, 0, 10);
$missing[] = '...';
}
$bp->addError('There are %d missing nodes: %s', $count, implode(', ', $missing));
$bp->addError('There are %d missing nodes: %s ', $count, implode(', ', $missing));
}
$this->content()->add($this->showHints($bp));
$this->content()->add($this->showWarnings($bp));
@ -232,6 +232,12 @@ class ProcessController extends Controller
->setParentNode($node)
->setSession($this->session())
->handleRequest();
} elseif ($action === 'cleanup' && $canEdit) {
$form = $this->loadForm('CleanupNode')
->setSuccessUrl(Url::fromRequest()->without('action'))
->setProcess($bp)
->setSession($this->session())
->handleRequest();
} elseif ($action === 'editmonitored' && $canEdit) {
$form = $this->loadForm('EditNode')
->setSuccessUrl(Url::fromRequest()->without('action'))
@ -324,6 +330,23 @@ class ProcessController extends Controller
{
$ul = Html::tag('ul', ['class' => 'error']);
foreach ($bp->getErrors() as $error) {
if (strpos($error, 'missing nodes')) {
$error = [
$error,
Html::tag(
'a',
[
'href' => Url::fromPath('businessprocess/process/show')
->setParams(
$this->getRequest()->getUrl()->getParams()
->add('action', 'cleanup')
)
],
$this->translate('Cleanup')
)
];
}
$ul->add(Html::tag('li')->setContent($error));
}
if ($bp->hasChanges()) {

View file

@ -0,0 +1,93 @@
<?php
namespace Icinga\Module\Businessprocess\Forms;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Modification\ProcessChanges;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Session\SessionNamespace;
use ipl\Html\FormattedString;
use ipl\Html\Html;
use ipl\Sql\Connection as IcingaDbConnection;
class CleanupNodeForm extends QuickForm
{
/** @var MonitoringBackend|IcingaDbConnection */
protected $backend;
/** @var BpConfig */
protected $bp;
/** @var SessionNamespace */
protected $session;
public function setup()
{
$this->addHtml(Html::tag('h2', $this->translate('Cleanup missing nodes')));
$this->addElement('checkbox', 'cleanup_all', [
'class' => 'autosubmit',
'label' => $this->translate('Cleanup all missing nodes'),
'description' => $this->translate('Remove all missing nodes from config')
]);
if ($this->getSentValue('cleanup_all') !== '1') {
$this->addElement('multiselect', 'nodes', [
'label' => $this->translate('Select nodes to cleanup'),
'required' => true,
'size' => 8,
'multiOptions' => $this->bp->getMissingChildren()
]);
}
}
/**
* @param MonitoringBackend|IcingaDbConnection $backend
* @return $this
*/
public function setBackend($backend)
{
$this->backend = $backend;
return $this;
}
/**
* @param BpConfig $process
* @return $this
*/
public function setProcess(BpConfig $process)
{
$this->bp = $process;
$this->setBackend($process->getBackend());
return $this;
}
/**
* @param SessionNamespace $session
* @return $this
*/
public function setSession(SessionNamespace $session)
{
$this->session = $session;
return $this;
}
public function onSuccess()
{
$changes = ProcessChanges::construct($this->bp, $this->session);
$nodesToCleanup = $this->getValue('cleanup_all') === '1'
? array_keys($this->bp->getMissingChildren())
: $this->getValue('nodes');
foreach ($nodesToCleanup as $nodeName) {
$node = $this->bp->getNode($nodeName);
$changes->deleteNode($node);
}
unset($changes);
parent::onSuccess();
}
}