Sync: introduce a 'disable' purge action

fixes #2285
This commit is contained in:
Thomas Gelf 2021-03-11 23:00:52 +01:00
parent 41f428e86b
commit a8d25b70ee
8 changed files with 62 additions and 4 deletions

View File

@ -74,9 +74,23 @@ class SyncRuleForm extends DirectorObjectForm
. ' longer exist at your import source.'
),
'required' => true,
'class' => 'autosubmit',
]);
if ($this->getSentOrObjectValue('purge_existing') === 'y') {
$this->addElement('select', 'purge_action', [
'label' => $this->translate('Purge Action'),
'description' => $this->translate(
'Whether to delete or to disable objects subject to purge'
),
'multiOptions' => $this->optionalEnum([
'delete' => $this->translate('Delete'),
'disable' => $this->translate('Disable'),
]),
'required' => true,
]);
}
$this->addElement('text', 'filter_expression', [
'label' => $this->translate('Filter Expression'),
'description' => sprintf(

View File

@ -14,6 +14,9 @@ next (will be 1.8.1)
### User Interface
* FIX: don't fail when showing a Host overriding multiple inherited groups (#2253)
### Import and Sync
* FEATURE: introduce 'disable' as your purge action on Sync (#2285)
### Automation, User Interface
* FIX: error message wording on failing related (or parent) object ref (#2224)

View File

@ -21,6 +21,7 @@ use Icinga\Module\Director\Objects\SyncRun;
use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Repository\IcingaTemplateRepository;
use InvalidArgumentException;
use RuntimeException;
class Sync
{
@ -633,6 +634,7 @@ class Sync
Benchmark::measure('Modified objects are ready, applying purge strategy');
$noAction = [];
$purgeAction = $this->rule->get('purge_action');
foreach ($this->rule->purgeStrategy()->listObjectsToPurge() as $key) {
if (array_key_exists($key, $newObjects)) {
// Object has been touched, do not delete
@ -642,7 +644,18 @@ class Sync
if (array_key_exists($key, $this->objects)) {
$object = $this->objects[$key];
if (! $object->hasBeenModified()) {
$object->markForRemoval();
switch ($purgeAction) {
case 'delete':
$object->markForRemoval();
break;
case 'disable':
$object->set('disabled', 'y');
break;
default:
throw new RuntimeException(
"Unsupported purge action: '$purgeAction'"
);
}
}
}
}

View File

@ -29,6 +29,7 @@ class SyncRule extends DbObject implements ExportInterface
'object_type' => null,
'update_policy' => null,
'purge_existing' => null,
'purge_action' => null,
'filter_expression' => null,
'sync_state' => 'unknown',
'last_error_message' => null,

View File

@ -0,0 +1,11 @@
ALTER TABLE sync_rule
ADD COLUMN purge_action ENUM('delete', 'disable') NULL DEFAULT NULL AFTER purge_existing;
UPDATE sync_rule SET purge_action = 'delete';
ALTER TABLE sync_rule
MODIFY COLUMN purge_action ENUM('delete', 'disable') DEFAULT NULL;
INSERT INTO director_schema_migration
(schema_version, migration_time)
VALUES (172, NOW());

View File

@ -1526,6 +1526,7 @@ CREATE TABLE sync_rule (
) NOT NULL,
update_policy ENUM('merge', 'override', 'ignore', 'update-only') NOT NULL,
purge_existing ENUM('y', 'n') NOT NULL DEFAULT 'n',
purge_action ENUM('delete', 'disable') NOT NULL,
filter_expression TEXT DEFAULT NULL,
sync_state ENUM(
'unknown',
@ -1883,4 +1884,4 @@ CREATE TABLE icinga_scheduled_downtime_range (
INSERT INTO director_schema_migration
(schema_version, migration_time)
VALUES (171, NOW());
VALUES (172, NOW());

View File

@ -0,0 +1,13 @@
CREATE TYPE enum_sync_rule_purge_action AS ENUM('delete', 'disable');
ALTER TABLE sync_rule
ADD COLUMN purge_action enum_sync_rule_purge_action NULL DEFAULT NULL;
UPDATE sync_rule SET purge_action = 'delete';
ALTER TABLE sync_rule
ALTER COLUMN purge_action SET NOT NULL;
INSERT INTO director_schema_migration
(schema_version, migration_time)
VALUES (172, NOW());

View File

@ -37,6 +37,7 @@ CREATE TYPE enum_sync_rule_object_type AS ENUM(
'dependency'
);
CREATE TYPE enum_sync_rule_update_policy AS ENUM('merge', 'override', 'ignore', 'update-only');
CREATE TYPE enum_sync_rule_purge_action AS ENUM('delete', 'disable');
CREATE TYPE enum_sync_property_merge_policy AS ENUM('override', 'merge');
CREATE TYPE enum_sync_state AS ENUM(
'unknown',
@ -1690,6 +1691,7 @@ CREATE TABLE sync_rule (
object_type enum_sync_rule_object_type NOT NULL,
update_policy enum_sync_rule_update_policy NOT NULL,
purge_existing enum_boolean NOT NULL DEFAULT 'n',
purge_action enum_sync_rule_purge_action NOT NULL,
filter_expression text DEFAULT NULL,
sync_state enum_sync_state NOT NULL DEFAULT 'unknown',
last_error_message text NULL DEFAULT NULL,
@ -2199,4 +2201,4 @@ COMMENT ON COLUMN icinga_scheduled_downtime_range.merge_behaviour IS 'set -> = {
INSERT INTO director_schema_migration
(schema_version, migration_time)
VALUES (171, NOW());
VALUES (172, NOW());