Fix group and children handling of ScheduleDowntimeCommand

refs #4580
This commit is contained in:
Johannes Meyer 2013-09-06 09:09:20 +02:00 committed by Eric Lippmann
parent 7045148f93
commit 8e60e2dcd9
3 changed files with 97 additions and 38 deletions

View File

@ -159,10 +159,6 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
) )
); );
/**
* @TODO: Display downtime list (Bug #4496)
*
*/
$this->addElement( $this->addElement(
'select', 'select',
'triggered', 'triggered',
@ -334,8 +330,8 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
*/ */
public function createCommand() public function createCommand()
{ {
// TODO: Add support for propagation, host-/servicegroups and services only (#4588) // TODO: Add support for host-/servicegroups and services only (#4588)
return new ScheduleDowntimeCommand( $command = new ScheduleDowntimeCommand(
$this->getValue('starttime'), $this->getValue('starttime'),
$this->getValue('endtime'), $this->getValue('endtime'),
new Comment( new Comment(
@ -346,5 +342,9 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
$this->getValue('hours') * 3600 + $this->getValue('minutes') * 60, $this->getValue('hours') * 3600 + $this->getValue('minutes') * 60,
$this->getValue('triggered') $this->getValue('triggered')
); );
return $command->includeChildren(
$this->getWithChildren(),
$this->getValue('childobjects') === 1
);
} }
} }

View File

@ -77,6 +77,13 @@ class ScheduleDowntimeCommand extends BaseCommand
*/ */
private $triggerId; private $triggerId;
/**
* Whether this downtime should trigger children hosts
*
* @var bool
*/
private $triggerChildren;
/** /**
* Set when to start this downtime * Set when to start this downtime
* *
@ -175,6 +182,19 @@ class ScheduleDowntimeCommand extends BaseCommand
$this->triggerId = $triggerId; $this->triggerId = $triggerId;
} }
/**
* Include all children hosts with this command
*
* @param bool $state
* @param bool $trigger Whether children are triggered by this downtime
*
* @return self
*/
public function includeChildren($state = true, $trigger = false) {
$this->triggerChildren = (bool) $trigger;
return parent::includeChildren($state);
}
/** /**
* Return this command's parameters properly arranged in an array * Return this command's parameters properly arranged in an array
* *
@ -197,31 +217,21 @@ class ScheduleDowntimeCommand extends BaseCommand
} }
/** /**
* Return the command as a string for the given host or all of it's services * Return the command as a string for the given host
* *
* @param type $hostname The name of the host to insert * @param type $hostname The name of the host to insert
* @param type $servicesOnly Whether this downtime is for the given host or all of it's services
* *
* @return string The string representation of the command * @return string The string representation of the command
*/ */
public function getHostCommand($hostname, $servicesOnly = false) public function getHostCommand($hostname)
{ {
return sprintf('SCHEDULE_HOST%s_DOWNTIME;', $servicesOnly ? '_SVC' : '') if ($this->withChildren) {
. implode(';', array_merge(array($hostname), $this->getParameters())); return sprintf('SCHEDULE_AND_PROPAGATE%s_HOST_DOWNTIME;', $this->triggerChildren ? '_TRIGGERED' : '')
}
/**
* Return the command as a string for the given host and all of it's children hosts
*
* @param string $hostname The name of the host to insert
* @param bool $triggered Whether it's children are triggered
*
* @return string The string representation of the command
*/
public function getPropagatedHostCommand($hostname, $triggered = false)
{
return sprintf('SCHEDULE_AND_PROPAGATE%s_HOST_DOWNTIME;', $triggered ? '_TRIGGERED' : '')
. implode(';', array_merge(array($hostname), $this->getParameters())); . implode(';', array_merge(array($hostname), $this->getParameters()));
} else {
return sprintf('SCHEDULE_HOST%s_DOWNTIME;', $this->onlyServices ? '_SVC' : '')
. implode(';', array_merge(array($hostname), $this->getParameters()));
}
} }
/** /**
@ -244,30 +254,28 @@ class ScheduleDowntimeCommand extends BaseCommand
} }
/** /**
* Return the command as a string for all hosts or services of the given hostgroup * Return the command as a string for the given hostgroup
* *
* @param type $hostgroup The name of the hostgroup to insert * @param type $hostgroup The name of the hostgroup to insert
* @param type $hostsOnly Whether only hosts or services are taken into account
* *
* @return string The string representation of the command * @return string The string representation of the command
*/ */
public function getHostgroupCommand($hostgroup, $hostsOnly = true) public function getHostgroupCommand($hostgroup)
{ {
return sprintf('SCHEDULE_HOSTGROUP_%s_DOWNTIME;', $hostsOnly ? 'HOST' : 'SVC') return sprintf('SCHEDULE_HOSTGROUP_%s_DOWNTIME;', $this->withoutHosts ? 'SVC' : 'HOST')
. implode(';', array_merge(array($hostgroup), $this->getParameters())); . implode(';', array_merge(array($hostgroup), $this->getParameters()));
} }
/** /**
* Return the command as a string for all hosts or services of the given servicegroup * Return the command as a string for the given servicegroup
* *
* @param type $servicegroup The name of the servicegroup to insert * @param type $servicegroup The name of the servicegroup to insert
* @param type $hostsOnly Whether only hosts or services are taken into account
* *
* @return string The string representation of the command * @return string The string representation of the command
*/ */
public function getServicegroupCommand($servicegroup, $hostsOnly = true) public function getServicegroupCommand($servicegroup)
{ {
return sprintf('SCHEDULE_SERVICEGROUP_%s_DOWNTIME;', $hostsOnly ? 'HOST' : 'SVC') return sprintf('SCHEDULE_SERVICEGROUP_%s_DOWNTIME;', $this->withoutServices ? 'HOST' : 'SVC')
. implode(';', array_merge(array($servicegroup), $this->getParameters())); . implode(';', array_merge(array($servicegroup), $this->getParameters()));
} }
} }

