Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customvar hook #1047

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions library/Icingadb/Hook/CustomVarEnricherHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/** Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Hook;

use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Module\Icingadb\Hook\Common\HookUtils;
use ipl\Orm\Model;
use Throwable;

abstract class CustomVarEnricherHook
{
use HookUtils;

/**
* Return enriched vars in the following format
* [label => enriched custom var]
*
* @param array $vars
*
* @return array
*/
abstract public function enrichCustomVars(array &$vars, Model $object): array;

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() return type has no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() return type has no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() return type has no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() return type has no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() return type has no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() return type has no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 25 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::enrichCustomVars() return type has no value type specified in iterable type array.

public static function prepareEnrichedCustomVars(array $vars, Model $object): array

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() return type has no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() return type has no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() return type has no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() return type has no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() return type has no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() return type has no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/Hook/CustomVarEnricherHook.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method Icinga\Module\Icingadb\Hook\CustomVarEnricherHook::prepareEnrichedCustomVars() return type has no value type specified in iterable type array.
{
$enrichedVars = [];
$groups = [];

foreach (Hook::all('Icingadb/CustomVarEnricher') as $hook) {
/** @var self $hook */
try {
list($hookVars, $hookGroups) = $hook->enrichCustomVars($vars, $object);
$enrichedVars[] = $hookVars;
$groups[] = $hookGroups;
} catch (Throwable $e) {
Logger::error('Failed to load hook %s:', get_class($hook), $e);
}
}

$vars = array_merge($vars, ...$enrichedVars);
$groups = array_merge([], ...$groups);

return [$vars, $groups];
}
}
162 changes: 162 additions & 0 deletions library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

namespace Icinga\Module\Icingadb\ProvidedHook\Icingadb;

use Icinga\Application\Config;
use Icinga\Module\Director\Data\Db\DbConnection;
use Icinga\Module\Director\DataType\DataTypeDatalist;
use Icinga\Module\Director\DataType\DataTypeDictionary;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\DirectorDatafield;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Web\Form\IcingaObjectFieldLoader;
use Icinga\Module\Icingadb\Hook\CustomVarEnricherHook;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
use ipl\Orm\Model;

