Skip to content

Commit

Permalink
Util, Import, Fetch: return structured data
Browse files Browse the repository at this point in the history
fixes #5
  • Loading branch information
Thomas-Gelf committed Jul 10, 2017
1 parent 1671654 commit 7c91eaa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
5 changes: 5 additions & 0 deletions application/clicommands/FetchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Icinga\Module\Vsphere\ManagedObject\FullTraversal;
use Icinga\Module\Vsphere\ManagedObject\HostSystem;
use Icinga\Module\Vsphere\ManagedObject\VirtualMachine;
use Icinga\Module\Vsphere\Util;

/**
* Fetch information from a vCenter or ESXi host
Expand Down Expand Up @@ -60,6 +61,8 @@ public function virtualmachinesAction()
Benchmark::measure('Mapped properties');
$api->logout();
Benchmark::measure('Logged out');
$objects = Util::createNestedObjects($objects);

if ($this->params->get('json')) {
echo json_encode($objects);
} else {
Expand Down Expand Up @@ -109,6 +112,8 @@ public function hostsystemsAction()
}
$api->logout();
Benchmark::measure('Logged out');
$objects = Util::createNestedObjects($objects);

if ($this->params->get('json')) {
echo json_encode($objects);
} else {
Expand Down
3 changes: 2 additions & 1 deletion library/Vsphere/ProvidedHook/Director/ImportSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Icinga\Module\Director\Hook\ImportSourceHook;
use Icinga\Module\Director\Web\Form\QuickForm;
use Icinga\Module\Vsphere\Api;
use Icinga\Module\Vsphere\Util;

/**
* Class ImportSource
Expand All @@ -28,7 +29,7 @@ public function fetchData()
$objects = $this->callOnManagedObject('fetchWithDefaults', $api);
$api->idLookup()->enrichObjects($objects);
$api->logout();
return $objects;
return Util::createNestedObjects($objects);
}

public function listColumns()
Expand Down
48 changes: 48 additions & 0 deletions library/Vsphere/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Icinga\Module\Vsphere;

use Icinga\Exception\InvalidPropertyException;
use stdClass;

class Util
{
public static function createNestedObjects($objects)
{
foreach ($objects as $key => $object) {
$objects[$key] = static::createNestedObject($object);
}

return $objects;
}

protected static function createNestedObject($object)
{
$res = new stdClass();
foreach ((array) $object as $key => $value) {
$keys = explode('.', $key);
static::setDeepValue($res, $keys, $value);
}

return $res;
}

protected static function setDeepValue($object, $keys, $value)
{
$key = array_shift($keys);
if (empty($keys)) {
$object->$key = $value;
} else {
if (property_exists($object, $key)) {
if (! is_object($object->$key)) {
throw new InvalidPropertyException(
'A key can be either object or scalar: %s'
);
}
} else {
$object->$key = new stdClass();
}
static::setDeepValue($object->$key, $keys, $value);
}
}
}

0 comments on commit 7c91eaa

Please sign in to comment.