mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Added API route to update devices columns in DB
This commit is contained in:
@ -978,3 +978,40 @@ function list_bills() {
|
||||
$app->response->headers->set('Content-Type', 'application/json');
|
||||
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