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
+1 -2
View File
@@ -28,8 +28,7 @@ if ($argv[1])
$id = getidbyname($host); $id = getidbyname($host);
if ($id) if ($id)
{ {
echo(delete_device($id)); echo(delete_device($id)."\n");
echo("Removed $host\n");
} else { } else {
echo("Host doesn't exist!\n"); echo("Host doesn't exist!\n");
} }
+31 -16
View File
@@ -416,32 +416,47 @@ function del_device()
$app = \Slim\Slim::getInstance(); $app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams(); $router = $app->router()->getCurrentRoute()->getParams();
$hostname = $router['hostname']; $hostname = $router['hostname'];
$output = array();
// Default status to error and change it if we need to. // Default status to error and change it if we need to.
$status = "error"; $status = "error";
$code = 500;
if(empty($hostname)) if(empty($hostname))
{ {
$message = "No hostname has been provided to delete this device"; $message = "No hostname has been provided to delete";
$output = array("status" => $status, "message" => $message);
} }
elseif(empty($hostname)) else
{
$message = "Missing the device hostname";
}
if(empty($message))
{ {
require_once("../includes/functions.php"); require_once("../includes/functions.php");
$device_id = get_device_id($hostname);
$response = delete_device($device_id); // allow deleting by device_id or hostname
if(empty($response)) $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
{ $device = null;
$message = "Device couldn't be deleted"; if ($device_id) {
// save the current details for returning to the client on successful delete
$device = device_by_id_cache($device_id);
} }
else if ($device) {
{ $response = delete_device($device_id);
$message = $response; if(empty($response)) {
$status = "ok"; // 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));
}
}
else {
// no device matching the name
$code = 404;
$output = array("status" => $status, "message" => "Device $hostname not found");
} }
} }
$output = array("status" => $status, "message" => $message);
$app->response->setStatus($code);
$app->response->headers->set('Content-Type', 'application/json'); $app->response->headers->set('Content-Type', 'application/json');
echo _json_encode($output); echo _json_encode($output);
} }
+1 -1
View File
@@ -19,7 +19,7 @@ if (is_numeric($_REQUEST['id']))
'); ');
if ($_REQUEST['confirm']) if ($_REQUEST['confirm'])
{ {
print_message(delete_device(mres($_REQUEST['id']))); print_message(delete_device(mres($_REQUEST['id']))."\n");
} }
else else
{ {
+1 -1
View File
@@ -248,7 +248,7 @@ function delete_device($id)
shell_exec("rm -rf ".trim($config['rrd_dir'])."/$host"); shell_exec("rm -rf ".trim($config['rrd_dir'])."/$host");
$ret = "Removed device $host\n"; $ret = "Removed device $host";
return $ret; return $ret;
} }