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\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Objects\IcingaZone;
use Icinga\Module\Director\Util;
use Icinga\Module\Director\Web\Controller\ObjectController;
@ -130,74 +131,56 @@ class HostController extends ObjectController
$tables[$title] = $table->setTitle($title);
}
$title = $this->translate('Service sets');
$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)
->setTitle($title)
->setConnection($db);
->setConnection($db);
$tables[$title] = $table;
$this->view->tables = $tables;
}
/**
* @deprecated
*/
public function appliedserviceAction()
{
$db = $this->db();
/** @var IcingaHost $host */
$host = $this->object;
$serviceName = $this->params->get('service');
$applied = $host->vars()->get($db->settings()->magic_apply_for);
$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();
}
$serviceId = $this->params->get('service_id');
$parent = IcingaService::loadWithAutoIncId($serviceId, $db);
$serviceName = $parent->object_name;
$service = IcingaService::create(array(
'imports' => $parent,
'object_type' => 'apply',
'object_name' => $serviceName,
'host_id' => $host->id,
'vars' => $host->getOverriddenServiceVars($serviceName),
), $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->translate('Applied service: %s'),
$serviceName
);
$this->getTabs()->activate('services');
$this->view->form = $this->loadForm('IcingaService')
->setDb($db)
->setHost($host)
->setHostGenerated()
->setApplyGenerated($parent)
->setObject($service)
->handleRequest()
;
$this->setViewScript('object/form');
$this->commonForServices();
}
public function inheritedserviceAction()
@ -230,16 +213,68 @@ class HostController extends ObjectController
$serviceName
);
$this->getTabs()->activate('services');
$this->view->form = $this->loadForm('IcingaService')
->setDb($db)
->setHost($host)
->setInheritedFrom($from->object_name)
->setObject($service);
$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');
}

View File

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