From 483c40eddd07b19ce693879fc1a6298135e09de4 Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Thu, 26 Jan 2017 22:38:43 +0000 Subject: [PATCH] api: Added parent_id to VMs that have a host #3523 (#5621) --- html/includes/api_functions.inc.php | 9 ++++++++- html/includes/device-header.inc.php | 2 +- includes/common.php | 12 ++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index e20b2fe61e..45edee20ae 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -178,6 +178,10 @@ function get_device() echo _json_encode($output); $app->stop(); } else { + $host_id = get_vm_parent_id($device); + if (is_numeric($host_id)) { + $device = array_merge($device, array('parent_id' => $host_id)); + } $output = array( 'status' => 'ok', 'devices' => array($device), @@ -186,7 +190,6 @@ function get_device() } } - function list_devices() { // This will return a list of devices @@ -232,7 +235,11 @@ function list_devices() } $devices = array(); foreach (dbFetchRows("SELECT * FROM `devices` $join WHERE $sql ORDER by $order", $param) as $device) { + $host_id = get_vm_parent_id($device); $device['ip'] = inet6_ntop($device['ip']); + if (is_numeric($host_id)) { + $device['parent_id'] = $host_id; + } $devices[] = $device; } diff --git a/html/includes/device-header.inc.php b/html/includes/device-header.inc.php index 93fac6cd3e..82b8d2dec8 100644 --- a/html/includes/device-header.inc.php +++ b/html/includes/device-header.inc.php @@ -17,7 +17,7 @@ if ($device['disabled'] == '1') { $class = 'alert-info'; } -$host_id = dbFetchCell("SELECT `device_id` FROM `vminfo` WHERE `vmwVmDisplayName` = ? OR `vmwVmDisplayName` = ?", array($device['hostname'],$device['hostname'].'.'.$config['mydomain'])); +$host_id = get_vm_parent_id($device); echo ' diff --git a/includes/common.php b/includes/common.php index 6475a78b16..536e2b5468 100644 --- a/includes/common.php +++ b/includes/common.php @@ -1615,6 +1615,7 @@ function set_numeric($value, $default = 0) } return $value; } + function check_git_exists() { if (`which git`) { @@ -1623,3 +1624,14 @@ function check_git_exists() return false; } } + +function get_vm_parent_id($device) +{ + global $config; + + if (empty($device['hostname'])) { + return false; + } + + return dbFetchCell("SELECT `device_id` FROM `vminfo` WHERE `vmwVmDisplayName` = ? OR `vmwVmDisplayName` = ?", array($device['hostname'],$device['hostname'].'.'.$config['mydomain'])); +}