summaryrefslogtreecommitdiffstats
path: root/library/Director/Resolver/OverriddenVarsResolver.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
commitf66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch)
treefbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/Resolver/OverriddenVarsResolver.php
parentInitial commit. (diff)
downloadicingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz
icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/Resolver/OverriddenVarsResolver.php')
-rw-r--r--library/Director/Resolver/OverriddenVarsResolver.php74
1 files changed, 74 insertions, 0 deletions
diff --git a/library/Director/Resolver/OverriddenVarsResolver.php b/library/Director/Resolver/OverriddenVarsResolver.php
new file mode 100644
index 0000000..4541244
--- /dev/null
+++ b/library/Director/Resolver/OverriddenVarsResolver.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace Icinga\Module\Director\Resolver;
+
+use Icinga\Module\Director\Core\Json;
+use Icinga\Module\Director\Db;
+use Icinga\Module\Director\Objects\IcingaHost;
+
+class OverriddenVarsResolver
+{
+ /** @var \Zend_Db_Adapter_Abstract */
+ protected $db;
+
+ /** @var string */
+ protected $overrideVarName;
+
+ public function __construct(Db $connection)
+ {
+ $this->overrideVarName = $connection->settings()->get('override_services_varname');
+ $this->db = $connection->getDbAdapter();
+ }
+
+ public function fetchForHost(IcingaHost $host)
+ {
+ $overrides = [];
+ $parents = $host->listFlatResolvedImportNames();
+ if (empty($parents)) {
+ return $overrides;
+ }
+ $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);
+
+ 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;
+ }
+ }
+
+ 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;
+ }
+}