Remove CommandType interface

Moved BaseCommand from the monitoring module to the application's library.

refs #4580
This commit is contained in:
Eric Lippmann 2013-09-16 14:39:14 +02:00
parent 4964d84e78
commit bf012fbd7e
13 changed files with 86 additions and 174 deletions

View File

@ -26,18 +26,14 @@
*/ */
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Command; namespace Icinga\Protocol\Commandpipe;
use Icinga\Exception\NotImplementedError; use Icinga\Exception\ProgrammingError;
use Icinga\Protocol\Commandpipe\CommandType;
/** /**
* Base class for any concrete command implementation * Base class for any concrete command implementation
*
* Provides some example methods and often used routines. When implementing
* a new command one is encouraged to override one of those examples.
*/ */
class BaseCommand implements CommandType abstract class BaseCommand
{ {
/** /**
* Whether hosts are ignored in case of a host- or servicegroup * Whether hosts are ignored in case of a host- or servicegroup
@ -116,14 +112,11 @@ class BaseCommand implements CommandType
} }
/** /**
* Return this command's parameters properly arranged in an array * Return this command's arguments in the order expected by the actual command definition
* *
* @return array * @return array
*/ */
public function getParameters() abstract public function getParameters();
{
throw new NotImplementedError();
}
/** /**
* Return the command as a string with the given host being inserted * Return the command as a string with the given host being inserted
@ -132,10 +125,7 @@ class BaseCommand implements CommandType
* *
* @return string The string representation of the command * @return string The string representation of the command
*/ */
public function getHostCommand($hostname) abstract public function getHostCommand($hostname);
{
throw new NotImplementedError();
}
/** /**
* Return the command as a string with the given host and service being inserted * Return the command as a string with the given host and service being inserted
@ -145,10 +135,7 @@ class BaseCommand implements CommandType
* *
* @return string The string representation of the command * @return string The string representation of the command
*/ */
public function getServiceCommand($hostname, $servicename) abstract public function getServiceCommand($hostname, $servicename);
{
throw new NotImplementedError();
}
/** /**
* Return the command as a string with the given hostgroup being inserted * Return the command as a string with the given hostgroup being inserted
@ -159,7 +146,7 @@ class BaseCommand implements CommandType
*/ */
public function getHostgroupCommand($hostgroup) public function getHostgroupCommand($hostgroup)
{ {
throw new NotImplementedError(); throw new ProgrammingError(get_class($this) . ' does not provide a hostgroup command');
} }
/** /**
@ -171,6 +158,6 @@ class BaseCommand implements CommandType
*/ */
public function getServicegroupCommand($servicegroup) public function getServicegroupCommand($servicegroup)
{ {
throw new NotImplementedError(); throw new ProgrammingError(get_class($this) . ' does not provide a servicegroup command');
} }
} }

View File

