Tidy device deletion

This commit is contained in:
Paul Gear
2014-09-27 16:32:55 +10:00
parent 7a1d342348
commit 48d3a43669
4 changed files with 34 additions and 20 deletions

View File

@@ -28,8 +28,7 @@ if ($argv[1])
$id = getidbyname($host);
if ($id)
{
echo(delete_device($id));
echo("Removed $host\n");
echo(delete_device($id)."\n");
} else {
echo("Host doesn't exist!\n");
}

View File

@@ -416,32 +416,47 @@ function del_device()
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$hostname = $router['hostname'];
$output = array();
// Default status to error and change it if we need to.
$status = "error";
$code = 500;
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";
$message = "No hostname has been provided to delete";
$output = array("status" => $status, "message" => $message);
}
else
{
$message = $response;
require_once("../includes/functions.php");
// allow deleting by device_id or hostname
$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) {
$response = delete_device($device_id);
if(empty($response)) {
// FIXME: Need to provide better diagnostics out of delete_device
$output = array("status" => $status, "message" => "Device deletion failed");
}
else {
// deletion succeeded - include old device details in response
$code = 200;
$status = "ok";
$output = array("status" => $status, "message" => $response, "devices" => array($device));
}
}
$output = array("status" => $status, "message" => $message);
else {
// no device matching the name
$code = 404;
$output = array("status" => $status, "message" => "Device $hostname not found");
}
}
$app->response->setStatus($code);
$app->response->headers->set('Content-Type', 'application/json');
echo _json_encode($output);
}

View File

@@ -19,7 +19,7 @@ if (is_numeric($_REQUEST['id']))
');
if ($_REQUEST['confirm'])
{
print_message(delete_device(mres($_REQUEST['id'])));
print_message(delete_device(mres($_REQUEST['id']))."\n");
}
else
{

View File

@@ -248,7 +248,7 @@ function delete_device($id)
shell_exec("rm -rf ".trim($config['rrd_dir'])."/$host");
$ret = "Removed device $host\n";
$ret = "Removed device $host";
return $ret;
}