Added api call to delete device

This commit is contained in:
laf
2014-09-17 21:03:02 +01:00
parent d40244cb06
commit 8790bfee0a
4 changed files with 88 additions and 6 deletions

View File

@@ -30,12 +30,13 @@ $app->setName('api');
$app->group('/api', function() use ($app) {
$app->group('/v0', function() use ($app) {
$app->group('/devices', function() use ($app) {
$app->get('/:hostname/ports/:ifname/:type', 'authToken', 'get_graph_by_port_hostname');//api/v1/devices/$hostname/ports/$ifName/$type
$app->get('/:hostname/:type', 'authToken', 'get_graph_generic_by_hostname');//api/v1/devices/$hostname/$type
$app->get('/:hostname/ports/:ifname', 'authToken', 'get_port_stats_by_port_hostname');//api/v1/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/: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('/devices', 'authToken', 'list_devices');//api/v1/devices
$app->post('/devices', 'authToken', 'add_device');//api/v1/devices (json data needs to be passed)
$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)
});
});

View File

@@ -343,7 +343,7 @@ function add_device()
}
if(empty($message))
{
require_once("functions.php");
require_once("../includes/functions.php");
$result = addHost($hostname, $snmpver, $port, $transport, 1);
if($result)
{
@@ -360,3 +360,41 @@ function add_device()
$app->response->headers->set('Content-Type', 'application/json');
echo _json_encode($output);
}
function del_device()
{
// This will add a device using the data passed encoded with json
global $config;
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$hostname = $router['hostname'];
// Default status to error and change it if we need to.
$status = "error";
if(empty($hostname))
{
$message = "No hostname has been provided to delete this device";
}
elseif(empty($hostname))
{
$message = "Missing the device hostname";
}
if(empty($message))
{
require_once("../includes/functions.php");
$device_id = get_device_id($hostname);
$response = delete_device($device_id);
if(empty($response))
{
$message = "Device couldn't be deleted";
}
else
{
$message = $response;
$status = "ok";
}
}
$output = array("status" => $status, "message" => $message);
$app->response->headers->set('Content-Type', 'application/json');
echo _json_encode($output);
}

View File

@@ -193,6 +193,26 @@ if ($_SESSION['userlevel'] == '10')
<tr>
<td colspan="5"><code>curl -X POST -d '{"hostname":"localhost.localdomain","version":"v1","community":"public"}' \<br />-H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \<br/> "https://librenms.example.com/api/v1/devices"</code></td>
</tr>
<a name="delete"></a>
<tr class="success">
<td colspan="5"><strong>Delete</strong></td>
</tr>
<tr>
<td>/api</td>
<td>/v1</td>
<td>/devices/$hostname</td>
<td>
<ul class="list-unstyled">
<li>hostname = the hostname to be deleted</li>
</ul>
</td>
<td>
JSON
</td>
</tr>
<tr>
<td colspan="5"><code>curl -X DELETE -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \<br/> "https://librenms.example.com/api/v1/devices/localhost"</code></td>
</tr>
</table>
</div>
</div>
@@ -211,6 +231,7 @@ if ($_SESSION['userlevel'] == '10')
<li><a href="api-docs/#port_stats">Port Stats</a></li>
<li><a href="api-docs/#list">List</a></li>
<li><a href="api-docs/#add">Add</a></li>
<li><a href="api-docs/#delete">Delete</a></li>
</ul>
</li>
</ul>

View File

@@ -940,4 +940,26 @@ function validate_device_id($id)
}
return($return);
}
function get_device_id($hostname)
{
global $config;
if(empty($hostname))
{
$return = false;
}
else
{
$device_id = dbFetchCell("SELECT `device_id` FROM `devices` WHERE `hostname` = ?", array($hostname));
if(empty($device_id))
{
$return = false;
}
else
{
$return = $device_id;
}
}
return $return;
}
?>