class CustomVarEnricher extends CustomVarEnricherHook
{
protected $fieldConfig;

Check failure on line 21 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$fieldConfig has no type specified.

Check failure on line 21 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$fieldConfig has no type specified.

Check failure on line 21 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$fieldConfig has no type specified.

Check failure on line 21 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$fieldConfig has no type specified.

Check failure on line 21 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$fieldConfig has no type specified.

Check failure on line 21 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$fieldConfig has no type specified.

Check failure on line 21 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$fieldConfig has no type specified.

protected $datalistMaps = [];

Check failure on line 23 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$datalistMaps has no type specified.

Check failure on line 23 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$datalistMaps has no type specified.

Check failure on line 23 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$datalistMaps has no type specified.

Check failure on line 23 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$datalistMaps has no type specified.

Check failure on line 23 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$datalistMaps has no type specified.

Check failure on line 23 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$datalistMaps has no type specified.

Check failure on line 23 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$datalistMaps has no type specified.

protected $groups = [];

Check failure on line 25 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$groups has no type specified.

Check failure on line 25 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$groups has no type specified.

Check failure on line 25 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$groups has no type specified.

Check failure on line 25 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$groups has no type specified.

Check failure on line 25 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$groups has no type specified.

Check failure on line 25 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$groups has no type specified.

Check failure on line 25 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Property Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::$groups has no type specified.

public function enrichCustomVars(array &$vars, Model $object): array

Check failure on line 27 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Method Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Method Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Method Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.

Check failure on line 27 in library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method Icinga\Module\Icingadb\ProvidedHook\Icingadb\CustomVarEnricher::enrichCustomVars() has parameter $vars with no value type specified in iterable type array.
{
$directorObject = null;
$connection = Db::fromResourceName(Config::module('director')->get('db', 'resource'));
if ($object instanceof Host) {
$directorObject = IcingaHost::load($object->name, $connection);
} elseif ($object instanceof Service) {
$directorHost = IcingaHost::load($object->host->name, $connection);
$directorObject = IcingaService::load(
['object_name' => $object->name, 'host_id' => $directorHost->get('id')],
$connection
);
}

$newVars = [];
$this->fieldConfig = (new IcingaObjectFieldLoader($directorObject))->getFields();

$this->buildDataListMap($connection);

if ($directorObject) {
$varsToReplace = json_decode(json_encode($directorObject->getVars()), true)
+ json_decode(json_encode($directorObject->getInheritedVars()), true);

foreach ($varsToReplace as $varName => $customVar) {
if (isset($vars[$varName])) {
$newVars[] = $this->resolveCustomVarMapping($varName, $customVar, $connection);

unset($vars[$varName]);
}
}

$newVars = array_merge([], ...$newVars);
}

return [$newVars, $this->groups];
}

/**
* Returns the resolved mapping to custom variables in Director
*
* @param string $name
* @param $val
* @param DbConnection $conn
* @param bool $grouping Whether to enable grouping of custom variables into sections
*
* @return array
*/
protected function resolveCustomVarMapping(?string $name, $val, DbConnection $conn, bool $grouping = true): array
{
if (isset($this->fieldConfig[$name])) {
/** @var DirectorDatafield $field */
$field = $this->fieldConfig[$name];
$dataType = $field->get('datatype');

if ($dataType === get_class(new DataTypeDictionary())) {
$label = $field->get('caption');
$newVarValue = [];
foreach ($val as $nestedVarName => $nestedVarValue) {
$newVarValue[$nestedVarName] = $this->buildDictionaryMap(
$nestedVarValue,
$conn
);
}

return [$label => $newVarValue];
} elseif ($dataType === get_class(new DataTypeDatalist())) {
if (isset($this->datalistMaps[$name])) {
$val = $this->datalistMaps[$name][$val];
}

$name = $field->get('caption');
} else {
$name = $field->get('caption');
}

if ($grouping && $field->get('category_id') !== null) {
if (! isset($this->groups[$field->getCategoryName()])) {
$this->groups[$field->getCategoryName()] = [$name => $val];
} else {
$this->groups[$field->getCategoryName()][$name] = $val;
}

return [];
}
} elseif (is_array($val)) {
$newValue = [];
foreach ($val as $childName => $childValue) {
$newValue[] = $this->resolveCustomVarMapping($childName, $childValue, $conn, false);
}

$val = array_merge([], ...$newValue);
}

return [$name => $val];
}

private function buildDictionaryMap($val, DbConnection $connection): array
{
foreach ($val as $childName => $childValue) {
$newValue[] = $this->resolveCustomVarMapping($childName, $childValue, $connection, false);
}

return array_merge([], ...$newValue);
}

private function buildDataListMap(DbConnection $db)
{
$fieldsWithDataLists = [];
foreach ($this->fieldConfig as $field) {
if ($field->get('datatype') === 'Icinga\Module\Director\DataType\DataTypeDatalist') {
$fieldsWithDataLists[$field->get('id')] = $field;
}
}

if (! empty($fieldsWithDataLists)) {
$dataListEntries = $db->select()->from(
['dds' => 'director_datafield_setting'],
[
'dds.datafield_id',
'dde.entry_name',
'dde.entry_value'
]
)->join(
['dde' => 'director_datalist_entry'],
'CAST(dds.setting_value AS integer) = dde.list_id',
[]
)->where('dds.datafield_id', array_keys($fieldsWithDataLists))
->where('dds.setting_name', 'datalist_id');

foreach ($dataListEntries as $dataListEntry) {
$field = $fieldsWithDataLists[$dataListEntry->datafield_id];
$this->datalistMaps[$field->get('varname')][$dataListEntry->entry_name] = $dataListEntry->entry_value;
}
}
}
}
24 changes: 24 additions & 0 deletions library/Icingadb/ProvidedHook/Icingadb/CustomVarItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/** Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Common;

use ipl\Html\BaseHtmlElement;
use ipl\Html\ValidHtml;

class CustomVarItem extends BaseHtmlElement
{
// TODO: Class for an item in custom HTML Section
protected $item;

public function __construct(ValidHtml $item)
{
$this->item = $item;
}

protected function assemble()
{
$this->addHtml($this->item);
}
}
23 changes: 23 additions & 0 deletions library/Icingadb/ProvidedHook/Icingadb/CustomVarSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/** Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Common;

use ipl\Html\BaseHtmlElement;

class CustomVarSection extends BaseHtmlElement
{
// TODO: Class for custom HTML section
protected $items;

public function addItem(CustomVarItem $item)
{
$this->items[] = $item;
}

public function assemble()
{
$this->addHtml($this->items);
}
}
36 changes: 25 additions & 11 deletions library/Icingadb/Widget/Detail/CustomVarTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Icinga\Module\Icingadb\Widget\Detail;

use Icinga\Module\Icingadb\Hook\CustomVarEnricherHook;
use Icinga\Module\Icingadb\Hook\CustomVarRendererHook;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\Html\ValidHtml;
use ipl\Orm\Model;
use ipl\Web\Widget\EmptyState;
use ipl\Web\Widget\Icon;
Expand Down Expand Up @@ -101,11 +103,11 @@ protected function addRow($name, $value)
protected function renderVar($name, $value)
{
if ($this->object !== null && $this->level === 0) {
list($name, $value, $group) = call_user_func($this->hookApplier, $name, $value);
if ($group !== null) {
$this->groups[$group][] = [$name, $value];
return;
}
// list($name, $value, $group) = call_user_func($this->hookApplier, $name, $value);
// if ($group !== null) {
// $this->groups[$group][] = [$name, $value];
// return;
// }
}

$isArray = is_array($value);
Expand All @@ -116,6 +118,8 @@ protected function renderVar($name, $value)
case $isArray:
$this->renderObject($name, $value);
break;
case $value instanceof ValidHtml:
// Todo: Needs new implementation
default:
$this->renderScalar($name, $value);
}
Expand Down Expand Up @@ -213,9 +217,9 @@ protected function renderGroup(string $name, $entries)

protected function assemble()
{
if ($this->object !== null) {
$this->hookApplier = CustomVarRendererHook::prepareForObject($this->object);
}
// if ($this->object !== null) {
// $this->hookApplier = CustomVarRendererHook::prepareForObject($this->object);
// }

if ($this->headerTitle !== null) {
$this->getAttributes()
Expand Down Expand Up @@ -248,20 +252,30 @@ protected function assemble()
ksort($this->data);
}

foreach ($this->data as $name => $value) {
$groups = [];
if ($this->object !== null) {
list($enrichedCustomVars, $groups) = CustomVarEnricherHook::prepareEnrichedCustomVars(
(array) $this->data,
$this->object
);
} else {
$enrichedCustomVars = $this->data;
}

foreach ($enrichedCustomVars as $name => $value) {
$this->renderVar($name, $value);
}

$this->addHtml($this->body);

// Hooks can return objects as replacement for keys, hence a generator is needed for group entries
$genGenerator = function ($entries) {
foreach ($entries as list($key, $value)) {
foreach ($entries as $key => $value) {
yield $key => $value;
}
};

foreach ($this->groups as $group => $entries) {
foreach ($groups as $group => $entries) {
$this->renderGroup($group, $genGenerator($entries));
}
}
Expand Down
1 change: 1 addition & 0 deletions run.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

$this->provideHook('ApplicationState');
$this->provideHook('X509/Sni');
$this->provideHook('Icingadb/CustomVarEnricher');
$this->provideHook('health', 'IcingaHealth');
$this->provideHook('health', 'RedisHealth');
$this->provideHook('Reporting/Report', 'Reporting/HostSlaReport');
Expand Down
Loading
Loading