2021-10-05 23:12:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Db\Branch;
|
|
|
|
|
|
|
|
use Icinga\Module\Director\Db;
|
|
|
|
use Icinga\Module\Director\Objects\IcingaHost;
|
|
|
|
use Icinga\Module\Director\Objects\IcingaServiceSet;
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
use RuntimeException;
|
|
|
|
use function is_int;
|
|
|
|
use function is_resource;
|
|
|
|
use function is_string;
|
|
|
|
|
|
|
|
class UuidLookup
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Db $connection
|
|
|
|
* @param Branch $branch
|
|
|
|
* @param string $objectType
|
|
|
|
* @param int|string $key
|
|
|
|
* @param IcingaHost|null $host
|
|
|
|
* @param IcingaServiceSet $set
|
|
|
|
*/
|
|
|
|
public static function findServiceUuid(
|
|
|
|
Db $connection,
|
|
|
|
Branch $branch,
|
|
|
|
$objectType,
|
|
|
|
$key = null,
|
|
|
|
IcingaHost $host = null,
|
|
|
|
IcingaServiceSet $set = null
|
|
|
|
) {
|
|
|
|
$db = $connection->getDbAdapter();
|
|
|
|
$query = $db->select()->from('icinga_service', 'uuid')->where('object_type = ?', $objectType);
|
2021-12-17 11:46:55 +01:00
|
|
|
$query = self::addKeyToQuery($connection, $query, $key);
|
2021-10-05 23:12:26 +02:00
|
|
|
if ($host) {
|
2021-12-23 10:25:59 +01:00
|
|
|
$query->where('host_id = ?', $host->get('id'));
|
2021-10-05 23:12:26 +02:00
|
|
|
}
|
|
|
|
if ($set) {
|
2021-12-23 10:25:59 +01:00
|
|
|
$query->where('service_set_id = ?', $set->get('id'));
|
2021-10-05 23:12:26 +02:00
|
|
|
}
|
|
|
|
$uuid = self::fetchOptionalUuid($connection, $query);
|
|
|
|
|
|
|
|
if ($uuid === null && $branch->isBranch()) {
|
|
|
|
// TODO: use different tables?
|
|
|
|
$query = $db->select()->from('branched_icinga_service', 'uuid')->where('object_type = ?', $objectType);
|
2021-12-17 11:46:55 +01:00
|
|
|
$query = self::addKeyToQuery($connection, $query, $key);
|
2021-10-05 23:12:26 +02:00
|
|
|
if ($host) {
|
|
|
|
// TODO: uuid?
|
|
|
|
$query->add('host = ?', $host->getObjectName());
|
|
|
|
}
|
|
|
|
if ($set) {
|
|
|
|
$query->add('service_set = ?', $set->getObjectName());
|
|
|
|
}
|
|
|
|
$uuid = self::fetchOptionalUuid($connection, $query);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function findUuidForKey($key, $table, Db $connection, Branch $branch)
|
|
|
|
{
|
|
|
|
$db = $connection->getDbAdapter();
|
2021-12-17 11:46:55 +01:00
|
|
|
$query = self::addKeyToQuery($connection, $db->select()->from($table, 'uuid'), $key);
|
2021-10-05 23:12:26 +02:00
|
|
|
$uuid = self::fetchOptionalUuid($connection, $query);
|
|
|
|
if ($uuid === null && $branch->isBranch()) {
|
|
|
|
$query = $db->select()->from("branched_$table", 'uuid')->where('object_name = ?', $key);
|
|
|
|
$uuid = self::fetchOptionalUuid($connection, $query);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $uuid;
|
|
|
|
}
|
|
|
|
|
2021-12-17 11:46:55 +01:00
|
|
|
protected static function addKeyToQuery(Db $connection, $query, $key)
|
2021-10-05 23:12:26 +02:00
|
|
|
{
|
|
|
|
if (is_int($key)) {
|
|
|
|
$query->where('id = ?', $key);
|
|
|
|
} elseif (is_string($key)) {
|
|
|
|
$query->where('object_name = ?', $key);
|
|
|
|
} else {
|
2021-12-17 11:46:55 +01:00
|
|
|
foreach ($key as $k => $v) {
|
|
|
|
$query->where($connection->getDbAdapter()->quoteIdentifier($k) . ' = ?', $v);
|
|
|
|
}
|
2021-10-05 23:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function fetchOptionalUuid(Db $connection, $query)
|
|
|
|
{
|
|
|
|
$result = $connection->getDbAdapter()->fetchOne($query);
|
|
|
|
if (is_resource($result)) {
|
|
|
|
$result = stream_get_contents($result);
|
|
|
|
}
|
|
|
|
if (is_string($result)) {
|
|
|
|
return Uuid::fromBytes($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|