From 4efb95e059968a00b687f392086a84203546552c Mon Sep 17 00:00:00 2001 From: Misha Komarovskiy Date: Mon, 19 Nov 2018 17:36:23 +0300 Subject: [PATCH] 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 * api: Add get_links api call Signed-off-by: Misha Komarovskiy --- doc/API/Switching.md | 118 ++++++++++++++++++++++++++++ html/includes/api_functions.inc.php | 57 ++++++++++++++ html/legacy_api_v0.php | 4 + 3 files changed, 179 insertions(+) diff --git a/doc/API/Switching.md b/doc/API/Switching.md index bee543dcf8..bb54068224 100644 --- a/doc/API/Switching.md +++ b/doc/API/Switching.md @@ -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 +} +``` diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 3d73bad1a6..38800b17f8 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -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(); diff --git a/html/legacy_api_v0.php b/html/legacy_api_v0.php index 019290fd94..86851f70d9 100644 --- a/html/legacy_api_v0.php +++ b/html/legacy_api_v0.php @@ -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(