diff --git a/doc/API/DeviceGroups.md b/doc/API/DeviceGroups.md index 95a6593784..99fca0f92d 100644 --- a/doc/API/DeviceGroups.md +++ b/doc/API/DeviceGroups.md @@ -206,3 +206,66 @@ Output: } ``` +### Add devices to group + +Add devices to a device group. + +Route: `/api/v0/devicesgroups/:name/devices` + +- name Is the name of the device group which can be obtained using + [`get_devicegroups`](#function-get_devicegroups). Please ensure that + the name is urlencoded if it needs to be (i.e Linux Servers would + need to be urlencoded. + +Input (JSON): + +- `devices`: *required* - A list of devices to be added to the group. + +Example: + +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' \ + -X POST https://librenms.org/api/v0/devicegroups/devices \ + --data-raw '{"devices":[261,271]}' +``` + +Output: + +```json +{ + "status": "ok", + "message": "Devices added" +} +``` + +### Remove devices from group + +Removes devices from a device group. + +Route: `/api/v0/devicesgroups/:name/devices` + +- name Is the name of the device group which can be obtained using + [`get_devicegroups`](#function-get_devicegroups). Please ensure that + the name is urlencoded if it needs to be (i.e Linux Servers would + need to be urlencoded. + +Input (JSON): + +- `devices`: *required* - A list of devices to be removed from the group. + +Example: + +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' \ + -X DELETE https://librenms.org/api/v0/devicegroups/devices \ + --data-raw '{"devices":[261,271]}' +``` + +Output: + +```json +{ + "status": "ok", + "message": "Devices removed" +} +``` diff --git a/includes/html/api_functions.inc.php b/includes/html/api_functions.inc.php index a792faebdd..b3552d54e3 100644 --- a/includes/html/api_functions.inc.php +++ b/includes/html/api_functions.inc.php @@ -2233,6 +2233,80 @@ function add_device_group(Illuminate\Http\Request $request) return api_success($deviceGroup->id, 'id', 'Device group ' . $deviceGroup->name . ' created', 201); } +function update_device_group_add_devices(Illuminate\Http\Request $request) +{ + $data = json_decode($request->getContent(), true); + if (json_last_error() || ! is_array($data)) { + return api_error(400, "We couldn't parse the provided json. " . json_last_error_msg()); + } + + $name = $request->route('name'); + if (! $name) { + return api_error(400, 'No device group name provided'); + } + + $deviceGroup = ctype_digit($name) ? DeviceGroup::find($name) : DeviceGroup::where('name', $name)->first(); + + if (! $deviceGroup) { + return api_error(404, "Device group $name not found"); + } + + if ('static' != $deviceGroup->type) { + return api_error(422, 'Only static device group can have devices added'); + } + + $rules = [ + 'devices' => 'array', + 'devices.*' => 'integer', + ]; + + $v = Validator::make($data, $rules); + if ($v->fails()) { + return api_error(422, $v->messages()); + } + + $deviceGroup->devices()->syncWithoutDetaching($data['devices']); + + return api_success_noresult(200, 'Devices added'); +} + +function update_device_group_remove_devices(Illuminate\Http\Request $request) +{ + $data = json_decode($request->getContent(), true); + if (json_last_error() || ! is_array($data)) { + return api_error(400, "We couldn't parse the provided json. " . json_last_error_msg()); + } + + $name = $request->route('name'); + if (! $name) { + return api_error(400, 'No device group name provided'); + } + + $deviceGroup = ctype_digit($name) ? DeviceGroup::find($name) : DeviceGroup::where('name', $name)->first(); + + if (! $deviceGroup) { + return api_error(404, "Device group $name not found"); + } + + if ('static' != $deviceGroup->type) { + return api_error(422, 'Only static device group can have devices added'); + } + + $rules = [ + 'devices' => 'array', + 'devices.*' => 'integer', + ]; + + $v = Validator::make($data, $rules); + if ($v->fails()) { + return api_error(422, $v->messages()); + } + + $deviceGroup->devices()->detach($data['devices']); + + return api_success_noresult(200, 'Devices removed'); +} + function get_device_groups(Illuminate\Http\Request $request) { $hostname = $request->route('hostname'); diff --git a/routes/api.php b/routes/api.php index e2c6893ed7..48e1f2aece 100644 --- a/routes/api.php +++ b/routes/api.php @@ -68,6 +68,8 @@ Route::prefix('v0')->namespace('\App\Api\Controllers')->group(function () { }); Route::prefix('devicegroups')->group(function () { + Route::post('{name}/devices', 'LegacyApiController@update_device_group_add_devices')->name('update_device_group_add_devices'); + Route::delete('{name}/devices', 'LegacyApiController@update_device_group_remove_devices')->name('update_device_group_remove_devices'); Route::post('{name}/maintenance', 'LegacyApiController@maintenance_devicegroup')->name('maintenance_devicegroup'); });