OverriddenVarsResolver: externalize lookup logic

This now also takes into account properties overridden on intermediate
Host templates
This commit is contained in:
Thomas Gelf 2019-12-04 12:36:47 +01:00
parent e0c0662be1
commit 3e98d828e5
3 changed files with 138 additions and 48 deletions

View File

@ -66,6 +66,7 @@ class IcingaServiceForm extends DirectorObjectForm
}
if ($this->host && $this->set) {
// Probably never reached, as providesOverrides includes this
$this->setupOnHostForSet();
return;
@ -91,7 +92,7 @@ class IcingaServiceForm extends DirectorObjectForm
}
}
protected function providesOverrides()
public function providesOverrides()
{
return $this->applyGenerated
|| $this->inheritedFrom
@ -172,6 +173,14 @@ class IcingaServiceForm extends DirectorObjectForm
}
}
/**
* @return IcingaHost|null
*/
public function getHost()
{
return $this->host;
}
/**
* @param IcingaService $service
* @return IcingaService

View File

@ -6,7 +6,9 @@ use Icinga\Module\Director\Core\Json;
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Exception\DuplicateKeyException;
use Icinga\Module\Director\Forms\IcingaServiceForm;
use Icinga\Module\Director\Hook\DataTypeHook;
use Icinga\Module\Director\Resolver\OverriddenVarsResolver;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use InvalidArgumentException;
use Zend_Form_Element as ZfElement;
@ -243,57 +245,80 @@ class DirectorDatafield extends DbObjectWithSettings
protected function applyObjectData(ZfElement $el, DirectorObjectForm $form)
{
$object = $form->getObject();
if ($object instanceof IcingaObject) {
if ($object->isTemplate()) {
$el->setRequired(false);
}
if (! ($object instanceof IcingaObject)) {
return;
}
if ($object->isTemplate()) {
$el->setRequired(false);
}
$varname = $this->get('varname');
$varName = $this->get('varname');
$inherited = $origin = null;
$inherited = $object->getInheritedVar($varname);
if (null !== $inherited) {
$form->setInheritedValue(
$el,
$inherited,
$object->getOriginForVar($varname)
);
} elseif ($object->hasRelation('check_command')) {
// TODO: Move all of this elsewhere and test it
try {
/** @var IcingaCommand $command */
$command = $object->getResolvedRelated('check_command');
if ($command === null) {
return;
}
$inherited = $command->vars()->get($varname);
$inheritedFrom = null;
if ($inherited !== null) {
$inherited = $inherited->getValue();
}
if ($inherited === null) {
$inherited = $command->getResolvedVar($varname);
if ($inherited === null) {
$inheritedFrom = $command->getOriginForVar($varname);
}
} else {
$inheritedFrom = $command->getObjectName();
}
$inherited = $command->getResolvedVar($varname);
if (null !== $inherited) {
$form->setInheritedValue(
$el,
$inherited,
$inheritedFrom
);
}
} catch (\Exception $e) {
// Ignore failures
if ($form instanceof IcingaServiceForm && $form->providesOverrides()) {
$resolver = new OverriddenVarsResolver($form->getDb());
$vars = $resolver->resolveFor($form->getHost(), $object);
foreach ($vars as $host => $values) {
if (\property_exists($values, $varName)) {
$inherited = $values->$varName;
$origin = $host;
}
}
}
if ($inherited === null) {
$inherited = $object->getInheritedVar($varName);
if (null !== $inherited) {
$origin = $object->getOriginForVar($varName);
}
}
if ($inherited === null) {
$cmd = $this->eventuallyGetResolvedCommandVar($object, $varName);
if ($cmd !== null) {
list($inherited, $origin) = $cmd;
}
}
if ($inherited !== null) {
$form->setInheritedValue($el, $inherited, $origin);
}
}
protected function eventuallyGetResolvedCommandVar(IcingaObject $object, $varName)
{
if (! $object->hasRelation('check_command')) {
return null;
}
// TODO: Move all of this elsewhere and test it
try {
/** @var IcingaCommand $command */
$command = $object->getResolvedRelated('check_command');
if ($command === null) {
return null;
}
$inherited = $command->vars()->get($varName);
$inheritedFrom = null;
if ($inherited !== null) {
$inherited = $inherited->getValue();
}
if ($inherited === null) {
$inherited = $command->getResolvedVar($varName);
if ($inherited === null) {
$inheritedFrom = $command->getOriginForVar($varName);
}
} else {
$inheritedFrom = $command->getObjectName();
}
$inherited = $command->getResolvedVar($varName);
return [$inherited, $inheritedFrom];
} catch (\Exception $e) {
return null;
}
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Icinga\Module\Director\Resolver;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Util\Json;
class OverriddenVarsResolver
{
/** @var \Zend_Db_Adapter_Abstract */
protected $db;
/** @var string */
protected $varName;
public function __construct(Db $connection)
{
$this->varName = $connection->settings()->override_services_varname;
$this->db = $connection->getDbAdapter();
}
public function resolveFor(IcingaHost $host, IcingaService $service = null)
{
$parents = $host->listFlatResolvedImportNames();
$query = $this->db->select()->from(['hv' => 'icinga_host_var'], [
'host_name' => 'h.object_name',
'varvalue' => 'hv.varvalue',
])->join(
['h' => 'icinga_host'],
'h.id = hv.host_id',
[]
)->where('hv.varname = ?', $this->varName)->where('h.object_name IN (?)', $parents);
$overrides = [];
foreach ($this->db->fetchAll($query) as $row) {
if ($row->varvalue === null) {
continue;
}
foreach (Json::decode($row->varvalue) as $serviceName => $vars) {
$overrides[$serviceName][$row->host_name] = $vars;
}
}
if ($service) {
$name = $service->getObjectName();
if (isset($overrides[$name])) {
return $overrides[$name];
} else {
return [];
}
}
return $overrides;
}
}