Added resources/links and devices/hostname/links API calls for xDP (#9444)

* api: Add list_links and get_link api calls

Signed-off-by: Misha Komarovskiy <zombah@gmail.com>

* api: Add get_links api call

Signed-off-by: Misha Komarovskiy <zombah@gmail.com>
This commit is contained in:
Misha Komarovskiy
2018-11-19 17:36:23 +03:00
committed by Neil Lathwood
parent 6b379afffc
commit 4efb95e059
3 changed files with 179 additions and 0 deletions

View File

@@ -69,3 +69,121 @@ Output:
]
}
```
### `list_links`
Get a list of all Links.
Route: `/api/v0/resources/links`
Input:
-
Example:
```curl
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/resources/links
```
Output:
```json
{
"status": "ok",
"links": [
{
"id": 10,
"local_port_id": 100,
"local_device_id": 1,
"remote_port_id": 200,
"active": 1,
"protocol": "lldp",
"remote_hostname": "host2.example.com",
"remote_device_id": 2,
"remote_port": "xe-0/0/1",
"remote_platform": null,
"remote_version": "Example Router v.1.0"
},
...
],
"count": 100
}
```
### `get_links`
Get a list of Links per giver device.
Route: `/api/v0/devices/:hostname/links`
- hostname can be either the device hostname or id
Input:
-
Example:
```curl
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/links
```
Output:
```json
{
"status": "ok",
"links": [
{
"id": 10,
"local_port_id": 100,
"local_device_id": 1,
"remote_port_id": 200,
"active": 1,
"protocol": "lldp",
"remote_hostname": "host2.example.com",
"remote_device_id": 2,
"remote_port": "xe-0/0/1",
"remote_platform": null,
"remote_version": "Example Router v.1.0"
},
...
],
"count": 10
}
```
### `get_link`
Retrieves Link by ID
Route: `/api/v0/resources/links/:id`
Input:
-
Example:
```curl
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/resources/link/10
```
Output:
```json
{
"status": "ok",
"links": [
{
"id": 10,
"local_port_id": 100,
"local_device_id": 1,
"remote_port_id": 200,
"active": 1,
"protocol": "lldp",
"remote_hostname": "host2.example.com",
"remote_device_id": 2,
"remote_port": "xe-0/0/1",
"remote_platform": null,
"remote_version": "Example Router v.1.0"
}
],
"count": 1
}
```

View File

@@ -1970,6 +1970,63 @@ function list_vlans()
}
function list_links()
{
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$sql = '';
$sql_params = array();
$hostname = $router['hostname'];
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
if (is_numeric($device_id)) {
check_device_permission($device_id);
$sql = " AND `links`.`local_device_id`=?";
$sql_params = array($device_id);
}
if (!LegacyAuth::user()->hasGlobalRead()) {
$sql .= " AND `links`.`local_device_id` IN (SELECT device_id FROM devices_perms WHERE user_id = ?)";
$sql_params[] = LegacyAuth::id();
}
$links = array();
foreach (dbFetchRows("SELECT `links`.* FROM `links` LEFT JOIN `devices` ON `links`.`local_device_id` = `devices`.`device_id` WHERE `links`.`id` IS NOT NULL $sql", $sql_params) as $link) {
$host_id = get_vm_parent_id($device);
$device['ip'] = inet6_ntop($device['ip']);
if (is_numeric($host_id)) {
$device['parent_id'] = $host_id;
}
$links[] = $link;
}
$total_links = count($links);
if ($total_links == 0) {
api_error(404, 'Links do not exist');
}
api_success($links, 'links');
}
function get_link()
{
check_is_read();
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$linkId = $router['id'];
if (!is_numeric($linkId)) {
api_error(400, 'Invalid id has been provided');
}
$link = dbFetchRows("SELECT * FROM `links` WHERE `id` IS NOT NULL AND `id` = ?", array($linkId));
$link_count = count($link);
if ($link_count == 0) {
api_error(404, "Link $linkId does not exist");
}
api_success($link, 'link');
}
function list_ip_addresses()
{
check_is_read();

View File

@@ -60,6 +60,8 @@ $app->group(
$app->patch('/:hostname/rename/:new_hostname', 'authToken', 'rename_device')->name('rename_device');
$app->get('/:hostname/vlans', 'authToken', 'get_vlans')->name('get_vlans');
// api/v0/devices/$hostname/vlans
$app->get('/:hostname/links', 'authToken', 'list_links')->name('get_links');
// api/v0/devices/$hostname/links
$app->get('/:hostname/graphs', 'authToken', 'get_graphs')->name('get_graphs');
// api/v0/devices/$hostname/graphs
$app->get('/:hostname/health(/:type)(/:sensor_id)', 'authToken', 'list_available_health_graphs')->name('list_available_health_graphs');
@@ -183,6 +185,8 @@ $app->group(
$app->group(
'/resources',
function () use ($app) {
$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');
$app->get('/vlans', 'authToken', 'list_vlans')->name('list_vlans');
$app->group(