From 0ede8a2c1d4239cbdf7e6b861ae2530231c58f83 Mon Sep 17 00:00:00 2001 From: SourceDoctor Date: Thu, 19 Nov 2020 03:23:07 +0100 Subject: [PATCH] API - Allow Hostname on add/remove Device Dependencies (#12319) * API - Allow Hostname on add/remove Device Dependencies * style fix * change device placeholder name --- doc/API/Devices.md | 8 ++++---- includes/html/api_functions.inc.php | 26 +++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/doc/API/Devices.md b/doc/API/Devices.md index 291d5928e9..131406df93 100644 --- a/doc/API/Devices.md +++ b/doc/API/Devices.md @@ -1367,11 +1367,11 @@ Output: Add one or more parents to a host. -Route: `/api/v0/devices/:device_id/parents` +Route: `/api/v0/devices/:device/parents` Input (JSON): -- parent_ids: one or more parent ids +- parent_ids: one or more parent ids or hostnames Example: ```curl @@ -1390,11 +1390,11 @@ Output: Deletes some or all the parents from a host. -Route: `/api/v0/devices/:device_id/parents` +Route: `/api/v0/devices/:device/parents` Input (JSON): -- parent_ids: One or more parent ids, if not specified deletes all parents from host. +- parent_ids: One or more parent ids or hostnames, if not specified deletes all parents from host. Example: ```curl diff --git a/includes/html/api_functions.inc.php b/includes/html/api_functions.inc.php index 2757f951fc..c4252a52d9 100644 --- a/includes/html/api_functions.inc.php +++ b/includes/html/api_functions.inc.php @@ -2344,7 +2344,18 @@ function add_parents_to_host(Illuminate\Http\Request $request) { $data = json_decode($request->getContent(), true); $device_id = $request->route('id'); - $parent_ids = explode(',', $data['parent_ids']); + $device_id = ctype_digit($device_id) ? $device_id : getidbyname($device_id); + + $parent_ids = []; + foreach (explode(',', $data['parent_ids']) as $hostname) { + $hostname = trim($hostname); + $parent_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); + if (empty($parent_id)) { + return api_error(400, 'Parent device IDs/Hostname does not exist: ' . $hostname); + } + $parent_ids[] = $parent_id; + } + if (validateDeviceIds($parent_ids) && validateDeviceIds([$device_id]) && (! in_array($device_id, $parent_ids))) { Device::find($device_id)->parents()->sync($parent_ids); @@ -2357,15 +2368,24 @@ function add_parents_to_host(Illuminate\Http\Request $request) function del_parents_from_host(Illuminate\Http\Request $request) { $device_id = $request->route('id'); + $device_id = ctype_digit($device_id) ? $device_id : getidbyname($device_id); $data = json_decode($request->getContent(), true); if (! validateDeviceIds([$device_id])) { return api_error(400, 'Check your device ID!'); } $device = Device::find($device_id); if (! empty($data['parent_ids'])) { - $parents = explode(',', $data['parent_ids']); + foreach (explode(',', $data['parent_ids']) as $hostname) { + $hostname = trim($hostname); + $parent_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); + if (empty($parent_id)) { + return api_error(400, 'Parent device IDs/Hostname does not exist: ' . $hostname); + } + $parent_ids[] = $parent_id; + } + //remove parents included in the request if they are valid device ids - $result = validateDeviceIds($parents) ? $device->parents()->detach($parents) : false; + $result = validateDeviceIds($parent_ids) ? $device->parents()->detach($parent_ids) : false; } if (is_null($result)) { //$result doesn't exist so $data['parent_ids'] is empty