Skip to content

Commit

Permalink
Adding API route to set bgpPeerDescr field (librenms#13056)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
geg347 and murrant authored Aug 12, 2021
1 parent 35dbb3e commit 6dbefd4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
27 changes: 27 additions & 0 deletions doc/API/Routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions includes/html/api_functions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
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
Expand Down

0 comments on commit 6dbefd4

Please sign in to comment.