mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Create device groups via API (#10791)
* Add POST ability to devicegroups API endpoint * Simplify function and make use of Lavarel validation for API request
This commit is contained in:
committed by
Tony Murray
parent
ce628399a9
commit
67169bf7b9
@@ -36,7 +36,7 @@ Output:
|
|||||||
"alerted": "1",
|
"alerted": "1",
|
||||||
"open": "1",
|
"open": "1",
|
||||||
"timestamp": "2014-12-11 14:40:02"
|
"timestamp": "2014-12-11 14:40:02"
|
||||||
},
|
}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ Output:
|
|||||||
"alerted": "1",
|
"alerted": "1",
|
||||||
"open": "1",
|
"open": "1",
|
||||||
"timestamp": "2014-12-11 14:40:02"
|
"timestamp": "2014-12-11 14:40:02"
|
||||||
}
|
}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -247,7 +247,7 @@ Output:
|
|||||||
"extra": "{\"mute\":false,\"count\":\"15\",\"delay\":\"300\",\"invert\":false}",
|
"extra": "{\"mute\":false,\"count\":\"15\",\"delay\":\"300\",\"invert\":false}",
|
||||||
"disabled": "0",
|
"disabled": "0",
|
||||||
"name": "A test rule"
|
"name": "A test rule"
|
||||||
},
|
}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -37,6 +37,67 @@ Output:
|
|||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `add_devicegroup`
|
||||||
|
|
||||||
|
Add a new device group. Upon success, the ID of the new device group is returned
|
||||||
|
and the HTTP response code is `201`.
|
||||||
|
|
||||||
|
Route: `/api/v0/devicegroups`
|
||||||
|
|
||||||
|
Input (JSON):
|
||||||
|
|
||||||
|
- `name`: *required* - The name of the device group
|
||||||
|
- `type`: *required* - should be `static` or `dynamic`. Setting this to static
|
||||||
|
requires that the devices input be provided
|
||||||
|
- `desc`: *optional* - Description of the device group
|
||||||
|
- `rules`: *required if type == dynamic* - A set of rules to determine which
|
||||||
|
devices should be included in this device group
|
||||||
|
- `devices`: *required if type == static* - A list of devices that should be
|
||||||
|
included in this group. This is a static list of devices
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
Dynamic Example:
|
||||||
|
|
||||||
|
```curl
|
||||||
|
curl -H 'X-Auth-Token: YOURAPITOKENHERE' \
|
||||||
|
-d '{"name": "New Device Group", \
|
||||||
|
"desc": "A very fancy dynamic group", \
|
||||||
|
"type": "dynamic",
|
||||||
|
"rules": "{\"condition\":\"AND\",\"rules\":[{\"id\":\"access_points.name\",\"field\":\"access_points.name\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"equal\",\"value\":\"accesspoint1\"}],\"valid\":true}"}' \
|
||||||
|
https://librenms.org/api/v0/devicegroups
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": "ok",
|
||||||
|
"id": 86,
|
||||||
|
"message": "Device group New Device Group created"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Static Example:
|
||||||
|
|
||||||
|
```curl
|
||||||
|
curl -H 'X-Auth-Token: YOURAPITOKENHERE' \
|
||||||
|
-X POST \
|
||||||
|
-d '{"name":"New Device Group","type":"static","devices":[261,271]}' \
|
||||||
|
https://librenms.org/api/v0/devicegroups
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": "ok",
|
||||||
|
"id": 86,
|
||||||
|
"message": "Device group New Device Group created"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### `get_devices_by_group`
|
### `get_devices_by_group`
|
||||||
|
|
||||||
List all devices matching the group provided.
|
List all devices matching the group provided.
|
||||||
|
@@ -18,6 +18,7 @@ use App\Models\PortsFdb;
|
|||||||
use App\Models\Sensor;
|
use App\Models\Sensor;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Routing\Router;
|
use Illuminate\Routing\Router;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
use LibreNMS\Alerting\QueryBuilderParser;
|
use LibreNMS\Alerting\QueryBuilderParser;
|
||||||
use LibreNMS\Config;
|
use LibreNMS\Config;
|
||||||
use LibreNMS\Exceptions\InvalidIpException;
|
use LibreNMS\Exceptions\InvalidIpException;
|
||||||
@@ -1684,6 +1685,44 @@ function rename_device(\Illuminate\Http\Request $request)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function add_device_group(\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());
|
||||||
|
}
|
||||||
|
|
||||||
|
$rules = [
|
||||||
|
'name' => 'required|string|unique:device_groups',
|
||||||
|
'type' => 'required|in:dynamic,static',
|
||||||
|
'devices' => 'array|required_if:type,static',
|
||||||
|
'devices.*' => 'integer',
|
||||||
|
'rules' => 'json|required_if:type,dynamic',
|
||||||
|
];
|
||||||
|
|
||||||
|
$v = Validator::make($data, $rules);
|
||||||
|
if ($v->fails()) {
|
||||||
|
return api_error(422, $v->messages());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only use the rules if they are able to be parsed by the QueryBuilder
|
||||||
|
$query = QueryBuilderParser::fromJson($data['rules'])->toSql();
|
||||||
|
if (empty($query)) {
|
||||||
|
return api_error(500, "We couldn't parse your rule");
|
||||||
|
}
|
||||||
|
|
||||||
|
$deviceGroup = DeviceGroup::make(['name' => $data['name'], 'type' => $data['type'], 'desc' => $data['desc']]);
|
||||||
|
$deviceGroup->rules = json_decode($data['rules']);
|
||||||
|
$deviceGroup->save();
|
||||||
|
|
||||||
|
if ($data['type'] == 'static') {
|
||||||
|
$deviceGroup->devices()->sync($data['devices']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return api_success($deviceGroup->id, 'id', 'Device group ' . $deviceGroup->name . ' created', 201);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function get_device_groups(\Illuminate\Http\Request $request)
|
function get_device_groups(\Illuminate\Http\Request $request)
|
||||||
{
|
{
|
||||||
$hostname = $request->route('hostname');
|
$hostname = $request->route('hostname');
|
||||||
|
@@ -74,6 +74,7 @@ Route::group(['prefix' => 'v0', 'namespace' => '\App\Api\Controllers'], function
|
|||||||
Route::delete('rules/{id}', 'LegacyApiController@delete_rule')->name('delete_rule');
|
Route::delete('rules/{id}', 'LegacyApiController@delete_rule')->name('delete_rule');
|
||||||
Route::post('services/{hostname}', 'LegacyApiController@add_service_for_host')->name('add_service_for_host');
|
Route::post('services/{hostname}', 'LegacyApiController@add_service_for_host')->name('add_service_for_host');
|
||||||
Route::get('oxidized/config/search/{searchstring}', 'LegacyApiController@search_oxidized')->name('search_oxidized');
|
Route::get('oxidized/config/search/{searchstring}', 'LegacyApiController@search_oxidized')->name('search_oxidized');
|
||||||
|
Route::post('devicegroups', 'LegacyApiController@add_device_group')->name('add_device_group');
|
||||||
});
|
});
|
||||||
|
|
||||||
// restricted by access
|
// restricted by access
|
||||||
|
Reference in New Issue
Block a user