Added API call to list all ports FDB (#10020)

Add FDB API calls to resolve this [forum request](https://community.librenms.org/t/fdb-table-search-via-api-mac-ip-switch-port/1772/4) 

DO NOT DELETE THIS TEXT

#### Please note

> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.

- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)

#### Testers

If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`.  If there are schema changes, you can ask on discord how to revert.
This commit is contained in:
Misha Komarovskiy
2019-04-09 08:51:01 -04:00
committed by Neil Lathwood
parent e285a9268e
commit 7b4c63a2f4
4 changed files with 122 additions and 0 deletions

View File

@@ -445,6 +445,37 @@ Output:
]
}
```
### `get_device_fdb`
Get a list of FDB entries associated with a device.
Route: `/api/v0/devices/:hostname/fdb`
- hostname can be either the device hostname or id
Example:
```curl
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/fdb
```
Output:
```json
{
"status": "ok",
"ports_fdb": {
"ports_fdb_id": 10,
"port_id": 10000,
"mac_address": "1aaa2bbb3ccc",
"vlan_id": 20000,
"device_id": 1,
"created_at": "2019-01-1 01:01:01",
"updated_at": "2019-01-1 01:01:01"
}
}
```
### `get_device_ip_addresses`
Get a list of IP addresses (v4 and v6) associated with a device.

View File

@@ -187,3 +187,41 @@ Output:
"count": 1
}
```
### `list_fdb`
Get a list of all ports FDB.
Route: `/api/v0/resources/fdb/:mac`
- mac is the specific MAC address you would like to query
Input:
-
Example:
```curl
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/resources/fdb
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/resources/fdb/1aaa2bbb3ccc
```
Output:
```json
{
"status": "ok",
"ports_fdb": [
{
"ports_fdb_id": 10,
"port_id": 10000,
"mac_address": "1aaa2bbb3ccc",
"vlan_id": 20000,
"device_id": 1,
"created_at": "2019-01-1 01:01:01",
"updated_at": "2019-01-1 01:01:01"
},
...
],
"count": 100
}
```

View File

@@ -2025,6 +2025,55 @@ function get_link()
}
function get_fdb()
{
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$hostname = $router['hostname'];
if (empty($hostname)) {
api_error(500, 'No hostname has been provided');
}
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
$device = null;
if ($device_id) {
// save the current details for returning to the client on successful delete
$device = device_by_id_cache($device_id);
}
if (!$device) {
api_error(404, "Device $hostname not found");
}
check_device_permission($device_id);
$fdb = \App\Models\PortsFdb::find($device_id);
api_success($fdb, 'ports_fdb');
}
function list_fdb()
{
check_is_read();
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$mac = $router['mac'];
if (empty($mac)) {
$fdb = \App\Models\PortsFdb::hasAccess(Auth::user())->get();
} else {
$fdb = \App\Models\PortsFdb::find($mac);
}
$total_fdb = $fdb->count();
if ($total_fdb == 0) {
api_error(404, 'Fdb do not exist');
}
api_success($fdb, 'ports_fdb');
}
function list_sensors()
{
check_is_read();

View File

@@ -64,6 +64,8 @@ $app->group(
// api/v0/devices/$hostname/links
$app->get('/:hostname/graphs', 'authToken', 'get_graphs')->name('get_graphs');
// api/v0/devices/$hostname/graphs
$app->get('/:hostname/fdb', 'authToken', 'get_fdb')->name('get_fdb');
// api/v0/devices/$hostname/fdb
$app->get('/:hostname/health(/:type)(/:sensor_id)', 'authToken', 'list_available_health_graphs')->name('list_available_health_graphs');
$app->get('/:hostname/wireless(/:type)(/:sensor_id)', 'authToken', 'list_available_wireless_graphs')->name('list_available_wireless_graphs');
$app->get('/:hostname/ports', 'authToken', 'get_port_graphs')->name('get_port_graphs');
@@ -185,6 +187,8 @@ $app->group(
$app->group(
'/resources',
function () use ($app) {
$app->get('/fdb/', 'authToken', 'list_fdb')->name('list_fdb');
$app->get('/fdb/:mac', 'authToken', 'list_fdb')->name('list_fdb');
$app->get('/links', 'authToken', 'list_links')->name('list_links');
$app->get('/links/:id', 'authToken', 'get_link')->name('get_link');
$app->get('/locations', 'authToken', 'list_locations')->name('list_locations');