mirror of
https://github.com/stylersnico/librenms.git
synced 2026-08-03 08:01:38 +02:00
Added API route to update devices columns in DB
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
- [`list_devices`](#api-route-10)
|
- [`list_devices`](#api-route-10)
|
||||||
- [`add_device`](#api-route-11)
|
- [`add_device`](#api-route-11)
|
||||||
- [`list_oxidized`](#api-route-21)
|
- [`list_oxidized`](#api-route-21)
|
||||||
|
- [`update_device_field`](#api-route-update_device_field)
|
||||||
- [`routing`](#api-routing)
|
- [`routing`](#api-routing)
|
||||||
- [`list_bgp`](#api-route-1)
|
- [`list_bgp`](#api-route-1)
|
||||||
- [`switching`](#api-switching)
|
- [`switching`](#api-switching)
|
||||||
@@ -462,6 +463,33 @@ Output:
|
|||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <a name="api-route-update_device_field">Function: `update_device_field`</a> [`top`](#top)
|
||||||
|
|
||||||
|
Update devices field in the database.
|
||||||
|
|
||||||
|
Route: /api/v0/devices/:hostname
|
||||||
|
|
||||||
|
Input (JSON):
|
||||||
|
|
||||||
|
- field: The column name within the database
|
||||||
|
- data: The data to update the column with
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
```curl
|
||||||
|
curl -X PATCH -d '{"field": "notes", "data": "This server should be kept online"}' -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
```text
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"status": "ok",
|
||||||
|
"message": "Device notes has been updated"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
## <a name="api-routing">`Routing`</a> [`top`](#top)
|
## <a name="api-routing">`Routing`</a> [`top`](#top)
|
||||||
|
|
||||||
### <a name="api-route-1">Function: `list_bgp`</a> [`top`](#top)
|
### <a name="api-route-1">Function: `list_bgp`</a> [`top`](#top)
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ $app->group(
|
|||||||
// api/v0/devices/$hostname
|
// api/v0/devices/$hostname
|
||||||
$app->get('/:hostname', 'authToken', 'get_device')->name('get_device');
|
$app->get('/:hostname', 'authToken', 'get_device')->name('get_device');
|
||||||
// api/v0/devices/$hostname
|
// api/v0/devices/$hostname
|
||||||
|
$app->patch('/:hostname', 'authToken', 'update_device')->name('update_device_field');
|
||||||
$app->get('/:hostname/vlans', 'authToken', 'get_vlans')->name('get_vlans');
|
$app->get('/:hostname/vlans', 'authToken', 'get_vlans')->name('get_vlans');
|
||||||
// api/v0/devices/$hostname/vlans
|
// api/v0/devices/$hostname/vlans
|
||||||
$app->get('/:hostname/graphs', 'authToken', 'get_graphs')->name('get_graphs');
|
$app->get('/:hostname/graphs', 'authToken', 'get_graphs')->name('get_graphs');
|
||||||
|
|||||||
@@ -978,3 +978,40 @@ function list_bills() {
|
|||||||
$app->response->headers->set('Content-Type', 'application/json');
|
$app->response->headers->set('Content-Type', 'application/json');
|
||||||
echo _json_encode($output);
|
echo _json_encode($output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function update_device() {
|
||||||
|
global $config;
|
||||||
|
$app = \Slim\Slim::getInstance();
|
||||||
|
$router = $app->router()->getCurrentRoute()->getParams();
|
||||||
|
$status = 'error';
|
||||||
|
$message = '';
|
||||||
|
$code = 500;
|
||||||
|
$hostname = $router['hostname'];
|
||||||
|
// use hostname as device_id if it's all digits
|
||||||
|
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
|
||||||
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
$bad_fields = array('id','hostname');
|
||||||
|
if (empty($data['field'])) {
|
||||||
|
$message = 'Device field to patch has not been supplied';
|
||||||
|
}
|
||||||
|
elseif (in_array($data['field'], $bad_fields)) {
|
||||||
|
$message = 'Device field is not allowed to be updated';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (dbUpdate(array(mres($data['field']) => mres($data['data'])), 'devices', '`device_id`=?', array($device_id))) {
|
||||||
|
$status = 'ok';
|
||||||
|
$message = 'Device ' . mres($data['field']) . ' field has been updated';
|
||||||
|
$code = 200;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$message = 'Device ' . mres($data['field']) . ' field failed to be updated';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$output = array(
|
||||||
|
'status' => $status,
|
||||||
|
'message' => $message,
|
||||||
|
);
|
||||||
|
$app->response->setStatus($code);
|
||||||
|
$app->response->headers->set('Content-Type', 'application/json');
|
||||||
|
echo _json_encode($output);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user