api: Added add_service_for_host endpoint to API (#8113)

* Add add_service_for_host endpoint to API

Signed-off-by: Rémy Jacquin <remy@remyj.fr>

* Change permissions to allow users to modify device

Signed-off-by: Rémy Jacquin <remy@remyj.fr>
This commit is contained in:
Rémy Jacquin
2018-01-21 20:56:57 +01:00
committed by Neil Lathwood
parent 8a9fc7783d
commit e12e6720f8
6 changed files with 99 additions and 7 deletions

View File

@ -1249,7 +1249,7 @@ function list_bills()
$bill_ref = mres($_GET['ref']);
$bill_custid = mres($_GET['custid']);
$param = array();
if (!empty($bill_custid)) {
$sql .= '`bill_custid` = ?';
$param[] = $bill_custid;
@ -1721,3 +1721,52 @@ function validate_column_list($columns, $tableName)
$app->stop();
}
}
function add_service_for_host()
{
global $config;
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$hostname = $router['hostname'];
// use hostname as device_id if it's all digits
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
check_device_permission($device_id);
$data = json_decode(file_get_contents('php://input'), true);
$missing_fields = array();
// Check if some required fields are empty
if (empty($data['type'])) {
$missing_fields[] = 'type';
}
if (empty($data['ip'])) {
$missing_fields[] = 'ip';
}
// Print error if required fields are missing
if (!empty($missing_fields)) {
api_error(400, sprintf("Service field%s %s missing: %s.", ((sizeof($missing_fields)>1)?'s':''), ((sizeof($missing_fields)>1)?'are':'is'), implode(', ', $missing_fields)));
}
if (!filter_var($data['ip'], FILTER_VALIDATE_IP)) {
api_error(400, 'service_ip is not a valid IP address.');
}
// Check if service type exists
if (!in_array($data['type'], list_available_services())) {
api_error(400, "The service " . $data['type'] . " does not exist.\n Available service types: " . implode(', ', list_available_services()));
}
// Get parameters
$service_type = $data['type'];
$service_ip = $data['ip'];
$service_desc = $data['desc'] ? mres($data['desc']) : '';
$service_param = $data['param'] ? mres($data['param']) : '';
$service_ignore = $data['ignore'] ? true : false; // Default false
// Set the service
$service_id = add_service($device_id, $service_type, $service_desc, $service_ip, $service_param, (int)$service_ignore);
if ($service_id != false) {
api_success_noresult(201, "Service $service_type has been added to device $hostname (#$service_id)");
} else {
api_error(500, 'Failed to add the service');
}
}