Adding API route to set bgpPeerDescr field (#13056)

* Adding API route to set bgpPeerDescr field

* Fixing CI test

* Fixing CI test part 2

* Adding validation of bgpPeerId as integer and changing SQL request with question mark syntax.

* Fixing CI test

* Following murrant's advices to protect against SQL injection

* Use built in json function

Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
geg347
2021-08-13 00:58:47 +02:00
committed by GitHub
parent 35dbb3e9cb
commit 6dbefd4a61
3 changed files with 57 additions and 0 deletions

View File

@@ -103,6 +103,33 @@ Output:
}
```
### `edit_bgp_descr`
This is a POST type request
Set the BGP session description by ID
Route: `/api/v0/bgp/:id`
Input:
- id = The id of the BGP Peer Session.
- bgp_descr = The description for the bgpPeerDescr field on the BGP Session.
Example:
```curl
curl -v -H 'X-Auth-Token: YOURAPITOKENHERE' --data '{"bgp_descr": "Your description here"}' https://librenms.org/api/v0/bgp/4
```
Output:
```json
{
"status": "ok",
"message": "BGP description for peer X.X.X.X on device 1 updated to Your description here"
}
```
### `list_cbgp`
List the current BGP sessions counters.

View File

@@ -604,6 +604,35 @@ function get_bgp(Illuminate\Http\Request $request)
return api_success($bgp_session, 'bgp_session');
}
function edit_bgp_descr(Illuminate\Http\Request $request)
{
$bgp_descr = $request->json('bgp_descr');
if (! $bgp_descr) {
return api_error(500, 'Invalid JSON data');
}
//find existing bgp for update
$bgpPeerId = $request->route('id');
if (! is_numeric($bgpPeerId)) {
return api_error(400, 'Invalid id has been provided');
}
$peer = \App\Models\BgpPeer::firstWhere('bgpPeer_id', $bgpPeerId);
// update existing bgp
if ($peer === null) {
return api_error(404, 'BGP peer ' . $bgpPeerId . ' does not exist');
}
$peer->bgpPeerDescr = $bgp_descr;
if ($peer->save()) {
return api_success_noresult(200, 'BGP description for peer ' . $peer->bgpPeerIdentifier . ' on device ' . $peer->device_id . ' updated to ' . $peer->bgpPeerDescr . '.');
}
return api_error(500, 'Failed to update existing bgp');
}
function list_cbgp(Illuminate\Http\Request $request)
{
$sql = '';

View File

@@ -84,6 +84,7 @@ Route::group(['prefix' => 'v0', 'namespace' => '\App\Api\Controllers'], function
Route::delete('locations/{location}', 'LegacyApiController@del_location')->name('del_location');
Route::delete('services/{id}', 'LegacyApiController@del_service_from_host')->name('del_service_from_host');
Route::patch('services/{id}', 'LegacyApiController@edit_service_for_host')->name('edit_service_for_host');
Route::post('bgp/{id}', 'LegacyApiController@edit_bgp_descr')->name('edit_bgp_descr');
});
// restricted by access