mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-31 01:34:12 +02:00
OverriddenVarsResolver: refine api
This commit is contained in:
parent
9889fa9342
commit
21a284abbc
@ -257,7 +257,7 @@ class DirectorDatafield extends DbObjectWithSettings
|
|||||||
|
|
||||||
if ($form instanceof IcingaServiceForm && $form->providesOverrides()) {
|
if ($form instanceof IcingaServiceForm && $form->providesOverrides()) {
|
||||||
$resolver = new OverriddenVarsResolver($form->getDb());
|
$resolver = new OverriddenVarsResolver($form->getDb());
|
||||||
$vars = $resolver->resolveFor($form->getHost(), $object);
|
$vars = $resolver->resolveFor($form->getHost(), $object->getObjectName());
|
||||||
foreach ($vars as $host => $values) {
|
foreach ($vars as $host => $values) {
|
||||||
if (\property_exists($values, $varName)) {
|
if (\property_exists($values, $varName)) {
|
||||||
$inherited = $values->$varName;
|
$inherited = $values->$varName;
|
||||||
|
@ -2,10 +2,9 @@
|
|||||||
|
|
||||||
namespace Icinga\Module\Director\Resolver;
|
namespace Icinga\Module\Director\Resolver;
|
||||||
|
|
||||||
|
use Icinga\Module\Director\Core\Json;
|
||||||
use Icinga\Module\Director\Db;
|
use Icinga\Module\Director\Db;
|
||||||
use Icinga\Module\Director\Objects\IcingaHost;
|
use Icinga\Module\Director\Objects\IcingaHost;
|
||||||
use Icinga\Module\Director\Objects\IcingaService;
|
|
||||||
use Icinga\Util\Json;
|
|
||||||
|
|
||||||
class OverriddenVarsResolver
|
class OverriddenVarsResolver
|
||||||
{
|
{
|
||||||
@ -13,25 +12,30 @@ class OverriddenVarsResolver
|
|||||||
protected $db;
|
protected $db;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $varName;
|
protected $overrideVarName;
|
||||||
|
|
||||||
public function __construct(Db $connection)
|
public function __construct(Db $connection)
|
||||||
{
|
{
|
||||||
$this->varName = $connection->settings()->override_services_varname;
|
$this->overrideVarName = $connection->settings()->get('override_services_varname');
|
||||||
$this->db = $connection->getDbAdapter();
|
$this->db = $connection->getDbAdapter();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resolveFor(IcingaHost $host, IcingaService $service = null)
|
public function fetchForHost(IcingaHost $host)
|
||||||
{
|
{
|
||||||
$parents = $host->listFlatResolvedImportNames();
|
$parents = $host->listFlatResolvedImportNames();
|
||||||
$query = $this->db->select()->from(['hv' => 'icinga_host_var'], [
|
$query = $this->db->select()
|
||||||
|
->from(['hv' => 'icinga_host_var'], [
|
||||||
'host_name' => 'h.object_name',
|
'host_name' => 'h.object_name',
|
||||||
'varvalue' => 'hv.varvalue',
|
'varvalue' => 'hv.varvalue',
|
||||||
])->join(
|
])
|
||||||
|
->join(
|
||||||
['h' => 'icinga_host'],
|
['h' => 'icinga_host'],
|
||||||
'h.id = hv.host_id',
|
'h.id = hv.host_id',
|
||||||
[]
|
[]
|
||||||
)->where('hv.varname = ?', $this->varName)->where('h.object_name IN (?)', $parents);
|
)
|
||||||
|
->where('hv.varname = ?', $this->overrideVarName)
|
||||||
|
->where('h.object_name IN (?)', $parents);
|
||||||
|
|
||||||
$overrides = [];
|
$overrides = [];
|
||||||
foreach ($this->db->fetchAll($query) as $row) {
|
foreach ($this->db->fetchAll($query) as $row) {
|
||||||
if ($row->varvalue === null) {
|
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;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user