From 0d879c60d30e2f7ee284c9916edabc46d6074e69 Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Thu, 13 Sep 2018 13:57:18 +0200 Subject: [PATCH] IcingaTimeperiod: Support includes and excludes --- application/forms/IcingaTimePeriodForm.php | 46 +++++++++++++++++++ library/Director/Objects/IcingaTimePeriod.php | 13 ++++++ schema/mysql-migrations/upgrade_151.sql | 38 +++++++++++++++ schema/mysql.sql | 35 +++++++++++++- schema/pgsql-migrations/upgrade_151.sql | 38 +++++++++++++++ schema/pgsql.sql | 34 +++++++++++++- 6 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 schema/mysql-migrations/upgrade_151.sql create mode 100644 schema/pgsql-migrations/upgrade_151.sql diff --git a/application/forms/IcingaTimePeriodForm.php b/application/forms/IcingaTimePeriodForm.php index dca062f5..a0ada5a5 100644 --- a/application/forms/IcingaTimePeriodForm.php +++ b/application/forms/IcingaTimePeriodForm.php @@ -52,8 +52,54 @@ class IcingaTimePeriodForm extends DirectorObjectForm $this->addHidden('update_method', 'LegacyTimePeriod'); } + $this->addIncludeExclude(); + $this->addImportsElement(); $this->setButtons(); } + + protected function addIncludeExclude() + { + $periods = []; + foreach ($this->db->enumTimeperiods() as $id => $period) { + if ($this->object === null || $this->object->get('object_name') !== $period) { + $periods[$period] = $period; + } + } + + if (empty($periods)) { + return; + } + + $this->addElement( + 'extensibleSet', + 'includes', + array( + 'label' => $this->translate('Include period'), + 'description' => $this->translate( + 'Include other time periods into this.' + ), + 'multiOptions' => $this->optionalEnum($periods), + ) + ); + + $this->addElement( + 'extensibleSet', + 'excludes', + array( + 'label' => $this->translate('Exclude period'), + 'description' => $this->translate( + 'Exclude other time periods from this.' + ), + 'multiOptions' => $this->optionalEnum($periods), + ) + ); + + $this->optionalBoolean( + 'prefer_includes', + $this->translate('Prefer includes'), + $this->translate('Whether to prefer timeperiods includes or excludes. Default to true.') + ); + } } diff --git a/library/Director/Objects/IcingaTimePeriod.php b/library/Director/Objects/IcingaTimePeriod.php index 4b97b884..fadb68e6 100644 --- a/library/Director/Objects/IcingaTimePeriod.php +++ b/library/Director/Objects/IcingaTimePeriod.php @@ -14,6 +14,7 @@ class IcingaTimePeriod extends IcingaObject 'object_name' => null, 'object_type' => null, 'disabled' => 'n', + 'prefer_includes' => null, 'display_name' => null, 'update_method' => null, ); @@ -28,6 +29,18 @@ class IcingaTimePeriod extends IcingaObject 'zone' => 'IcingaZone', ); + protected $multiRelations = [ + 'includes' => [ + 'relatedObjectClass' => 'IcingaTimeperiod', + 'relatedShortName' => 'include', + ], + 'excludes' => [ + 'relatedObjectClass' => 'IcingaTimeperiod', + 'relatedShortName' => 'exclude', + 'legacyPropertyName' => 'exclude' + ], + ]; + /** * Render update property * diff --git a/schema/mysql-migrations/upgrade_151.sql b/schema/mysql-migrations/upgrade_151.sql new file mode 100644 index 00000000..a811fd4c --- /dev/null +++ b/schema/mysql-migrations/upgrade_151.sql @@ -0,0 +1,38 @@ +ALTER TABLE icinga_timeperiod + ADD COLUMN prefer_includes ENUM('y', 'n') DEFAULT NULL; + +CREATE TABLE icinga_timeperiod_include ( + timeperiod_id INT(10) UNSIGNED NOT NULL, + include_id INT(10) UNSIGNED NOT NULL, + PRIMARY KEY (timeperiod_id, include_id), + CONSTRAINT icinga_timeperiod_include + FOREIGN KEY timeperiod (include_id) + REFERENCES icinga_timeperiod (id) + ON DELETE RESTRICT + ON UPDATE RESTRICT, + CONSTRAINT icinga_timeperiod_include_timeperiod + FOREIGN KEY include (timeperiod_id) + REFERENCES icinga_timeperiod (id) + ON DELETE CASCADE + ON UPDATE CASCADE +); + +CREATE TABLE icinga_timeperiod_exclude ( + timeperiod_id INT(10) UNSIGNED NOT NULL, + exclude_id INT(10) UNSIGNED NOT NULL, + PRIMARY KEY (timeperiod_id, exclude_id), + CONSTRAINT icinga_timeperiod_exclude + FOREIGN KEY timeperiod (exclude_id) + REFERENCES icinga_timeperiod (id) + ON DELETE RESTRICT + ON UPDATE RESTRICT, + CONSTRAINT icinga_timeperiod_exclude_timeperiod + FOREIGN KEY exclude (timeperiod_id) + REFERENCES icinga_timeperiod (id) + ON DELETE CASCADE + ON UPDATE CASCADE +); + +INSERT INTO director_schema_migration + (schema_version, migration_time) + VALUES (151, NOW()); diff --git a/schema/mysql.sql b/schema/mysql.sql index b85b2a80..39b919cb 100644 --- a/schema/mysql.sql +++ b/schema/mysql.sql @@ -197,6 +197,7 @@ CREATE TABLE icinga_timeperiod ( zone_id INT(10) UNSIGNED DEFAULT NULL, object_type ENUM('object', 'template') NOT NULL, disabled ENUM('y', 'n') NOT NULL DEFAULT 'n', + prefer_includes ENUM('y', 'n') DEFAULT NULL, PRIMARY KEY (id), UNIQUE INDEX object_name (object_name, zone_id), CONSTRAINT icinga_timeperiod_zone @@ -1695,6 +1696,38 @@ CREATE TABLE icinga_dependency_states_set ( ON UPDATE CASCADE ) ENGINE=InnoDB; +CREATE TABLE icinga_timeperiod_include ( + timeperiod_id INT(10) UNSIGNED NOT NULL, + include_id INT(10) UNSIGNED NOT NULL, + PRIMARY KEY (timeperiod_id, include_id), + CONSTRAINT icinga_timeperiod_include + FOREIGN KEY timeperiod (include_id) + REFERENCES icinga_timeperiod (id) + ON DELETE RESTRICT + ON UPDATE RESTRICT, + CONSTRAINT icinga_timeperiod_include_timeperiod + FOREIGN KEY include (timeperiod_id) + REFERENCES icinga_timeperiod (id) + ON DELETE CASCADE + ON UPDATE CASCADE +); + +CREATE TABLE icinga_timeperiod_exclude ( + timeperiod_id INT(10) UNSIGNED NOT NULL, + exclude_id INT(10) UNSIGNED NOT NULL, + PRIMARY KEY (timeperiod_id, exclude_id), + CONSTRAINT icinga_timeperiod_exclude + FOREIGN KEY timeperiod (exclude_id) + REFERENCES icinga_timeperiod (id) + ON DELETE RESTRICT + ON UPDATE RESTRICT, + CONSTRAINT icinga_timeperiod_exclude_timeperiod + FOREIGN KEY exclude (timeperiod_id) + REFERENCES icinga_timeperiod (id) + ON DELETE CASCADE + ON UPDATE CASCADE +); + INSERT INTO director_schema_migration (schema_version, migration_time) - VALUES (150, NOW()); + VALUES (151, NOW()); diff --git a/schema/pgsql-migrations/upgrade_151.sql b/schema/pgsql-migrations/upgrade_151.sql new file mode 100644 index 00000000..a24189aa --- /dev/null +++ b/schema/pgsql-migrations/upgrade_151.sql @@ -0,0 +1,38 @@ +ALTER TABLE icinga_timeperiod + ADD COLUMN prefer_includes enum_boolean DEFAULT NULL; + +CREATE TABLE icinga_timeperiod_include ( + timeperiod_id integer NOT NULL, + include_id integer NOT NULL, + PRIMARY KEY (timeperiod_id, include_id), + CONSTRAINT icinga_timeperiod_timeperiod_include + FOREIGN KEY (include_id) + REFERENCES icinga_timeperiod (id) + ON DELETE RESTRICT + ON UPDATE CASCADE, + CONSTRAINT icinga_timeperiod_include + FOREIGN KEY (timeperiod_id) + REFERENCES icinga_timeperiod (id) + ON DELETE CASCADE + ON UPDATE CASCADE +); + +CREATE TABLE icinga_timeperiod_exclude ( + timeperiod_id integer NOT NULL, + exclude_id integer NOT NULL, + PRIMARY KEY (timeperiod_id, exclude_id), + CONSTRAINT icinga_timeperiod_timeperiod_exclude + FOREIGN KEY (exclude_id) + REFERENCES icinga_timeperiod (id) + ON DELETE RESTRICT + ON UPDATE CASCADE, + CONSTRAINT icinga_timeperiod_exclude + FOREIGN KEY (timeperiod_id) + REFERENCES icinga_timeperiod (id) + ON DELETE CASCADE + ON UPDATE CASCADE +); + +INSERT INTO director_schema_migration + (schema_version, migration_time) +VALUES (151, NOW()); diff --git a/schema/pgsql.sql b/schema/pgsql.sql index 5a1762f2..962b98c5 100644 --- a/schema/pgsql.sql +++ b/schema/pgsql.sql @@ -273,6 +273,7 @@ CREATE TABLE icinga_timeperiod ( zone_id integer DEFAULT NULL, object_type enum_object_type_all NOT NULL, disabled enum_boolean NOT NULL DEFAULT 'n', + prefer_includes enum_boolean DEFAULT NULL, PRIMARY KEY (id), CONSTRAINT icinga_timeperiod_zone FOREIGN KEY (zone_id) @@ -1990,7 +1991,38 @@ CREATE TABLE icinga_dependency_states_set ( CREATE INDEX dependency_states_set_dependency ON icinga_dependency_states_set (dependency_id); COMMENT ON COLUMN icinga_dependency_states_set.merge_behaviour IS 'override: = [], extend: += [], blacklist: -= []'; +CREATE TABLE icinga_timeperiod_include ( + timeperiod_id integer NOT NULL, + include_id integer NOT NULL, + PRIMARY KEY (timeperiod_id, include_id), + CONSTRAINT icinga_timeperiod_timeperiod_include + FOREIGN KEY (include_id) + REFERENCES icinga_timeperiod (id) + ON DELETE RESTRICT + ON UPDATE CASCADE, + CONSTRAINT icinga_timeperiod_include + FOREIGN KEY (timeperiod_id) + REFERENCES icinga_timeperiod (id) + ON DELETE CASCADE + ON UPDATE CASCADE +); + +CREATE TABLE icinga_timeperiod_exclude ( + timeperiod_id integer NOT NULL, + exclude_id integer NOT NULL, + PRIMARY KEY (timeperiod_id, exclude_id), + CONSTRAINT icinga_timeperiod_timeperiod_exclude + FOREIGN KEY (exclude_id) + REFERENCES icinga_timeperiod (id) + ON DELETE RESTRICT + ON UPDATE CASCADE, + CONSTRAINT icinga_timeperiod_exclude + FOREIGN KEY (timeperiod_id) + REFERENCES icinga_timeperiod (id) + ON DELETE CASCADE + ON UPDATE CASCADE +); INSERT INTO director_schema_migration (schema_version, migration_time) - VALUES (150, NOW()); + VALUES (151, NOW());