diff --git a/application/clicommands/FetchCommand.php b/application/clicommands/FetchCommand.php index ae9b5a3..cfa5aee 100644 --- a/application/clicommands/FetchCommand.php +++ b/application/clicommands/FetchCommand.php @@ -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 @@ -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 { @@ -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 { diff --git a/library/Vsphere/ProvidedHook/Director/ImportSource.php b/library/Vsphere/ProvidedHook/Director/ImportSource.php index 3431b30..1c45f79 100644 --- a/library/Vsphere/ProvidedHook/Director/ImportSource.php +++ b/library/Vsphere/ProvidedHook/Director/ImportSource.php @@ -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 @@ -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() diff --git a/library/Vsphere/Util.php b/library/Vsphere/Util.php new file mode 100644 index 0000000..82ef6f4 --- /dev/null +++ b/library/Vsphere/Util.php @@ -0,0 +1,48 @@ + $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); + } + } +}