mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Add port description API endpoints and documentation (#15578)
* Add port description API endpoints and documentation * Change "update_port_description" behaviour to mimic webUI behaviour * Fix code style issue * Fix another code style issue
This commit is contained in:
@@ -379,3 +379,49 @@ Output:
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### `get_port_description`
|
||||
|
||||
Get the description (`ifAlias`) for a given port id.
|
||||
|
||||
Route: `/api/v0/ports/:portid/description`
|
||||
|
||||
Example:
|
||||
|
||||
```curl
|
||||
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/ports/323/description
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
{
|
||||
"status": "ok",
|
||||
"port_description": "GigabitEthernet14"
|
||||
}
|
||||
```
|
||||
|
||||
### `update_port_description`
|
||||
|
||||
Change the description (`ifAlias`) for a given port id.
|
||||
|
||||
Route: `/api/v0/ports/:portid/description`
|
||||
|
||||
Input (JSON):
|
||||
|
||||
- description: The string data to use as the new port description.
|
||||
Sending an empty string will reset the description to default.
|
||||
|
||||
Example:
|
||||
|
||||
```curl
|
||||
curl -X PATCH -d '{"description": "Out-of-Band Management Link"}' -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/ports/323/description
|
||||
```
|
||||
|
||||
Output:
|
||||
```json
|
||||
{
|
||||
"status": "ok",
|
||||
"message": "Port description updated."
|
||||
}
|
||||
```
|
@@ -1107,6 +1107,62 @@ function get_port_info(Illuminate\Http\Request $request)
|
||||
});
|
||||
}
|
||||
|
||||
function update_port_description(Illuminate\Http\Request $request)
|
||||
{
|
||||
$port_id = $request->route('portid');
|
||||
$port = Port::hasAccess(Auth::user())
|
||||
->where([
|
||||
'port_id' => $port_id,
|
||||
])->first();
|
||||
if (empty($port)) {
|
||||
return api_error(400, 'Invalid port ID.');
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true);
|
||||
$field = 'description';
|
||||
$description = $data[$field];
|
||||
|
||||
if (empty($description)) {
|
||||
// from update-ifalias.inc.php:
|
||||
// "Set to repoll so we avoid using ifDescr on port poll"
|
||||
$description = 'repoll';
|
||||
}
|
||||
|
||||
$port->ifAlias = $description;
|
||||
$port->save();
|
||||
|
||||
$ifName = $port->ifName;
|
||||
$device = $port->device_id;
|
||||
|
||||
if ($description == 'repoll') {
|
||||
// No description provided, clear description
|
||||
del_dev_attrib($port, 'ifName:' . $ifName); // "port" object has required device_id
|
||||
log_event("$ifName Port ifAlias cleared via API", $device, 'interface', 3, $port_id);
|
||||
|
||||
return api_success_noresult(200, 'Port description cleared.');
|
||||
} else {
|
||||
// Prevent poller from overwriting new description
|
||||
set_dev_attrib($port, 'ifName:' . $ifName, 1); // see above
|
||||
log_event("$ifName Port ifAlias set via API: $description", $device, 'interface', 3, $port_id);
|
||||
|
||||
return api_success_noresult(200, 'Port description updated.');
|
||||
}
|
||||
}
|
||||
|
||||
function get_port_description(Illuminate\Http\Request $request)
|
||||
{
|
||||
$port_id = $request->route('portid');
|
||||
$port = Port::hasAccess(Auth::user())
|
||||
->where([
|
||||
'port_id' => $port_id,
|
||||
])->first();
|
||||
if (empty($port)) {
|
||||
return api_error(400, 'Invalid port ID.');
|
||||
} else {
|
||||
return api_success($port->ifAlias, 'port_description');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \LibreNMS\Exceptions\ApiException
|
||||
*/
|
||||
|
@@ -134,6 +134,8 @@ Route::prefix('v0')->namespace('\App\Api\Controllers')->group(function () {
|
||||
Route::get('search/{field}/{search?}', 'LegacyApiController@search_ports')->name('search_ports')->where('search', '.*');
|
||||
Route::get('mac/{search}', 'LegacyApiController@search_by_mac')->name('search_mac');
|
||||
Route::get('', 'LegacyApiController@get_all_ports')->name('get_all_ports');
|
||||
Route::get('{portid}/description', 'LegacyApiController@get_port_description')->name('get_port_description');
|
||||
Route::patch('{portid}/description', 'LegacyApiController@update_port_description')->name('update_port_description');
|
||||
});
|
||||
|
||||
Route::prefix('bills')->group(function () {
|
||||
|
Reference in New Issue
Block a user