-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ImportSource: ships object path array
fixes #255
- Loading branch information
1 parent
c3e06bb
commit 4ec13a9
Showing
5 changed files
with
99 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters