SyncruleCommand: show number of expected changes

Sample output when there are changes:

    icingaweb2@web:~$ icingacli director syncrule check --id 36
    There are pending changes for this Sync Rule. You should  trigger a new Sync Run.
    Expected modifications: 2x create, 0x modify, 0x delete
    icingaweb2@web:~$ echo $?
    1

...when there are no changes:

    icingaweb2@web:~$ icingacli director syncrule check --id 34
    This Sync Rule is in sync
    icingaweb2@web:~$ echo $?
    0

...and when something failes:

    icingaweb2@web:~$ icingacli director syncrule check --id 32
    This Sync Rule failed: No data has been imported for "Benutzergruppen aus Nagios" yet
    icingaweb2@web:~$ echo $?
    2

fixes #1849
This commit is contained in:
Thomas Gelf 2019-05-27 17:46:43 +02:00
parent 7533334de9
commit 0d405cba98
2 changed files with 66 additions and 4 deletions

View File

@ -3,7 +3,9 @@
namespace Icinga\Module\Director\Clicommands; namespace Icinga\Module\Director\Clicommands;
use Icinga\Module\Director\Cli\Command; use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\SyncRule; use Icinga\Module\Director\Objects\SyncRule;
use RuntimeException;
/** /**
* Deal with Director Sync Rules * Deal with Director Sync Rules
@ -59,8 +61,47 @@ class SyncruleCommand extends Command
public function checkAction() public function checkAction()
{ {
$rule = $this->getSyncRule(); $rule = $this->getSyncRule();
$rule->checkForChanges(); $hasChanges = $rule->checkForChanges();
$this->showSyncStateDetails($rule); $this->showSyncStateDetails($rule);
if ($hasChanges) {
$mods = $this->getExpectedModificationCounts($rule);
printf(
"Expected modifications: %dx create, %dx modify, %dx delete\n",
$mods->modify,
$mods->create,
$mods->delete
);
}
exit($this->getSyncStateExitCode($rule));
}
protected function getExpectedModificationCounts(SyncRule $rule)
{
$modifications = $rule->getExpectedModifications();
$create = 0;
$modify = 0;
$delete = 0;
/** @var IcingaObject $object */
foreach ($modifications as $object) {
if ($object->hasBeenLoadedFromDb()) {
if ($object->shouldBeRemoved()) {
$delete++;
} else {
$modify++;
}
} else {
$create++;
}
}
return (object) [
'create' => $create,
'modify' => $modify,
'delete' => $delete,
];
} }
/** /**
@ -103,7 +144,6 @@ class SyncruleCommand extends Command
/** /**
* @param SyncRule $rule * @param SyncRule $rule
* @throws \Icinga\Exception\IcingaException
*/ */
protected function showSyncStateDetails(SyncRule $rule) protected function showSyncStateDetails(SyncRule $rule)
{ {
@ -113,7 +153,6 @@ class SyncruleCommand extends Command
/** /**
* @param SyncRule $rule * @param SyncRule $rule
* @return string * @return string
* @throws \Icinga\Exception\IcingaException
*/ */
protected function getSyncStateDescription(SyncRule $rule) protected function getSyncStateDescription(SyncRule $rule)
{ {
@ -128,6 +167,28 @@ class SyncruleCommand extends Command
. ' trigger a new Sync Run.'; . ' trigger a new Sync Run.';
case 'failing': case 'failing':
return 'This Sync Rule failed: '. $rule->get('last_error_message'); return 'This Sync Rule failed: '. $rule->get('last_error_message');
default:
throw new RuntimeException('Invalid sync state: ' . $rule->get('sync_state'));
}
}
/**
* @param SyncRule $rule
* @return string
*/
protected function getSyncStateExitCode(SyncRule $rule)
{
switch ($rule->get('sync_state')) {
case 'unknown':
return 3;
case 'in-sync':
return 0;
case 'pending-changes':
return 1;
case 'failing':
return 2;
default:
throw new RuntimeException('Invalid sync state: ' . $rule->get('sync_state'));
} }
} }
} }

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Objects;
use Icinga\Application\Benchmark; use Icinga\Application\Benchmark;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Data\Db\DbObject; use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface; use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
@ -184,7 +185,7 @@ class SyncRule extends DbObject implements ExportInterface
/** /**
* @return IcingaObject[] * @return IcingaObject[]
* @throws Exception * @throws IcingaException
*/ */
public function getExpectedModifications() public function getExpectedModifications()
{ {