Skip to content

Commit

Permalink
Json protocol contact support update (glpi-project#10316)
Browse files Browse the repository at this point in the history
* Add inventory related hooks constant definitions

* Update json protocol CONTACT support
* use hooks constant definitions
* add hooks support for all tasks supported by GLPI-Agent
* disable not supported tasks
* enable inventory and remoteinventory tasks by default

* Update src/Plugin/Hooks.php

Co-authored-by: Alexandre Delaunay <[email protected]>

* Use is_array & count in place of !empty

Co-authored-by: Alexandre Delaunay <[email protected]>

* Add handle remoteinventory task hook constant definition

* Preset inventory support in hooks params and follow suggestions:
* prepare hooks params in handleTask
* also handle remoteinventory task as hook

Co-authored-by: Alexandre Delaunay <[email protected]>
  • Loading branch information
g-bougard and orthagh authored Jan 17, 2022
1 parent e47d2ee commit 31c25e5
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 15 deletions.
157 changes: 142 additions & 15 deletions src/Inventory/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,43 @@ protected function handleAction($query, $content = null): bool

/**
* Handle Task
*
* @param string $task Task (one of self::*_TASK)
*
* @return array
*/
protected function handleTask($task): array
{
$params = [
'options' => [
'response' => []
],
'item' => $this->inventory->getAgent(),
];
switch ($task) {
case self::INVENT_TASK:
return $this->handleInventoryTask();
return $this->handleInventoryTask($params);
break;
case self::NETDISCOVERY_TASK:
return $this->handleNetDiscoveryTask($params);
break;
case self::NETINV_TASK:
return $this->handleNetInventoryTask($params);
break;
case self::ESX_TASK:
return $this->handleESXTask($params);
break;
case self::COLLECT_TASK:
return $this->handleCollectTask($params);
break;
case self::DEPLOY_TASK:
return $this->handleDeployTask($params);
break;
case self::WOL_TASK:
return $this->handleWakeOnLanTask($params);
break;
case self::REMOTEINV_TASK:
// Task is not supported, disable it and add unsupported message in response
$this->addToResponse([
"message" => "$task task not supported",
"disabled" => $task
]);
return $this->handleRemoteInventoryTask($params);
break;
default:
$this->addError("Task '$task' is not supported.", 400);
Expand Down Expand Up @@ -160,7 +176,7 @@ public function getParams($data)
'item' => $this->inventory->getAgent()
];

$params = Plugin::doHookFunction("inventory_get_params", $params);
$params = Plugin::doHookFunction(Hooks::INVENTORY_GET_PARAMS, $params);

$this->addToResponse($params['options']['response']);
}
Expand Down Expand Up @@ -310,8 +326,16 @@ public function contact($data)
//For the moment it's the Agent who informs us about the active tasks
if (property_exists($this->inventory->getRawData(), 'enabled-tasks')) {
foreach ($this->inventory->getRawData()->{'enabled-tasks'} as $task) {
if ((!empty($handle = $this->handleTask($task)))) {
$response['tasks'] = $handle;
$handle = $this->handleTask($task);
if (is_array($handle) && count($handle)) {
// Insert related task information under tasks list property
$response['tasks'][$task] = $handle;
} else {
// Task is not supported, disable it and add unsupported message in response
$this->addToResponse([
"message" => "$task task not supported",
"disabled" => $task
]);
}
}
}
Expand Down Expand Up @@ -354,18 +378,121 @@ public function inventory($data)
}

/**
* Handle agent inventory task request
* Handle agent enabled inventory task support on contact request
*
* @param array $params Required hooks params
*
* @return array
*/
public function handleInventoryTask(array $params): array
{
// Preset response as glpi supports native inventory by default
$params['options']['response'][self::INVENT_TASK] = [
'server' => 'glpi',
'version' => GLPI_VERSION
];
$params = Plugin::doHookFunction(Hooks::HANDLE_INVENTORY_TASK, $params);

// Return inventory task support
return $params['options']['response'][self::INVENT_TASK] ?? [];
}

/**
* Handle agent enabled netdiscovery task support on contact request
*
* @param array $params Required hooks params
*
* @return array
*/
public function handleInventoryTask()
public function handleNetDiscoveryTask(array $params): array
{
$params = Plugin::doHookFunction(Hooks::HANDLE_NETDISCOVERY_TASK, $params);

return $params['options']['response'][self::NETDISCOVERY_TASK] ?? [];
}

$params['options']['response'] = [];
$params['item'] = $this->inventory->getAgent();
$params = Plugin::doHookFunction("handle_inventory_task", $params);
/**
* Handle agent enabled netinventory task support on contact request
*
* @param array $params Required hooks params
*
* @return array
*/
public function handleNetInventoryTask(array $params): array
{
$params = Plugin::doHookFunction(Hooks::HANDLE_NETINVENTORY_TASK, $params);

return $params['options']['response'][self::NETINV_TASK] ?? [];
}

/**
* Handle agent enabled ESX task support on contact request
*
* @param array $params Required hooks params
*
* @return array
*/
public function handleESXTask(array $params): array
{
$params = Plugin::doHookFunction(Hooks::HANDLE_ESX_TASK, $params);

return $params['options']['response'][self::ESX_TASK] ?? [];
}

/**
* Handle agent enabled collect task support on contact request
*
* @param array $params Required hooks params
*
* @return array
*/
public function handleCollectTask(array $params): array
{
$params = Plugin::doHookFunction(Hooks::HANDLE_COLLECT_TASK, $params);

return $params['options']['response'][self::COLLECT_TASK] ?? [];
}

/**
* Handle agent enabled deploy task support on contact request
*
* @param array $params Required hooks params
*
* @return array
*/
public function handleDeployTask(array $params): array
{
$params = Plugin::doHookFunction(Hooks::HANDLE_DEPLOY_TASK, $params);

return $params['options']['response'][self::DEPLOY_TASK] ?? [];
}

/**
* Handle agent enabled wakeonlan task support on contact request
*
* @param array $params Required hooks params
*
* @return array
*/
public function handleWakeOnLanTask(array $params): array
{
$params = Plugin::doHookFunction(Hooks::HANDLE_WAKEONLAN_TASK, $params);

return $params['options']['response'][self::WAKEONLAN_TASK] ?? [];
}

/**
* Handle agent enabled remoteinventory task support on contact request
*
* @param array $params Required hooks params
*
* @return array
*/
public function handleRemoteInventoryTask(array $params): array
{
$params = Plugin::doHookFunction(Hooks::HANDLE_REMOTEINV_TASK, $params);

return $params['options']['response'];
return $params['options']['response'][self::REMOTEINV_TASK] ?? [];
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Plugin/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ class Hooks
const PROLOG_RESPONSE = 'prolog_response';
const NETWORK_DISCOVERY = 'network_discovery';
const NETWORK_INVENTORY = 'network_inventory';
const INVENTORY_GET_PARAMS = 'inventory_get_params';

// Agent contact request related hooks
const HANDLE_INVENTORY_TASK = 'handle_inventory_task';
const HANDLE_NETDISCOVERY_TASK = 'handle_netdiscovery_task';
const HANDLE_NETINVENTORY_TASK = 'handle_netinventory_task';
const HANDLE_ESX_TASK = 'handle_esx_task';
const HANDLE_COLLECT_TASK = 'handle_collect_task';
const HANDLE_DEPLOY_TASK = 'handle_deploy_task';
const HANDLE_WAKEONLAN_TASK = 'handle_wakeonlan_task';
const HANDLE_REMOTEINV_TASK = 'handle_remoteinventory_task';

// Debug / Development hooks
const DEBUG_TABS = 'debug_tabs';
Expand Down

0 comments on commit 31c25e5

Please sign in to comment.