IcingaServiceForm: handle overrides

fixes #12546
This commit is contained in:
Thomas Gelf 2016-09-08 11:25:48 +00:00
parent 4f4b6eb63a
commit 6cc1a90b0a
4 changed files with 129 additions and 18 deletions

View File

@ -6,6 +6,7 @@ use Exception;
use Icinga\Exception\NotFoundError;
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\IcingaZone;
use Icinga\Module\Director\Util;
@ -98,6 +99,7 @@ class HostController extends ObjectController
foreach ($resolver->fetchResolvedParents() as $parent) {
$table = $this->loadTable('IcingaHostService')
->setHost($parent)
->setInheritedBy($host)
->enforceFilter('host_id', $parent->id)
->setConnection($db);
if (! count($table)) {
@ -133,7 +135,6 @@ class HostController extends ObjectController
'vars' => $props->vars->getValue(),
), $db);
$service = IcingaService::create(array(
'object_type' => 'apply',
'object_name' => $serviceName,
@ -172,6 +173,49 @@ class HostController extends ObjectController
$this->setViewScript('object/form');
}
public function inheritedserviceAction()
{
$db = $this->db();
$host = $this->object;
$serviceName = $this->params->get('service');
$from = IcingaHost::load($this->params->get('inheritedFrom'), $this->db());
$parent = IcingaService::load(
array(
'object_name' => $serviceName,
'host_id' => $from->id
),
$this->db()
);
$parent->object_name = $from->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('Inherited service: %s'),
$serviceName
);
$this->getTabs()->activate('services');
$this->view->form = $this->loadForm('IcingaService')
->setDb($db)
->setHost($host)
->setInheritedFrom($from->object_name)
->setObject($service)
->handleRequest()
;
$this->setViewScript('object/form');
}
public function agentAction()
{
switch ($this->params->get('download')) {

View File

@ -14,18 +14,30 @@ class IcingaServiceForm extends DirectorObjectForm
private $hostGenerated = false;
private $inheritedFrom;
public function setHostGenerated($hostGenerated = true)
{
$this->hostGenerated = $hostGenerated;
return $this;
}
public function setInheritedFrom($hostname)
{
$this->inheritedFrom = $hostname;
return $this;
}
public function setup()
{
if ($this->hostGenerated) {
return $this->setupHostGenerated();
}
if ($this->inheritedFrom) {
return $this->setupInherited();
}
if (!$this->isNew() && $this->host === null) {
$this->host = $this->object->getResolvedRelated('host');
}
@ -85,6 +97,30 @@ class IcingaServiceForm extends DirectorObjectForm
}
}
protected function setupInherited()
{
$msg = $this->translate(
'This service has been inherited from %s. Still, you might want'
. ' to change the following properties for this host only.'
);
$name = $this->inheritedFrom;
$link = $this->getView()->qlink(
$name,
'director/service',
array(
'host' => $name,
'name' => $this->object->object_name,
),
array('data-base-target' => '_next')
);
$this->addHtmlHint(sprintf($msg, $link));
$this->setSubmitLabel(
$this->translate('Override vars')
);
}
protected function addAssignmentElements()
{
if (!$this->object || !$this->object->isApplyRule()) {
@ -240,26 +276,45 @@ class IcingaServiceForm extends DirectorObjectForm
return $db->fetchPairs($select);
}
public function onSuccess()
protected function succeedForOverrides()
{
if (! $this->hostGenerated) {
return parent::onSuccess();
}
$modified =array();
$vars = (object) array();
foreach ($this->object->vars() as $key => $var) {
$modified[$key] = $var->getValue();
$vars->$key = $var->getValue();
}
$host = $this->host;
if (empty($modified)) {
unset($host->vars()->_director_apply_override);
} else {
$host->vars()->_director_apply_override = $modified;
}
$serviceName = $this->object->object_name;
$this->host->overrideServiceVars($serviceName, $vars);
if ($host->hasBeenModified()) {
$msg = sprintf(
empty((array) $vars)
? $this->translate('All overrides have been removed from "%s"')
: $this->translate('The given properties have been stored for "%s"'),
$this->translate($host->object_name)
);
$host->store();
} else {
if ($this->isApiRequest()) {
$this->setHttpResponseCode(304);
}
$msg = $this->translate('No action taken, object has not been modified');
}
$this->redirectOnSuccess($msg);
}
public function onSuccess()
{
if ($this->hostGenerated || $this->inheritedFrom) {
return $this->succeedForOverrides();
}
return parent::onSuccess();
}
}

View File

@ -11,6 +11,8 @@ class IcingaHostServiceTable extends QuickTable
protected $host;
protected $inheritedBy;
protected $searchColumns = array(
'service',
);
@ -38,8 +40,24 @@ class IcingaHostServiceTable extends QuickTable
return $this;
}
public function setInheritedBy(IcingaHost $host)
{
$this->inheritedBy = $host;
return $this;
}
protected function getActionUrl($row)
{
if ($target = $this->inheritedBy) {
$params = array(
'name' => $target->object_name,
'service' => $row->service,
'inheritedFrom' => $row->host,
);
return $this->url('director/host/inheritedservice', $params);
}
if ($row->object_type === 'apply') {
$params['id'] = $row->id;
} else {

View File

@ -10,13 +10,7 @@
<?= $this->form ?>
<?php foreach ($this->tables as $key => $table): ?>
<?php if (count($table)): ?>
<?php if ($key === 0 || $key === 1): ?>
<?= $table->render() ?>
<?php else: ?>
<div data-base-target="_next">
<?= $table->render() ?>
</div>
<?php endif ?>
<?php endif ?>
<?php endforeach ?>
</div>