@ -139,10 +139,10 @@ class CommandPipe
/** /**
* Send a command to the icinga pipe * Send a command to the icinga pipe
* *
* @param \Icinga\Protocol\Commandpipe\CommandType $command * @param Command $command
* @param array $objects * @param array $objects
*/ */
public function sendCommand(CommandType $command, array $objects) public function sendCommand(BaseCommand $command, array $objects)
{ {
foreach ($objects as $object) { foreach ($objects as $object) {
$objectType = $this->getObjectType($object); $objectType = $this->getObjectType($object);

View File

@ -1,36 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Protocol\Commandpipe;
/**
* Interface definition for command objects processed by the CommandPipe
*/
interface CommandType
{
}

View File

@ -29,7 +29,7 @@
namespace Icinga\Protocol\Commandpipe; namespace Icinga\Protocol\Commandpipe;
/** /**
* Container for comment information that can be send to icinga's external command pipe * Container for comment information that can be send to Icinga's external command pipe
*/ */
class Comment class Comment
{ {

View File

@ -28,6 +28,7 @@
namespace Icinga\Module\Monitoring\Command; namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
use Icinga\Protocol\Commandpipe\Comment; use Icinga\Protocol\Commandpipe\Comment;
/** /**
@ -88,7 +89,7 @@ class AcknowledgeCommand extends BaseCommand
*/ */
public function setExpire($expireTime) public function setExpire($expireTime)
{ {
$this->expireTime = intval($expireTime); $this->expireTime = (int) $expireTime;
return $this; return $this;
} }
@ -135,7 +136,6 @@ class AcknowledgeCommand extends BaseCommand
* Return this command's parameters properly arranged in an array * Return this command's parameters properly arranged in an array
* *
* @return array * @return array
*
* @see BaseCommand::getParameters() * @see BaseCommand::getParameters()
*/ */
public function getParameters() public function getParameters()
@ -161,7 +161,6 @@ class AcknowledgeCommand extends BaseCommand
* @param string $hostname The name of the host to insert * @param string $hostname The name of the host to insert
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getHostCommand() * @see BaseCommand::getHostCommand()
*/ */
public function getHostCommand($hostname) public function getHostCommand($hostname)
@ -178,7 +177,6 @@ class AcknowledgeCommand extends BaseCommand
* @param string $servicename The name of the service to insert * @param string $servicename The name of the service to insert
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand() * @see BaseCommand::getServiceCommand()
*/ */
public function getServiceCommand($hostname, $servicename) public function getServiceCommand($hostname, $servicename)

View File

@ -28,6 +28,7 @@
namespace Icinga\Module\Monitoring\Command; namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
use Icinga\Protocol\Commandpipe\Comment; use Icinga\Protocol\Commandpipe\Comment;
/** /**
@ -67,18 +68,22 @@ class AddCommentCommand extends BaseCommand
return $this; return $this;
} }
public function getParameters()
{
return $this->comment->getParameters();
}
/** /**
* Return the command as a string with the given host being inserted * Return the command as a string with the given host being inserted
* *
* @param string $hostname The name of the host to insert * @param string $hostname The name of the host to insert
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getHostCommand() * @see BaseCommand::getHostCommand()
*/ */
public function getHostCommand($hostname) public function getHostCommand($hostname)
{ {
return sprintf('ADD_HOST_COMMENT;%s;', $hostname) . implode(';', $this->comment->getParameters()); return sprintf('ADD_HOST_COMMENT;%s;', $hostname) . implode(';', $this->getParameters());
} }
/** /**
@ -88,12 +93,11 @@ class AddCommentCommand extends BaseCommand
* @param string $servicename The name of the service to insert * @param string $servicename The name of the service to insert
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand() * @see BaseCommand::getServiceCommand()
*/ */
public function getServiceCommand($hostname, $servicename) public function getServiceCommand($hostname, $servicename)
{ {
return sprintf('ADD_SVC_COMMENT;%s;%s;', $hostname, $servicename) return sprintf('ADD_SVC_COMMENT;%s;%s;', $hostname, $servicename)
. implode(';', $this->comment->getParameters()); . implode(';', $this->getParameters());
} }
} }

View File

@ -28,6 +28,7 @@
namespace Icinga\Module\Monitoring\Command; namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
use Icinga\Protocol\Commandpipe\Comment; use Icinga\Protocol\Commandpipe\Comment;
/** /**
@ -113,7 +114,6 @@ class CustomNotificationCommand extends BaseCommand
* Return this command's parameters properly arranged in an array * Return this command's parameters properly arranged in an array
* *
* @return array * @return array
*
* @see BaseCommand::getParameters() * @see BaseCommand::getParameters()
*/ */
public function getParameters() public function getParameters()
@ -134,7 +134,6 @@ class CustomNotificationCommand extends BaseCommand
* @param string $hostname The name of the host to insert * @param string $hostname The name of the host to insert
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getHostCommand() * @see BaseCommand::getHostCommand()
*/ */
public function getHostCommand($hostname) public function getHostCommand($hostname)
@ -149,7 +148,6 @@ class CustomNotificationCommand extends BaseCommand
* @param string $servicename The name of the service to insert * @param string $servicename The name of the service to insert
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand() * @see BaseCommand::getServiceCommand()
*/ */
public function getServiceCommand($hostname, $servicename) public function getServiceCommand($hostname, $servicename)

View File

@ -28,6 +28,8 @@
namespace Icinga\Module\Monitoring\Command; namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
/** /**
* Command to delay a notification * Command to delay a notification
*/ */
@ -59,7 +61,7 @@ class DelayNotificationCommand extends BaseCommand
*/ */
public function setDelay($seconds) public function setDelay($seconds)
{ {
$this->delay = intval($seconds); $this->delay = (int) $seconds;
return $this; return $this;
} }
@ -67,7 +69,6 @@ class DelayNotificationCommand extends BaseCommand
* Return this command's parameters properly arranged in an array * Return this command's parameters properly arranged in an array
* *
* @return array * @return array
*
* @see BaseCommand::getParameters() * @see BaseCommand::getParameters()
*/ */
public function getParameters() public function getParameters()
@ -81,7 +82,6 @@ class DelayNotificationCommand extends BaseCommand
* @param string $hostname The name of the host to insert * @param string $hostname The name of the host to insert
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getHostCommand() * @see BaseCommand::getHostCommand()
*/ */
public function getHostCommand($hostname) public function getHostCommand($hostname)
@ -96,7 +96,6 @@ class DelayNotificationCommand extends BaseCommand
* @param string $servicename The name of the service to insert * @param string $servicename The name of the service to insert
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand() * @see BaseCommand::getServiceCommand()
*/ */
public function getServiceCommand($hostname, $servicename) public function getServiceCommand($hostname, $servicename)

View File

@ -28,6 +28,8 @@
namespace Icinga\Module\Monitoring\Command; namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
/** /**
* Command to schedule checks * Command to schedule checks
*/ */
@ -68,7 +70,7 @@ class ScheduleCheckCommand extends BaseCommand
*/ */
public function setCheckTime($checkTime) public function setCheckTime($checkTime)
{ {
$this->checkTime = intval($checkTime); $this->checkTime = (int) $checkTime;
return $this; return $this;
} }
@ -89,7 +91,6 @@ class ScheduleCheckCommand extends BaseCommand
* Return this command's parameters properly arranged in an array * Return this command's parameters properly arranged in an array
* *
* @return array * @return array
*
* @see BaseCommand::getParameters() * @see BaseCommand::getParameters()
*/ */
public function getParameters() public function getParameters()
@ -101,10 +102,8 @@ class ScheduleCheckCommand 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 or all of it's services
* *
* @param string $hostname The name of the host to insert * @param string $hostname The name of the host to insert
* @param type $servicesOnly Whether the given host or each of it's services is checked
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getHostCommand() * @see BaseCommand::getHostCommand()
*/ */
public function getHostCommand($hostname) public function getHostCommand($hostname)
@ -123,7 +122,6 @@ class ScheduleCheckCommand extends BaseCommand
* @param string $servicename The name of the service to insert * @param string $servicename The name of the service to insert
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand() * @see BaseCommand::getServiceCommand()
*/ */
public function getServiceCommand($hostname, $servicename) public function getServiceCommand($hostname, $servicename)

View File

@ -29,6 +29,7 @@
namespace Icinga\Module\Monitoring\Command; namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\Comment; use Icinga\Protocol\Commandpipe\Comment;
use Icinga\Protocol\Commandpipe\BaseCommand;
/** /**
* Command for scheduling a new downtime * Command for scheduling a new downtime
@ -93,7 +94,7 @@ class ScheduleDowntimeCommand extends BaseCommand
*/ */
public function setStart($startTime) public function setStart($startTime)
{ {
$this->startTime = intval($startTime); $this->startTime = (int) $startTime;
return $this; return $this;
} }
@ -106,7 +107,7 @@ class ScheduleDowntimeCommand extends BaseCommand
*/ */
public function setEnd($endTime) public function setEnd($endTime)
{ {
$this->endTime = intval($endTime); $this->endTime = (int) $endTime;
return $this; return $this;
} }
@ -145,7 +146,7 @@ class ScheduleDowntimeCommand extends BaseCommand
*/ */
public function setDuration($duration) public function setDuration($duration)
{ {
$this->duration = intval($duration); $this->duration = (int) $duration;
return $this; return $this;
} }
@ -158,7 +159,7 @@ class ScheduleDowntimeCommand extends BaseCommand
*/ */
public function setTriggerId($triggerId) public function setTriggerId($triggerId)
{ {
$this->triggerId = intval($triggerId); $this->triggerId = (int) $triggerId;
return $this; return $this;
} }
@ -199,7 +200,6 @@ class ScheduleDowntimeCommand extends BaseCommand
* Return this command's parameters properly arranged in an array * Return this command's parameters properly arranged in an array
* *
* @return array * @return array
*
* @see BaseCommand::getParameters() * @see BaseCommand::getParameters()
*/ */
public function getParameters() public function getParameters()

View File

@ -28,6 +28,8 @@
namespace Icinga\Module\Monitoring\Command; namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
/** /**
* Command to submit passive check results * Command to submit passive check results
*/ */
@ -77,7 +79,7 @@ class SubmitPassiveCheckresultCommand extends BaseCommand
*/ */
public function setState($state) public function setState($state)
{ {
$this->state = intval($state); $this->state = (int) $state;
return $this; return $this;
} }
@ -110,7 +112,6 @@ class SubmitPassiveCheckresultCommand extends BaseCommand
* Return this command's parameters properly arranged in an array * Return this command's parameters properly arranged in an array
* *
* @return array * @return array
*
* @see BaseCommand::getParameters() * @see BaseCommand::getParameters()
*/ */
public function getParameters() public function getParameters()
@ -127,7 +128,6 @@ class SubmitPassiveCheckresultCommand extends BaseCommand
* @param string $hostname The name of the host to insert * @param string $hostname The name of the host to insert
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getHostCommand() * @see BaseCommand::getHostCommand()
*/ */
public function getHostCommand($hostname) public function getHostCommand($hostname)
@ -142,7 +142,6 @@ class SubmitPassiveCheckresultCommand extends BaseCommand
* @param string $servicename The name of the service to insert * @param string $servicename The name of the service to insert
* *
* @return string The string representation of the command * @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand() * @see BaseCommand::getServiceCommand()
*/ */
public function getServiceCommand($hostname, $servicename) public function getServiceCommand($hostname, $servicename)

View File

@ -1,33 +0,0 @@
<?php
namespace Tests\Icinga\Protocol\Commandpipe;
use Icinga\Protocol\Commandpipe\Comment;
use Icinga\Module\Monitoring\Command\AcknowledgeCommand;
require_once("../../library/Icinga/Protocol/Commandpipe/Comment.php");
require_once("../../library/Icinga/Protocol/Commandpipe/CommandType.php");
require_once("../../library/Icinga/Protocol/Commandpipe/CommandPipe.php");
require_once('../../modules/monitoring/library/Monitoring/Command/BaseCommand.php');
require_once('../../modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php');
class AcknowledgementTest extends \PHPUnit_Framework_TestCase
{
public function testAcknowledgeHostMessage()
{
$ack = new AcknowledgeCommand(new Comment("author", "commentdata"));
$this->assertEquals("ACKNOWLEDGE_HOST_PROBLEM;foo;0;0;0;author;commentdata", $ack->getHostCommand('foo'));
$ack->setExpire(1000);
$this->assertEquals("ACKNOWLEDGE_HOST_PROBLEM_EXPIRE;bar;0;0;0;1000;author;commentdata", $ack->getHostCommand('bar'));
}
public function testAcknowledgeServiceMessage()
{
$ack = new AcknowledgeCommand(new Comment("author", "commentdata"));
$this->assertEquals("ACKNOWLEDGE_SVC_PROBLEM;foo;bar;0;0;0;author;commentdata", $ack->getServiceCommand('foo', 'bar'));
$ack->setExpire(1000);
$this->assertEquals("ACKNOWLEDGE_SVC_PROBLEM_EXPIRE;bar;foo;0;0;0;1000;author;commentdata", $ack->getServiceCommand('bar', 'foo'));
}
}

View File

@ -28,7 +28,7 @@
namespace Tests\Icinga\Protocol\Commandpipe; namespace Tests\Icinga\Protocol\Commandpipe;
require_once("./library/Icinga/LibraryLoader.php"); require_once('./library/Icinga/LibraryLoader.php');
use Test\Icinga\LibraryLoader; use Test\Icinga\LibraryLoader;
@ -36,25 +36,23 @@ class CommandPipeLoader extends LibraryLoader {
public static function requireLibrary() public static function requireLibrary()
{ {
require_once("Zend/Config.php"); require_once('Zend/Config.php');
require_once("Zend/Log.php"); require_once('Zend/Log.php');
require_once("../../library/Icinga/Application/Logger.php"); require_once(realpath('../../library/Icinga/Application/Logger.php'));
require_once(realpath('../../library/Icinga/Protocol/Commandpipe/BaseCommand.php'));
require_once("../../library/Icinga/Protocol/Commandpipe/Comment.php"); require_once(realpath('../../library/Icinga/Protocol/Commandpipe/Comment.php'));
require_once("../../library/Icinga/Protocol/Commandpipe/CommandType.php"); require_once(realpath('../../library/Icinga/Protocol/Commandpipe/CommandPipe.php'));
require_once("../../library/Icinga/Protocol/Commandpipe/CommandPipe.php"); require_once(realpath('../../library/Icinga/Protocol/Commandpipe/PropertyModifier.php'));
require_once("../../library/Icinga/Protocol/Commandpipe/PropertyModifier.php"); require_once(realpath('../../library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php'));
require_once("../../library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php"); require_once(realpath('../../library/Icinga/Protocol/Commandpipe/Transport/Transport.php'));
require_once("../../library/Icinga/Protocol/Commandpipe/Transport/Transport.php"); require_once(realpath('../../library/Icinga/Protocol/Commandpipe/Transport/SecureShell.php'));
require_once("../../library/Icinga/Protocol/Commandpipe/Transport/SecureShell.php"); require_once(realpath('../../library/Icinga/Protocol/Commandpipe/Transport/LocalPipe.php'));
require_once("../../library/Icinga/Protocol/Commandpipe/Transport/LocalPipe.php"); require_once(realpath('../../modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php'));
require_once('../../modules/monitoring/library/Monitoring/Command/BaseCommand.php'); require_once(realpath('../../modules/monitoring/library/Monitoring/Command/AddCommentCommand.php'));
require_once('../../modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php'); require_once(realpath('../../modules/monitoring/library/Monitoring/Command/ScheduleDowntimeCommand.php'));
require_once('../../modules/monitoring/library/Monitoring/Command/AddCommentCommand.php'); require_once(realpath('../../modules/monitoring/library/Monitoring/Command/CustomNotificationCommand.php'));
require_once('../../modules/monitoring/library/Monitoring/Command/ScheduleDowntimeCommand.php'); require_once(realpath('../../modules/monitoring/library/Monitoring/Command/DelayNotificationCommand.php'));
require_once('../../modules/monitoring/library/Monitoring/Command/CustomNotificationCommand.php'); require_once(realpath('../../modules/monitoring/library/Monitoring/Command/ScheduleCheckCommand.php'));
require_once('../../modules/monitoring/library/Monitoring/Command/DelayNotificationCommand.php'); require_once(realpath('../../modules/monitoring/library/Monitoring/Command/SubmitPassiveCheckresultCommand.php'));
require_once('../../modules/monitoring/library/Monitoring/Command/ScheduleCheckCommand.php');
require_once('../../modules/monitoring/library/Monitoring/Command/SubmitPassiveCheckresultCommand.php');
} }
} }