From b8a2b7c291988f2e7ee74de9342d54749c0852fc Mon Sep 17 00:00:00 2001 From: laf Date: Wed, 1 Oct 2014 20:49:34 +0100 Subject: [PATCH 1/6] Added /vlans/:hostname route to the API --- html/api_v0.php | 3 +++ html/includes/api_functions.inc.php | 29 +++++++++++++++++++++++++++++ html/pages/api-docs.inc.php | 19 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/html/api_v0.php b/html/api_v0.php index 2431adb3c..879e14ded 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -35,6 +35,9 @@ $app->group('/api', function() use ($app) { $app->get('/:hostname/ports/:ifname', 'authToken', 'get_port_stats_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName $app->get('/:hostname/ports/:ifname/:type', 'authToken', 'get_graph_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName/$type }); + $app->group('/vlans', function() use ($app) { + $app->get('/:hostname', 'authToken', 'get_vlans');//api/v0/vlans/$hostname + }); $app->get('/devices', 'authToken', 'list_devices');//api/v0/devices $app->post('/devices', 'authToken', 'add_device');//api/v0/devices (json data needs to be passed) $app->delete('/devices/:hostname', 'authToken', 'del_device');//api/v0/devices (json data needs to be passed) diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index c728a6dd5..1d8665bcd 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -447,3 +447,32 @@ function del_device() $app->response->headers->set('Content-Type', 'application/json'); echo _json_encode($output); } + +function get_vlans() { + // This will list all vlans for a given device + global $config; + $app = \Slim\Slim::getInstance(); + $router = $app->router()->getCurrentRoute()->getParams(); + $hostname = $router['hostname']; + $status = "error"; + $code = 500; + if(empty($hostname)) { + $output = $output = array("status" => "error", "message" => "No hostname has been provided"); + } else { + require_once("../includes/functions.php"); + $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) { + $vlans = dbFetchRows("SELECT vlan_vlan,vlan_domain,vlan_name,vlan_type,vlan_mtu FROM vlans WHERE `device_id` = ?", array($device_id)); + $output = array("status" => "ok", "vlans" => array($vlans)); + } else { + $code = 404; + $output = array("status" => "error", "Device $hostname not found"); + } + } + echo _json_encode($output); +} diff --git a/html/pages/api-docs.inc.php b/html/pages/api-docs.inc.php index 96888b1ce..f374a99ff 100644 --- a/html/pages/api-docs.inc.php +++ b/html/pages/api-docs.inc.php @@ -231,6 +231,25 @@ if ($_SESSION['userlevel'] == '10') curl -X DELETE -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost"
+ + List VLANs + + + /api + /v0 + /vlans/$hostname + + + + + JSON + + + + curl -X DELETE -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/vlans/localhost"
+ From d2f727cfcb2be6d44591675203451e4cc148a6da Mon Sep 17 00:00:00 2001 From: laf Date: Wed, 1 Oct 2014 20:53:15 +0100 Subject: [PATCH 2/6] Added a total vlan response in api --- html/includes/api_functions.inc.php | 49 +++++++++++++++-------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 1d8665bcd..b50cca4a3 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -449,30 +449,31 @@ function del_device() } function get_vlans() { - // This will list all vlans for a given device - global $config; - $app = \Slim\Slim::getInstance(); - $router = $app->router()->getCurrentRoute()->getParams(); - $hostname = $router['hostname']; - $status = "error"; - $code = 500; - if(empty($hostname)) { - $output = $output = array("status" => "error", "message" => "No hostname has been provided"); - } else { - require_once("../includes/functions.php"); - $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) { - $vlans = dbFetchRows("SELECT vlan_vlan,vlan_domain,vlan_name,vlan_type,vlan_mtu FROM vlans WHERE `device_id` = ?", array($device_id)); - $output = array("status" => "ok", "vlans" => array($vlans)); + // This will list all vlans for a given device + global $config; + $app = \Slim\Slim::getInstance(); + $router = $app->router()->getCurrentRoute()->getParams(); + $hostname = $router['hostname']; + $status = "error"; + $code = 500; + if(empty($hostname)) { + $output = $output = array("status" => "error", "message" => "No hostname has been provided"); } else { - $code = 404; - $output = array("status" => "error", "Device $hostname not found"); + require_once("../includes/functions.php"); + $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) { + $vlans = dbFetchRows("SELECT vlan_vlan,vlan_domain,vlan_name,vlan_type,vlan_mtu FROM vlans WHERE `device_id` = ?", array($device_id)); + $total_vlans = count($vlans); + $output = array("status" => "ok", "count" => $total_vlans, "vlans" => array($vlans)); + } else { + $code = 404; + $output = array("status" => "error", "Device $hostname not found"); + } } - } - echo _json_encode($output); + echo _json_encode($output); } From a87dd794e4556160a620aafa2d19d69ee10b9373 Mon Sep 17 00:00:00 2001 From: laf Date: Wed, 1 Oct 2014 21:00:07 +0100 Subject: [PATCH 3/6] Updated vlans returned, was a double array by accident --- html/includes/api_functions.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index b50cca4a3..d85f4e0fb 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -469,7 +469,7 @@ function get_vlans() { if ($device) { $vlans = dbFetchRows("SELECT vlan_vlan,vlan_domain,vlan_name,vlan_type,vlan_mtu FROM vlans WHERE `device_id` = ?", array($device_id)); $total_vlans = count($vlans); - $output = array("status" => "ok", "count" => $total_vlans, "vlans" => array($vlans)); + $output = array("status" => "ok", "count" => $total_vlans, "vlans" => $vlans); } else { $code = 404; $output = array("status" => "error", "Device $hostname not found"); From c55c1a71ba65b1c016babcad55bda9b1a17e8eb0 Mon Sep 17 00:00:00 2001 From: laf Date: Wed, 1 Oct 2014 21:12:21 +0100 Subject: [PATCH 4/6] missed out the response code and header --- html/includes/api_functions.inc.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index d85f4e0fb..dbadd26f8 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -454,7 +454,6 @@ function get_vlans() { $app = \Slim\Slim::getInstance(); $router = $app->router()->getCurrentRoute()->getParams(); $hostname = $router['hostname']; - $status = "error"; $code = 500; if(empty($hostname)) { $output = $output = array("status" => "error", "message" => "No hostname has been provided"); @@ -475,5 +474,7 @@ function get_vlans() { $output = array("status" => "error", "Device $hostname not found"); } } + $app->response->setStatus($code); + $app->response->headers->set('Content-Type', 'application/json'); echo _json_encode($output); } From d57c0614bfbbe05db551b110d16203688c67833b Mon Sep 17 00:00:00 2001 From: laf Date: Sat, 4 Oct 2014 01:59:45 +0100 Subject: [PATCH 5/6] Updated route url to /api/v0/devices/hostname/vlans --- html/api_v0.php | 4 +--- html/pages/api-docs.inc.php | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/html/api_v0.php b/html/api_v0.php index 879e14ded..dc05c16ff 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -34,9 +34,7 @@ $app->group('/api', function() use ($app) { $app->get('/:hostname/:type', 'authToken', 'get_graph_generic_by_hostname');//api/v0/devices/$hostname/$type $app->get('/:hostname/ports/:ifname', 'authToken', 'get_port_stats_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName $app->get('/:hostname/ports/:ifname/:type', 'authToken', 'get_graph_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName/$type - }); - $app->group('/vlans', function() use ($app) { - $app->get('/:hostname', 'authToken', 'get_vlans');//api/v0/vlans/$hostname + $app->get('/:hostname/vlans', 'authToken', 'get_vlans');//api/v0/devices/$hostname/vlans }); $app->get('/devices', 'authToken', 'list_devices');//api/v0/devices $app->post('/devices', 'authToken', 'add_device');//api/v0/devices (json data needs to be passed) diff --git a/html/pages/api-docs.inc.php b/html/pages/api-docs.inc.php index f374a99ff..b72ab81b4 100644 --- a/html/pages/api-docs.inc.php +++ b/html/pages/api-docs.inc.php @@ -237,7 +237,7 @@ if ($_SESSION['userlevel'] == '10') /api /v0 - /vlans/$hostname + /devices/$hostname/vlans
  • hostname = the hostname to list vlans for
  • @@ -248,7 +248,7 @@ if ($_SESSION['userlevel'] == '10') - curl -X DELETE -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
    "https://librenms.example.com/api/v0/vlans/localhost"
    + curl -X DELETE -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
    "https://librenms.example.com/api/v0/devices/localhost/vlans"
    From 347288ac234cdec2faa7490e628f106dc1654e27 Mon Sep 17 00:00:00 2001 From: laf Date: Sat, 4 Oct 2014 02:05:32 +0100 Subject: [PATCH 6/6] Moved vlans route + updated docs --- html/api_v0.php | 2 +- html/pages/api-docs.inc.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/html/api_v0.php b/html/api_v0.php index dc05c16ff..44bcc59fe 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -31,10 +31,10 @@ $app->group('/api', function() use ($app) { $app->group('/v0', function() use ($app) { $app->group('/devices', function() use ($app) { $app->get('/:hostname', 'authToken', 'get_device');//api/v0/devices/$hostname + $app->get('/:hostname/vlans', 'authToken', 'get_vlans');//api/v0/devices/$hostname/vlans $app->get('/:hostname/:type', 'authToken', 'get_graph_generic_by_hostname');//api/v0/devices/$hostname/$type $app->get('/:hostname/ports/:ifname', 'authToken', 'get_port_stats_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName $app->get('/:hostname/ports/:ifname/:type', 'authToken', 'get_graph_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName/$type - $app->get('/:hostname/vlans', 'authToken', 'get_vlans');//api/v0/devices/$hostname/vlans }); $app->get('/devices', 'authToken', 'list_devices');//api/v0/devices $app->post('/devices', 'authToken', 'add_device');//api/v0/devices (json data needs to be passed) diff --git a/html/pages/api-docs.inc.php b/html/pages/api-docs.inc.php index b72ab81b4..ad4d7dd55 100644 --- a/html/pages/api-docs.inc.php +++ b/html/pages/api-docs.inc.php @@ -248,7 +248,7 @@ if ($_SESSION['userlevel'] == '10') - curl -X DELETE -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
    "https://librenms.example.com/api/v0/devices/localhost/vlans"
    + curl -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
    "https://librenms.example.com/api/v0/devices/localhost/vlans"