mirror of
				https://github.com/librenms/librenms.git
				synced 2024-10-07 16:52:45 +00:00 
			
		
		
		
	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:
		@@ -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.
 | 
			
		||||
 
 | 
			
		||||
@@ -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 = '';
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user