mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
API - Allow Hostname on add/remove Device Dependencies (#12319)
* API - Allow Hostname on add/remove Device Dependencies * style fix * change device placeholder name
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user