IcingaService (on host): add applied services...

...and improve ServiceSet handling

fixes #13849
This commit is contained in:
Thomas Gelf 2017-01-02 10:51:54 +01:00
parent e05eb60286
commit 886ba0e168
2 changed files with 89 additions and 49 deletions

View File

@ -9,6 +9,7 @@ use Icinga\Module\Director\IcingaConfig\AgentWizard;
use Icinga\Module\Director\Objects\IcingaEndpoint; use Icinga\Module\Director\Objects\IcingaEndpoint;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService; use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Objects\IcingaZone; use Icinga\Module\Director\Objects\IcingaZone;
use Icinga\Module\Director\Util; use Icinga\Module\Director\Util;
use Icinga\Module\Director\Web\Controller\ObjectController; use Icinga\Module\Director\Web\Controller\ObjectController;
@ -130,74 +131,56 @@ class HostController extends ObjectController
$tables[$title] = $table->setTitle($title); $tables[$title] = $table->setTitle($title);
} }
$title = $this->translate('Service sets');
$table = $this->loadTable('IcingaHostServiceSet') $table = $this->loadTable('IcingaHostServiceSet')
->setHost($host)
->setConnection($db);
$tables[$this->translate('Service sets')] = $table;
$title = $this->translate('Applied services');
$table = $this->loadTable('IcingaHostAppliedServices')
->setHost($host) ->setHost($host)
->setTitle($title) ->setTitle($title)
->setConnection($db); ->setConnection($db);
$tables[$title] = $table; $tables[$title] = $table;
$this->view->tables = $tables; $this->view->tables = $tables;
} }
/**
* @deprecated
*/
public function appliedserviceAction() public function appliedserviceAction()
{ {
$db = $this->db(); $db = $this->db();
/** @var IcingaHost $host */
$host = $this->object; $host = $this->object;
$serviceName = $this->params->get('service'); $serviceId = $this->params->get('service_id');
$parent = IcingaService::loadWithAutoIncId($serviceId, $db);
$applied = $host->vars()->get($db->settings()->magic_apply_for); $serviceName = $parent->object_name;
$props = $applied->{$serviceName};
$parent = IcingaService::create(array(
'object_type' => 'template',
'object_name' => $this->translate('Host'),
), $db);
if (isset($props->vars)) {
$parent->vars = $props->vars->getValue();
}
$service = IcingaService::create(array( $service = IcingaService::create(array(
'imports' => $parent,
'object_type' => 'apply', 'object_type' => 'apply',
'object_name' => $serviceName, 'object_name' => $serviceName,
'host_id' => $host->id, 'host_id' => $host->id,
'vars' => $host->getOverriddenServiceVars($serviceName), 'vars' => $host->getOverriddenServiceVars($serviceName),
), $db); ), $db);
if (isset($props->templates) && $templates = $props->templates->getValue()) {
$imports = $templates;
} else {
$imports = $serviceName;
}
if (! is_array($imports)) {
$imports = array($imports);
}
// TODO: Validation for $imports? They might not exist!
array_push($imports, $parent);
$service->imports = $imports;
$this->view->title = sprintf( $this->view->title = sprintf(
$this->translate('Applied service: %s'), $this->translate('Applied service: %s'),
$serviceName $serviceName
); );
$this->getTabs()->activate('services');
$this->view->form = $this->loadForm('IcingaService') $this->view->form = $this->loadForm('IcingaService')
->setDb($db) ->setDb($db)
->setHost($host) ->setHost($host)
->setHostGenerated() ->setApplyGenerated($parent)
->setObject($service) ->setObject($service)
->handleRequest()
; ;
$this->setViewScript('object/form'); $this->commonForServices();
} }
public function inheritedserviceAction() public function inheritedserviceAction()
@ -230,16 +213,68 @@ class HostController extends ObjectController
$serviceName $serviceName
); );
$this->getTabs()->activate('services');
$this->view->form = $this->loadForm('IcingaService') $this->view->form = $this->loadForm('IcingaService')
->setDb($db) ->setDb($db)
->setHost($host) ->setHost($host)
->setInheritedFrom($from->object_name) ->setInheritedFrom($from->object_name)
->setObject($service); ->setObject($service);
$this->view->form->setResolvedImports(); $this->view->form->setResolvedImports();
$this->view->form->handleRequest(); $this->commonForServices();
}
public function servicesetserviceAction()
{
$db = $this->db();
/** @var IcingaHost $host */
$host = $this->object;
$serviceName = $this->params->get('service');
$set = IcingaServiceSet::loadWithAutoIncId((int) $this->params->get('serviceSet'), $this->db());
$parent = IcingaService::load(
array(
'object_type' => 'apply',
'object_name' => $serviceName,
'service_set_id' => (int) $set->id
),
$this->db()
);
$parent->object_name = $set->object_name;
$service = IcingaService::create(array(
'object_type' => 'apply',
'object_name' => $serviceName,
'host_id' => $host->id,
'imports' => array($parent),
'vars' => $host->getOverriddenServiceVars($serviceName),
), $db);
$this->view->title = sprintf(
$this->translate('Service "%s" (from set "%s")'),
$serviceName,
$set->object_name
);
$this->view->form = $this->loadForm('IcingaService')
->setDb($db)
->setHost($host)
->setInheritedFrom($parent->object_name)
->setObject($service);
$this->view->form->setResolvedImports();
$this->commonForServices();
}
protected function commonForServices()
{
$host = $this->object;
$this->view->actionLinks = $this->view->qlink(
$this->translate('back'),
'director/host/services',
array('name' => $host->object_name),
array('class' => 'icon-left-big')
);
$this->getTabs()->activate('services');
$this->view->form->handleRequest();
$this->setViewScript('object/form'); $this->setViewScript('object/form');
} }

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Forms; namespace Icinga\Module\Director\Forms;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Data\PropertiesFilter\ArrayCustomVariablesFilter; use Icinga\Module\Director\Data\PropertiesFilter\ArrayCustomVariablesFilter;
use Icinga\Module\Director\Exception\NestingError; use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\Web\Form\DirectorObjectForm; use Icinga\Module\Director\Web\Form\DirectorObjectForm;
@ -21,13 +22,13 @@ class IcingaServiceForm extends DirectorObjectForm
/** @var IcingaService */ /** @var IcingaService */
protected $object; protected $object;
private $hostGenerated = false; private $applyGenerated;
private $inheritedFrom; private $inheritedFrom;
public function setHostGenerated($hostGenerated = true) public function setApplyGenerated(IcingaService $applyGenerated)
{ {
$this->hostGenerated = $hostGenerated; $this->applyGenerated = $applyGenerated;
return $this; return $this;
} }
@ -63,7 +64,7 @@ class IcingaServiceForm extends DirectorObjectForm
protected function providesOverrides() protected function providesOverrides()
{ {
return ($this->object && $this->object->usesVarOverrides()) return ($this->object && $this->object->usesVarOverrides())
|| $this->hostGenerated || $this->applyGenerated
|| $this->inheritedFrom; || $this->inheritedFrom;
} }
@ -135,6 +136,8 @@ class IcingaServiceForm extends DirectorObjectForm
protected function getOverrideHint() protected function getOverrideHint()
{ {
$view = $this->getView();
if ($this->object && $this->object->usesVarOverrides()) { if ($this->object && $this->object->usesVarOverrides()) {
return $this->translate( return $this->translate(
'This service has been generated in an automated way, but still' 'This service has been generated in an automated way, but still'
@ -142,14 +145,16 @@ class IcingaServiceForm extends DirectorObjectForm
); );
} }
if ($this->hostGenerated) { if ($this->applyGenerated) {
return $this->translate( return $view->escape(sprintf(
'This service has been generated from host properties.' $this->translate(
); 'This service has been generated using an apply rule, assigned where %s'
),
Filter::fromQueryString($this->applyGenerated->assign_filter)
));
} }
if ($this->inheritedFrom) { if ($this->inheritedFrom) {
$view = $this->getView();
$msg = $view->escape($this->translate( $msg = $view->escape($this->translate(
'This service has been inherited from %s. Still, you might want' 'This service has been inherited from %s. Still, you might want'
. ' to change the following properties for this host only.' . ' to change the following properties for this host only.'
@ -432,7 +437,7 @@ class IcingaServiceForm extends DirectorObjectForm
public function onSuccess() public function onSuccess()
{ {
if ($this->hostGenerated || $this->inheritedFrom || $this->object->usesVarOverrides()) { if ($this->applyGenerated || $this->inheritedFrom || $this->object->usesVarOverrides()) {
return $this->succeedForOverrides(); return $this->succeedForOverrides();
} }