diff --git a/application/forms/Command/Object/ScheduleHostDowntimeForm.php b/application/forms/Command/Object/ScheduleHostDowntimeForm.php index a09b00d82..84979da54 100644 --- a/application/forms/Command/Object/ScheduleHostDowntimeForm.php +++ b/application/forms/Command/Object/ScheduleHostDowntimeForm.php @@ -4,14 +4,10 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; -use CallbackFilterIterator; use DateInterval; use DateTime; use Icinga\Application\Config; -use Icinga\Module\Icingadb\Command\Object\PropagateHostDowntimeCommand; -use Icinga\Module\Icingadb\Command\Object\ScheduleHostDowntimeCommand; use Icinga\Web\Notification; -use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; use Iterator; use Traversable; @@ -60,7 +56,7 @@ protected function assembleElements() $decorator = new IcingaFormDecorator(); - $this->addElement( + $allServices = $this->createElement( 'checkbox', 'all_services', [ @@ -72,54 +68,19 @@ protected function assembleElements() 'value' => $this->hostDowntimeAllServices ] ); - $decorator->decorate($this->getElement('all_services')); - - $this->addElement( - 'select', - 'child_options', - array( - 'description' => t('Schedule child downtimes.'), - 'label' => t('Child Options'), - 'options' => [ - 0 => t('Do nothing with child hosts'), - 1 => t('Schedule triggered downtime for all child hosts'), - 2 => t('Schedule non-triggered downtime for all child hosts') - ] - ) - ); - $decorator->decorate($this->getElement('child_options')); + $this->insertBefore($allServices, $this->getElement('child_options')); + $this->registerElement($allServices); + $decorator->decorate($allServices); } protected function getCommands(Iterator $objects): Traversable { - $granted = new CallbackFilterIterator($objects, function (Model $object): bool { - return $this->isGrantedOn('icingadb/command/downtime/schedule', $object); - }); - - $granted->rewind(); // Forwards the pointer to the first element - if ($granted->valid()) { - if (($childOptions = (int) $this->getValue('child_options'))) { - $command = new PropagateHostDowntimeCommand(); - $command->setTriggered($childOptions === 1); - } else { - $command = new ScheduleHostDowntimeCommand(); - } - - $command->setObjects($granted); - $command->setComment($this->getValue('comment')); - $command->setAuthor($this->getAuth()->getUser()->getUsername()); - $command->setStart($this->getValue('start')->getTimestamp()); - $command->setEnd($this->getValue('end')->getTimestamp()); - $command->setForAllServices($this->getElement('all_services')->isChecked()); - - if ($this->getElement('flexible')->isChecked()) { - $command->setFixed(false); - $command->setDuration( - $this->getValue('hours') * 3600 + $this->getValue('minutes') * 60 - ); - } + if (! $this->getElement('all_services')->isChecked()) { + yield from parent::getCommands($objects); + } - yield $command; + foreach (parent::getCommands($objects) as $command) { + yield $command->setForAllServices(); } } } diff --git a/application/forms/Command/Object/ScheduleServiceDowntimeForm.php b/application/forms/Command/Object/ScheduleServiceDowntimeForm.php index b237408ae..7b8935bea 100644 --- a/application/forms/Command/Object/ScheduleServiceDowntimeForm.php +++ b/application/forms/Command/Object/ScheduleServiceDowntimeForm.php @@ -8,7 +8,7 @@ use DateInterval; use DateTime; use Icinga\Application\Config; -use Icinga\Module\Icingadb\Command\Object\ScheduleServiceDowntimeCommand; +use Icinga\Module\Icingadb\Command\Object\ScheduleDowntimeCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Web\Notification; use ipl\Html\Attributes; @@ -229,6 +229,27 @@ function ($hoursInputWrapper) use ($minutesInput, $hoursInput) { $this->add($hoursInput); } + + $this->addElement( + 'select', + 'child_options', + array( + 'description' => t('Schedule child downtimes.'), + 'label' => t('Child Options'), + 'options' => [ + ScheduleDowntimeCommand::IGNORE_CHILDREN => t( + 'Do nothing with children' + ), + ScheduleDowntimeCommand::TRIGGER_CHILDREN => t( + 'Schedule a downtime for all children and trigger them by this downtime' + ), + ScheduleDowntimeCommand::SCHEDULE_CHILDREN => t( + 'Schedule non-triggered downtime for all children' + ) + ] + ) + ); + $decorator->decorate($this->getElement('child_options')); } protected function assembleSubmitButton() @@ -253,12 +274,13 @@ protected function getCommands(Iterator $objects): Traversable $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { - $command = new ScheduleServiceDowntimeCommand(); + $command = new ScheduleDowntimeCommand(); $command->setObjects($granted); $command->setComment($this->getValue('comment')); $command->setAuthor($this->getAuth()->getUser()->getUsername()); $command->setStart($this->getValue('start')->getTimestamp()); $command->setEnd($this->getValue('end')->getTimestamp()); + $command->setChildOption((int) $this->getValue('child_options')); if ($this->getElement('flexible')->isChecked()) { $command->setFixed(false); diff --git a/doc/05-Upgrading.md b/doc/05-Upgrading.md index 007a4c1ab..437e74308 100644 --- a/doc/05-Upgrading.md +++ b/doc/05-Upgrading.md @@ -3,6 +3,15 @@ Specific version upgrades are described below. Please note that version upgrades are incremental. If you are upgrading across multiple versions, make sure to follow the steps for each of them. +## Upgrading to Icinga DB Web v1.2 + +**Deprecations** + +The following classes have been deprecated and will be removed in a future release: +* `\Icinga\Module\Icingadb\Command\Object\PropagateHostDowntimeCommand` +* `\Icinga\Module\Icingadb\Command\Object\ScheduleHostDowntimeCommand` +* `\Icinga\Module\Icingadb\Command\Object\ScheduleServiceDowntimeCommand` + ## Upgrading to Icinga DB Web v1.1 **Breaking Changes** diff --git a/library/Icingadb/Command/Object/PropagateHostDowntimeCommand.php b/library/Icingadb/Command/Object/PropagateHostDowntimeCommand.php index 88964fbaa..49b4c7769 100644 --- a/library/Icingadb/Command/Object/PropagateHostDowntimeCommand.php +++ b/library/Icingadb/Command/Object/PropagateHostDowntimeCommand.php @@ -6,15 +6,12 @@ /** * Schedule and propagate host downtime + * + * @deprecated Use {@see ScheduleDowntimeCommand} instead */ class PropagateHostDowntimeCommand extends ScheduleHostDowntimeCommand { - /** - * Whether the downtime for child hosts are all set to be triggered by this' host downtime - * - * @var bool - */ - protected $triggered = false; + protected $childOption = ScheduleDowntimeCommand::SCHEDULE_CHILDREN; /** * Set whether the downtime for child hosts are all set to be triggered by this' host downtime @@ -25,9 +22,11 @@ class PropagateHostDowntimeCommand extends ScheduleHostDowntimeCommand */ public function setTriggered(bool $triggered = true): self { - $this->triggered = $triggered; - - return $this; + return $this->setChildOption( + $triggered + ? ScheduleDowntimeCommand::TRIGGER_CHILDREN + : ScheduleDowntimeCommand::SCHEDULE_CHILDREN + ); } /** @@ -37,6 +36,6 @@ public function setTriggered(bool $triggered = true): self */ public function getTriggered(): bool { - return $this->triggered; + return $this->getChildOption() === ScheduleDowntimeCommand::TRIGGER_CHILDREN; } } diff --git a/library/Icingadb/Command/Object/ScheduleDowntimeCommand.php b/library/Icingadb/Command/Object/ScheduleDowntimeCommand.php new file mode 100644 index 000000000..bb2cda770 --- /dev/null +++ b/library/Icingadb/Command/Object/ScheduleDowntimeCommand.php @@ -0,0 +1,264 @@ +start = $start; + + return $this; + } + + /** + * Get the time when the downtime should start + * + * @return int Unix timestamp + */ + public function getStart(): int + { + if ($this->start === null) { + throw new \LogicException( + 'You are accessing an unset property. Please make sure to set it beforehand.' + ); + } + + return $this->start; + } + + /** + * Set the time when the downtime should end + * + * @param int $end Unix timestamp + * + * @return $this + */ + public function setEnd(int $end): self + { + $this->end = $end; + + return $this; + } + + /** + * Get the time when the downtime should end + * + * @return int Unix timestamp + */ + public function getEnd(): int + { + if ($this->start === null) { + throw new \LogicException( + 'You are accessing an unset property. Please make sure to set it beforehand.' + ); + } + + return $this->end; + } + + /** + * Set whether it's a fixed or flexible downtime + * + * @param boolean $fixed + * + * @return $this + */ + public function setFixed(bool $fixed = true): self + { + $this->fixed = $fixed; + + return $this; + } + + /** + * Is the downtime fixed? + * + * @return boolean + */ + public function getFixed(): bool + { + return $this->fixed; + } + + /** + * Set the ID of the downtime which triggers this downtime + * + * @param int $triggerId + * + * @return $this + */ + public function setTriggerId(int $triggerId): self + { + $this->triggerId = $triggerId; + + return $this; + } + + /** + * Get the ID of the downtime which triggers this downtime + * + * @return int|null + */ + public function getTriggerId() + { + return $this->triggerId; + } + + /** + * Set the duration in seconds the downtime must last if it's a flexible downtime + * + * @param int $duration + * + * @return $this + */ + public function setDuration(int $duration): self + { + $this->duration = $duration; + + return $this; + } + + /** + * Get the duration in seconds the downtime must last if it's a flexible downtime + * + * @return int|null + */ + public function getDuration() + { + return $this->duration; + } + + /** + * Set whether to schedule a downtime for all services associated with a particular host + * + * @param bool $forAllServices + * + * @return $this + */ + public function setForAllServices(bool $forAllServices = true): self + { + $this->forAllServices = $forAllServices; + + return $this; + } + + /** + * Get whether to schedule a downtime for all services associated with a particular host + * + * @return bool + */ + public function getForAllServices(): bool + { + return $this->forAllServices; + } + + /** + * Set what to do with children + * + * @param CHILD_OPTION $option + * + * @return $this + */ + public function setChildOption(int $option): self + { + $this->childOption = $option; + + return $this; + } + + /** + * Get what to do with children + * + * @return CHILD_OPTION + */ + public function getChildOption(): int + { + return $this->childOption; + } +} diff --git a/library/Icingadb/Command/Object/ScheduleHostDowntimeCommand.php b/library/Icingadb/Command/Object/ScheduleHostDowntimeCommand.php index 0e4d84fad..a0cb64510 100644 --- a/library/Icingadb/Command/Object/ScheduleHostDowntimeCommand.php +++ b/library/Icingadb/Command/Object/ScheduleHostDowntimeCommand.php @@ -6,37 +6,9 @@ /** * Schedule a host downtime + * + * @deprecated Use {@see ScheduleDowntimeCommand} instead */ class ScheduleHostDowntimeCommand extends ScheduleServiceDowntimeCommand { - /** - * Whether to schedule a downtime for all services associated with a particular host - * - * @var bool - */ - protected $forAllServices = false; - - /** - * Set whether to schedule a downtime for all services associated with a particular host - * - * @param bool $forAllServices - * - * @return $this - */ - public function setForAllServices(bool $forAllServices = true): self - { - $this->forAllServices = $forAllServices; - - return $this; - } - - /** - * Get whether to schedule a downtime for all services associated with a particular host - * - * @return bool - */ - public function getForAllServices(): bool - { - return $this->forAllServices; - } } diff --git a/library/Icingadb/Command/Object/ScheduleServiceDowntimeCommand.php b/library/Icingadb/Command/Object/ScheduleServiceDowntimeCommand.php index 3bad28ef2..05a88d5d0 100644 --- a/library/Icingadb/Command/Object/ScheduleServiceDowntimeCommand.php +++ b/library/Icingadb/Command/Object/ScheduleServiceDowntimeCommand.php @@ -6,189 +6,11 @@ /** * Schedule a service downtime + * + * @deprecated Use {@see ScheduleDowntimeCommand} instead */ -class ScheduleServiceDowntimeCommand extends AddCommentCommand +class ScheduleServiceDowntimeCommand extends ScheduleDowntimeCommand { - /** - * Downtime starts at the exact time specified - * - * If `Downtime::$fixed' is set to false, the time between `Downtime::$start' and `Downtime::$end' at which a - * host or service transitions to a problem state determines the time at which the downtime actually starts. - * The downtime will then last for `Downtime::$duration' seconds. - * - * @var int Unix timestamp - */ - protected $start; - - /** - * Downtime ends at the exact time specified - * - * If `Downtime::$fixed' is set to false, the time between `Downtime::$start' and `Downtime::$end' at which a - * host or service transitions to a problem state determines the time at which the downtime actually starts. - * The downtime will then last for `Downtime::$duration' seconds. - * - * @var int Unix timestamp - */ - protected $end; - - /** - * Whether it's a fixed or flexible downtime - * - * @var bool - */ - protected $fixed = true; - - /** - * ID of the downtime which triggers this downtime - * - * The start of this downtime is triggered by the start of the other scheduled host or service downtime. - * - * @var int|null - */ - protected $triggerId; - - /** - * The duration in seconds the downtime must last if it's a flexible downtime - * - * If `Downtime::$fixed' is set to false, the downtime will last for the duration in seconds specified, even - * if the host or service recovers before the downtime expires. - * - * @var int|null - */ - protected $duration; - - /** - * Set the time when the downtime should start - * - * @param int $start Unix timestamp - * - * @return $this - */ - public function setStart(int $start): self - { - $this->start = $start; - - return $this; - } - - /** - * Get the time when the downtime should start - * - * @return int Unix timestamp - */ - public function getStart(): int - { - if ($this->start === null) { - throw new \LogicException( - 'You are accessing an unset property. Please make sure to set it beforehand.' - ); - } - - return $this->start; - } - - /** - * Set the time when the downtime should end - * - * @param int $end Unix timestamp - * - * @return $this - */ - public function setEnd(int $end): self - { - $this->end = $end; - - return $this; - } - - /** - * Get the time when the downtime should end - * - * @return int Unix timestamp - */ - public function getEnd(): int - { - if ($this->start === null) { - throw new \LogicException( - 'You are accessing an unset property. Please make sure to set it beforehand.' - ); - } - - return $this->end; - } - - /** - * Set whether it's a fixed or flexible downtime - * - * @param boolean $fixed - * - * @return $this - */ - public function setFixed(bool $fixed = true): self - { - $this->fixed = $fixed; - - return $this; - } - - /** - * Is the downtime fixed? - * - * @return boolean - */ - public function getFixed(): bool - { - return $this->fixed; - } - - /** - * Set the ID of the downtime which triggers this downtime - * - * @param int $triggerId - * - * @return $this - */ - public function setTriggerId(int $triggerId): self - { - $this->triggerId = $triggerId; - - return $this; - } - - /** - * Get the ID of the downtime which triggers this downtime - * - * @return int|null - */ - public function getTriggerId() - { - return $this->triggerId; - } - - /** - * Set the duration in seconds the downtime must last if it's a flexible downtime - * - * @param int $duration - * - * @return $this - */ - public function setDuration(int $duration): self - { - $this->duration = $duration; - - return $this; - } - - /** - * Get the duration in seconds the downtime must last if it's a flexible downtime - * - * @return int|null - */ - public function getDuration() - { - return $this->duration; - } - public function getName(): string { return 'ScheduleDowntime'; diff --git a/library/Icingadb/Command/Renderer/IcingaApiCommandRenderer.php b/library/Icingadb/Command/Renderer/IcingaApiCommandRenderer.php index 37075c994..c7d221bac 100644 --- a/library/Icingadb/Command/Renderer/IcingaApiCommandRenderer.php +++ b/library/Icingadb/Command/Renderer/IcingaApiCommandRenderer.php @@ -6,6 +6,7 @@ use Icinga\Module\Icingadb\Command\IcingaApiCommand; use Icinga\Module\Icingadb\Command\Object\GetObjectCommand; +use Icinga\Module\Icingadb\Command\Object\ScheduleDowntimeCommand; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; use Icinga\Module\Icingadb\Command\Instance\ToggleInstanceFeatureCommand; @@ -14,11 +15,8 @@ use Icinga\Module\Icingadb\Command\Object\DeleteCommentCommand; use Icinga\Module\Icingadb\Command\Object\DeleteDowntimeCommand; use Icinga\Module\Icingadb\Command\Object\ProcessCheckResultCommand; -use Icinga\Module\Icingadb\Command\Object\PropagateHostDowntimeCommand; use Icinga\Module\Icingadb\Command\Object\RemoveAcknowledgementCommand; -use Icinga\Module\Icingadb\Command\Object\ScheduleHostDowntimeCommand; use Icinga\Module\Icingadb\Command\Object\ScheduleCheckCommand; -use Icinga\Module\Icingadb\Command\Object\ScheduleServiceDowntimeCommand; use Icinga\Module\Icingadb\Command\Object\SendCustomNotificationCommand; use Icinga\Module\Icingadb\Command\Object\ToggleObjectFeatureCommand; use Icinga\Module\Icingadb\Command\IcingaCommand; @@ -191,7 +189,7 @@ public function renderScheduleCheck(ScheduleCheckCommand $command): IcingaApiCom return IcingaApiCommand::create($endpoint, $data); } - public function renderScheduleDowntime(ScheduleServiceDowntimeCommand $command): IcingaApiCommand + public function renderScheduleDowntime(ScheduleDowntimeCommand $command): IcingaApiCommand { $endpoint = 'actions/schedule-downtime'; $data = [ @@ -201,14 +199,11 @@ public function renderScheduleDowntime(ScheduleServiceDowntimeCommand $command): 'end_time' => $command->getEnd(), 'duration' => $command->getDuration(), 'fixed' => $command->getFixed(), - 'trigger_name' => $command->getTriggerId() + 'trigger_name' => $command->getTriggerId(), + 'child_options' => $command->getChildOption() ]; - if ($command instanceof PropagateHostDowntimeCommand) { - $data['child_options'] = $command->getTriggered() ? 1 : 2; - } - - if ($command instanceof ScheduleHostDowntimeCommand && $command->getForAllServices()) { + if ($command->getForAllServices()) { $data['all_services'] = true; }