Skip to content

Commit

Permalink
ImportSource: ships object path array
Browse files Browse the repository at this point in the history
fixes #255
  • Loading branch information
Thomas-Gelf committed Apr 21, 2023
1 parent c3e06bb commit 4ec13a9
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 21 deletions.
5 changes: 5 additions & 0 deletions doc/84-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ v1.8.0 (unreleased)

* FIX: vCenter filter is now preserved across main table tabs (#516)

### Integrations
* FEATURE: Director Import Source now ships an object path array (#255)

![Path Export](screenshot/84_changelog/v1.8.0/84-18-03_path_export.png)

### REST API
* Hosts, VirtualMachines and DataStores can now be exported via REST API (#511)

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions library/Vspheredb/Db/BulkPathLookup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Icinga\Module\Vspheredb\Db;

use Icinga\Module\Vspheredb\Db;
use Ramsey\Uuid\Uuid;
use RuntimeException;

class BulkPathLookup
{
/** @var Db */
protected $db;

protected $nodes;

/** @var ?array */
protected $vCenterFilterUuids;

public function __construct(Db $db, ?array $vCenterUuids = null)
{
$this->db = $db;
$this->vCenterFilterUuids = $vCenterUuids;
}

public function getParents(?string $objectParent): array
{
if ($this->nodes === null) {
$this->nodes = $this->fetchAllParents();
}
$path = [];
$parentUuid = $objectParent;
while ($parentUuid !== null) {
if (! isset($this->nodes[$parentUuid])) {
throw new RuntimeException('Parent lookup failed: ' . Uuid::fromBytes($parentUuid)->toString());
}
$node = $this->nodes[$parentUuid];
$path[$parentUuid] = $node->object_name;
$parentUuid = $node->parent_uuid;
}

return array_reverse($path, true);
}

protected function fetchAllParents(): array
{
$db = $this->db->getDbAdapter();
$query = $db->select()->from(['p' => 'object'], [
'uuid' => 'p.uuid',
'parent_uuid' => 'p.parent_uuid',
'object_name' => 'p.object_name'
])->join(['o' => 'object'], 'o.parent_uuid = p.uuid', []);
QueryHelper::applyOptionalVCenterFilter($db, $query, 'o.vcenter_uuid', $this->vCenterFilterUuids);
$result = [];
foreach ($db->fetchAll($query) as $row) {
$result[$row->uuid] = $row;
}
return $result;
}
}
25 changes: 25 additions & 0 deletions library/Vspheredb/Db/QueryHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Icinga\Module\Vspheredb\Db;

use Zend_Db_Adapter_Abstract as ZfDb;

class QueryHelper
{
public static function applyOptionalVCenterFilter(ZfDb $db, $query, string $column, ?array $vCenterFilterUuids)
{
if ($vCenterFilterUuids === null) {
return;
}
if (empty($vCenterFilterUuids)) {
$query->where('1 = 0');
return;
}

if (count($vCenterFilterUuids) === 1) {
$query->where("$column = ?", DbUtil::quoteBinaryCompat(array_shift($vCenterFilterUuids), $db));
} else {
$query->where("$column IN (?)", DbUtil::quoteBinaryCompat($vCenterFilterUuids, $db));
}
}
}
31 changes: 10 additions & 21 deletions library/Vspheredb/ProvidedHook/Director/ImportSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use Icinga\Module\Director\Hook\ImportSourceHook;
use Icinga\Module\Director\Web\Form\QuickForm;
use Icinga\Module\Vspheredb\Db;
use Icinga\Module\Vspheredb\Db\BulkPathLookup;
use Icinga\Module\Vspheredb\Db\DbUtil;
use Icinga\Module\Vspheredb\Db\QueryHelper;
use Icinga\Module\Vspheredb\DbObject\VCenter;
use Icinga\Module\Vspheredb\Web\Table\TableWithParentFilter;
use Icinga\Module\Vspheredb\Web\Table\TableWithVCenterFilter;
use Ramsey\Uuid\Uuid;
use Zend_Db_Adapter_Abstract as ZfDb;
use function array_keys;
use function in_array;

/**
* Class ImportSource
Expand All @@ -26,6 +27,7 @@ class ImportSource extends ImportSourceHook implements TableWithVCenterFilter, T
protected $hostColumns = [
'object_name' => 'o.object_name',
'uuid' => 'o.uuid',
'parent_uuid' => 'o.parent_uuid',
'vcenter_name' => 'vc.name',
'sysinfo_vendor' => 'h.sysinfo_vendor',
'sysinfo_model' => 'h.sysinfo_model',
Expand All @@ -43,6 +45,7 @@ class ImportSource extends ImportSourceHook implements TableWithVCenterFilter, T
'object_name' => 'o.object_name',
'moref' => 'o.moref',
'uuid' => 'o.uuid',
'parent_uuid' => 'o.parent_uuid',
'vcenter_name' => 'vc.name',
'guest_ip_address' => 'vm.guest_ip_address',
'hardware_numcpu' => 'vm.hardware_numcpu',
Expand All @@ -62,6 +65,7 @@ class ImportSource extends ImportSourceHook implements TableWithVCenterFilter, T
'object_name' => 'o.object_name',
'object_type' => 'o.object_type',
'uuid' => 'o.uuid',
'parent_uuid' => 'o.parent_uuid',
'vcenter_name' => 'vc.name',
'effective_cpu_mhz' => 'cr.effective_cpu_mhz',
'effective_memory_size_mb' => 'cr.effective_memory_size_mb',
Expand All @@ -77,6 +81,7 @@ class ImportSource extends ImportSourceHook implements TableWithVCenterFilter, T
protected $datastoreColumns = [
'object_name' => 'o.object_name',
'uuid' => 'o.uuid',
'parent_uuid' => 'o.parent_uuid',
'vcenter_name' => 'vc.name',
'maintenance_mode' => 'ds.maintenance_mode',
'capacity' => 'ds.capacity',
Expand Down Expand Up @@ -159,6 +164,7 @@ public function fetchData(): array
{
$connection = Db::newConfiguredInstance();
$db = $connection->getDbAdapter();
$pathLookup = new BulkPathLookup($connection);
$objectType = $this->getSetting('object_type');
switch ($objectType) {
case 'host_system':
Expand All @@ -176,7 +182,7 @@ public function fetchData(): array
default:
return [];
}
$this->applyOptionalVCenterFilter($db, $query);
QueryHelper::applyOptionalVCenterFilter($db, $query, 'vc.instance_uuid', $this->vCenterFilterUuids);
$this->applyOptionalParentFilter($query);
$result = $db->fetchAll(
$this->eventuallyFilterVCenter($this->joinVCenter($query))
Expand All @@ -187,6 +193,8 @@ public function fetchData(): array
}

foreach ($result as $row) {
$row->path = array_values($pathLookup->getParents($row->parent_uuid));
unset($row->parent_uuid);
static::convertDbRowToJsonData($row);
}

Expand Down Expand Up @@ -338,23 +346,4 @@ protected function applyOptionalParentFilter($query)

$query->where('o.parent_uuid IN (?)', $this->parentFilterUuids);
}

protected function applyOptionalVCenterFilter(ZfDb $db, $query)
{
$uuids = $this->vCenterFilterUuids;
if ($uuids === null) {
return;
}
if (empty($uuids)) {
$query->where('1 = 0');
return;
}

$column = 'vc.instance_uuid';
if (count($uuids) === 1) {
$query->where("$column = ?", DbUtil::quoteBinaryCompat(array_shift($uuids), $db));
} else {
$query->where("$column IN (?)", DbUtil::quoteBinaryCompat($uuids, $db));
}
}
}

0 comments on commit 4ec13a9

Please sign in to comment.