View File

@ -421,27 +421,78 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase
{ {
$pipe = $this->getLocalTestPipe(); $pipe = $this->getLocalTestPipe();
try { try {
$downtime = new ScheduleDowntimeCommand(25, 26, new Comment("me", "test")); $downtime = new ScheduleDowntimeCommand(25, 26, new Comment('me', 'test'));
$pipe->sendCommand( $pipe->sendCommand(
$downtime, $downtime,
array( array(
(object) array( (object) array(
"host_name" => "Testhost" 'host_name' => 'Testhost'
) )
) )
); );
$this->assertCommandSucceeded("SCHEDULE_HOST_DOWNTIME;Testhost;25;26;1;0;0;me;test"); $this->assertCommandSucceeded('SCHEDULE_HOST_DOWNTIME;Testhost;25;26;1;0;0;me;test');
$pipe->sendCommand( $pipe->sendCommand(
$downtime, $downtime,
array( array(
(object) array( (object) array(
"host_name" => "Testhost", 'host_name' => 'Testhost',
"service_description" => "svc" 'service_description' => 'svc'
) )
) )
); );
$this->assertCommandSucceeded("SCHEDULE_SVC_DOWNTIME;Testhost;svc;25;26;1;0;0;me;test"); $this->assertCommandSucceeded('SCHEDULE_SVC_DOWNTIME;Testhost;svc;25;26;1;0;0;me;test');
$downtime->excludeHost();
$pipe->sendCommand(
$downtime,
array(
(object) array(
'host_name' => 'Testhost'
)
)
);
$this->assertCommandSucceeded('SCHEDULE_HOST_SVC_DOWNTIME;Testhost;25;26;1;0;0;me;test');
} catch (Exception $e) {
$this->cleanup();
throw $e;
}
$this->cleanup();
}
/**
* Test whether propagated host downtimes are correctly scheduled
*
* @throws Exception
*/
public function testSchedulePropagatedDowntime()
{
$pipe = $this->getLocalTestPipe();
try {
$downtime = new ScheduleDowntimeCommand(25, 26, new Comment('me', 'test'));
$downtime->includeChildren();
$pipe->sendCommand(
$downtime,
array(
(object) array(
'host_name' => 'Testhost'
)
)
);
$this->assertCommandSucceeded('SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME;Testhost;25;26;1;0;0;me;test');
$downtime->includeChildren(true, true);
$pipe->sendCommand(
$downtime,
array(
(object) array(
'host_name' => 'Testhost'
)
)
);
$this->assertCommandSucceeded(
'SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME;Testhost;25;26;1;0;0;me;test'
);
} catch (Exception $e) { } catch (Exception $e) {
$this->cleanup(); $this->cleanup();
throw $e; throw $e;