From a8d25b70ee0f8d0a365841d1edd7652a47f2c7a4 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 11 Mar 2021 23:00:52 +0100 Subject: [PATCH] Sync: introduce a 'disable' purge action fixes #2285 --- application/forms/SyncRuleForm.php | 16 +++++++++++++++- doc/82-Changelog.md | 3 +++ library/Director/Import/Sync.php | 15 ++++++++++++++- library/Director/Objects/SyncRule.php | 1 + schema/mysql-migrations/upgrade_172.sql | 11 +++++++++++ schema/mysql.sql | 3 ++- schema/pgsql-migrations/upgrade_172.sql | 13 +++++++++++++ schema/pgsql.sql | 4 +++- 8 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 schema/mysql-migrations/upgrade_172.sql create mode 100644 schema/pgsql-migrations/upgrade_172.sql diff --git a/application/forms/SyncRuleForm.php b/application/forms/SyncRuleForm.php index d6d155d2..d88e493d 100644 --- a/application/forms/SyncRuleForm.php +++ b/application/forms/SyncRuleForm.php @@ -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( diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index 03289063..585b1e74 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -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) diff --git a/library/Director/Import/Sync.php b/library/Director/Import/Sync.php index afdb0ba4..d3906bc8 100644 --- a/library/Director/Import/Sync.php +++ b/library/Director/Import/Sync.php @@ -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'" + ); + } } } } diff --git a/library/Director/Objects/SyncRule.php b/library/Director/Objects/SyncRule.php index cb04b179..be3c1b27 100644 --- a/library/Director/Objects/SyncRule.php +++ b/library/Director/Objects/SyncRule.php @@ -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, diff --git a/schema/mysql-migrations/upgrade_172.sql b/schema/mysql-migrations/upgrade_172.sql new file mode 100644 index 00000000..3af3571b --- /dev/null +++ b/schema/mysql-migrations/upgrade_172.sql @@ -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()); diff --git a/schema/mysql.sql b/schema/mysql.sql index fa980092..d421234b 100644 --- a/schema/mysql.sql +++ b/schema/mysql.sql @@ -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()); diff --git a/schema/pgsql-migrations/upgrade_172.sql b/schema/pgsql-migrations/upgrade_172.sql new file mode 100644 index 00000000..49e66a2d --- /dev/null +++ b/schema/pgsql-migrations/upgrade_172.sql @@ -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()); diff --git a/schema/pgsql.sql b/schema/pgsql.sql index 5de9e105..ac554e55 100644 --- a/schema/pgsql.sql +++ b/schema/pgsql.sql @@ -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());