mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Added add and remove parents for device from the V0 API (#11100)
* Added add and remove parents for device from the V0 API * Fixed some minors errors (white spaces and lack of them, an unnecesary else) * Added missing } * Reducing cognitive complexity * Further reduced cognitive complexity and added a check if parent device existed (it blew up when it did not) * Refactored the code and fixed bugs (that i created) when deviceIds or parentIds where nonexistent * Fixed } not on a new line issue * Empty space removed * Deleted lines added by mistake to composer.json * removed a comma that broke composer.json * added the missing element in composer.json * Added optional ids to del_parents_fom_host Updated del_parents_from_host() so it can accept parent(s) id(s) to delete, if none given it deletes all it's parents. * Fixing code issues on del_parents_from_host Refactored and solved small issues with del_parents_from_host * Refactored code del_parents_from_host Refactored del_parents_from_host() in order to comply with the projects standard * Added one space Added missing whitespace * refactoring for better readability
This commit is contained in:
@ -2259,6 +2259,52 @@ function add_service_for_host(\Illuminate\Http\Request $request)
|
||||
return api_error(500, 'Failed to add the service');
|
||||
}
|
||||
|
||||
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']);
|
||||
if (validateDeviceIds($parent_ids) && validateDeviceIds(array($device_id)) && (!in_array($device_id, $parent_ids))) {
|
||||
Device::find($device_id)->parents()->sync($parent_ids);
|
||||
return api_success_noresult(201, 'Device dependencies have been saved');
|
||||
}
|
||||
return api_error(400, "Check your parent and device IDs");
|
||||
}
|
||||
|
||||
function del_parents_from_host(\Illuminate\Http\Request $request)
|
||||
{
|
||||
$device_id = $request->route('id');
|
||||
$data = json_decode($request->getContent(), true);
|
||||
if (!validateDeviceIds(array($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']);
|
||||
//remove parents included in the request if they are valid device ids
|
||||
$result = validateDeviceIds($parents)?$device->parents()->detach($parents):false;
|
||||
}
|
||||
if (is_null($result)) {
|
||||
//$result doesn't exist so $data['parent_ids'] is empty
|
||||
$result = $device->parents()->detach(); //remove all parents
|
||||
}
|
||||
if ($result) {
|
||||
return api_success_noresult(201, 'All device dependencies have been removed');
|
||||
}
|
||||
return api_error(400, 'Device dependency cannot be deleted check device and parents ids');
|
||||
}
|
||||
|
||||
function validateDeviceIds($ids)
|
||||
{
|
||||
foreach ($ids as $id) {
|
||||
$invalidId = !is_numeric($id) || $id < 1 || is_null(Device::find($id));
|
||||
if ($invalidId) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display Librenms Instance Info
|
||||
*/
|
||||
|
@ -76,6 +76,8 @@ Route::group(['prefix' => 'v0', 'namespace' => '\App\Api\Controllers'], function
|
||||
Route::get('oxidized/config/search/{searchstring}', 'LegacyApiController@search_oxidized')->name('search_oxidized');
|
||||
Route::get('oxidized/config/{device_name}', 'LegacyApiController@get_oxidized_config')->name('get_oxidized_config');
|
||||
Route::post('devicegroups', 'LegacyApiController@add_device_group')->name('add_device_group');
|
||||
Route::post('devices/{id}/parents', 'LegacyApiController@add_parents_to_host')->name('add_parents_to_host');
|
||||
Route::delete('/devices/{id}/parents', 'LegacyApiController@del_parents_from_host')->name('del_parents_from_host');
|
||||
});
|
||||
|
||||
// restricted by access
|
||||
|
Reference in New Issue
Block a user