api: Added API method to rename devices (#7895)

This commit is contained in:
Thom Seddon
2017-12-19 09:32:31 +00:00
committed by Neil Lathwood
parent 70d68db6e3
commit d6d4a3a69a
3 changed files with 53 additions and 0 deletions

View File

@@ -911,6 +911,34 @@ Output:
]
```
### `rename_device`
Rename device.
Route: `/api/v0/devices/:hostname/rename/:new_hostname`
- hostname can be either the device hostname or id
Input:
-
Examples:
```curl
curl -X PATCH -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/rename/localhost2
```
Output:
```json
[
{
"status": "ok",
"message": "Device has been renamed"
}
]
```
### `get_device_groups`
List the device groups that a device is matched on.

View File

@@ -52,6 +52,7 @@ $app->group(
$app->get('/:hostname', 'authToken', 'get_device')->name('get_device');
// api/v0/devices/$hostname
$app->patch('/:hostname', 'authToken', 'update_device')->name('update_device_field');
$app->patch('/:hostname/rename/:new_hostname', 'authToken', 'rename_device')->name('rename_device');
$app->get('/:hostname/vlans', 'authToken', 'get_vlans')->name('get_vlans');
// api/v0/devices/$hostname/vlans
$app->get('/:hostname/graphs', 'authToken', 'get_graphs')->name('get_graphs');

View File

@@ -1262,6 +1262,30 @@ function update_device()
}
}
function rename_device()
{
check_is_admin();
global $config;
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$hostname = $router['hostname'];
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
$new_hostname = $router['new_hostname'];
$new_device = getidbyname($new_hostname);
if (empty($new_hostname)) {
api_error(500, 'Missing new hostname');
} elseif ($new_device) {
api_error(500, 'Device failed to rename, new hostname already exists');
} else {
if (renamehost($device_id, $new_hostname, 'api') == '') {
api_success_noresult(200, 'Device has been renamed');
} else {
api_success_noresult(200, 'Device failed to be renamed');
}
}
}
function get_device_groups()
{
$app = \Slim\Slim::getInstance();