API - Allow Hostname on add/remove Device Dependencies (#12319)

* API - Allow Hostname on add/remove Device Dependencies

* style fix

* change device placeholder name
This commit is contained in:
SourceDoctor
2020-11-19 03:23:07 +01:00
committed by GitHub
parent 8ee9d0cfb7
commit 0ede8a2c1d
2 changed files with 27 additions and 7 deletions

View File

@@ -1367,11 +1367,11 @@ Output:
Add one or more parents to a host.
Route: `/api/v0/devices/:device_id/parents`
Route: `/api/v0/devices/:device/parents`
Input (JSON):
- parent_ids: one or more parent ids
- parent_ids: one or more parent ids or hostnames
Example:
```curl
@@ -1390,11 +1390,11 @@ Output:
Deletes some or all the parents from a host.
Route: `/api/v0/devices/:device_id/parents`
Route: `/api/v0/devices/:device/parents`
Input (JSON):
- parent_ids: One or more parent ids, if not specified deletes all parents from host.
- parent_ids: One or more parent ids or hostnames, if not specified deletes all parents from host.
Example:
```curl

View File

@@ -2344,7 +2344,18 @@ function add_parents_to_host(Illuminate\Http\Request $request)
{
$data = json_decode($request->getContent(), true);
$device_id = $request->route('id');
$parent_ids = explode(',', $data['parent_ids']);
$device_id = ctype_digit($device_id) ? $device_id : getidbyname($device_id);
$parent_ids = [];
foreach (explode(',', $data['parent_ids']) as $hostname) {
$hostname = trim($hostname);
$parent_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
if (empty($parent_id)) {
return api_error(400, 'Parent device IDs/Hostname does not exist: ' . $hostname);
}
$parent_ids[] = $parent_id;
}
if (validateDeviceIds($parent_ids) && validateDeviceIds([$device_id]) && (! in_array($device_id, $parent_ids))) {
Device::find($device_id)->parents()->sync($parent_ids);
@@ -2357,15 +2368,24 @@ function add_parents_to_host(Illuminate\Http\Request $request)
function del_parents_from_host(Illuminate\Http\Request $request)
{
$device_id = $request->route('id');
$device_id = ctype_digit($device_id) ? $device_id : getidbyname($device_id);
$data = json_decode($request->getContent(), true);
if (! validateDeviceIds([$device_id])) {
return api_error(400, 'Check your device ID!');
}
$device = Device::find($device_id);
if (! empty($data['parent_ids'])) {
$parents = explode(',', $data['parent_ids']);
foreach (explode(',', $data['parent_ids']) as $hostname) {
$hostname = trim($hostname);
$parent_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
if (empty($parent_id)) {
return api_error(400, 'Parent device IDs/Hostname does not exist: ' . $hostname);
}
$parent_ids[] = $parent_id;
}
//remove parents included in the request if they are valid device ids
$result = validateDeviceIds($parents) ? $device->parents()->detach($parents) : false;
$result = validateDeviceIds($parent_ids) ? $device->parents()->detach($parent_ids) : false;
}
if (is_null($result)) {
//$result doesn't exist so $data['parent_ids'] is empty