OverriddenVarsResolver: refine api

This commit is contained in:
Thomas Gelf 2021-01-19 01:47:31 +01:00
parent 9889fa9342
commit 21a284abbc
2 changed files with 38 additions and 23 deletions

View File

@ -257,7 +257,7 @@ class DirectorDatafield extends DbObjectWithSettings
if ($form instanceof IcingaServiceForm && $form->providesOverrides()) {
$resolver = new OverriddenVarsResolver($form->getDb());
$vars = $resolver->resolveFor($form->getHost(), $object);
$vars = $resolver->resolveFor($form->getHost(), $object->getObjectName());
foreach ($vars as $host => $values) {
if (\property_exists($values, $varName)) {
$inherited = $values->$varName;

View File

@ -2,10 +2,9 @@
namespace Icinga\Module\Director\Resolver;
use Icinga\Module\Director\Core\Json;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Util\Json;
class OverriddenVarsResolver
{
@ -13,25 +12,30 @@ class OverriddenVarsResolver
protected $db;
/** @var string */
protected $varName;
protected $overrideVarName;
public function __construct(Db $connection)
{
$this->varName = $connection->settings()->override_services_varname;
$this->overrideVarName = $connection->settings()->get('override_services_varname');
$this->db = $connection->getDbAdapter();
}
public function resolveFor(IcingaHost $host, IcingaService $service = null)
public function fetchForHost(IcingaHost $host)
{
$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);
$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->overrideVarName)
->where('h.object_name IN (?)', $parents);
$overrides = [];
foreach ($this->db->fetchAll($query) as $row) {
if ($row->varvalue === null) {
@ -42,15 +46,26 @@ class OverriddenVarsResolver
}
}
if ($service) {
$name = $service->getObjectName();
if (isset($overrides[$name])) {
return $overrides[$name];
} else {
return [];
}
}
return $overrides;
}
public function fetchForServiceName(IcingaHost $host, $serviceName)
{
$overrides = $this->fetchForHost($host);
if (isset($overrides[$serviceName])) {
return $overrides[$serviceName];
}
return [];
}
public function fetchVarForServiceName(IcingaHost $host, $serviceName, $varName)
{
$overrides = $this->fetchForHost($host);
if (isset($overrides[$serviceName][$varName])) {
return $overrides[$serviceName][$varName];
}
return null;
}
}