From: Marcin Haba Date: Mon, 25 Sep 2023 14:21:14 +0000 (+0200) Subject: baculum: Add os, version properties and overview parameter to clients endpoint X-Git-Tag: Beta-15.0.0~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08e94aee6bf38bdd200d53b728f1ad1465dcce21;p=thirdparty%2Fbacula.git baculum: Add os, version properties and overview parameter to clients endpoint --- diff --git a/gui/baculum/protected/API/Modules/ClientManager.php b/gui/baculum/protected/API/Modules/ClientManager.php index 1ee3bd4e3..305526824 100644 --- a/gui/baculum/protected/API/Modules/ClientManager.php +++ b/gui/baculum/protected/API/Modules/ClientManager.php @@ -33,15 +33,27 @@ use Prado\Data\ActiveRecord\TActiveRecordCriteria; */ class ClientManager extends APIModule { + /** + * Result modes. + */ + const CLIENT_RESULT_MODE_NORMAL = 'normal'; + const CLIENT_RESULT_MODE_OVERVIEW = 'overview'; + + /** + * Uname pattern to split values. + */ + const CLIENT_UNAME_PATTERN = '/^(?P[\w\d\.]+)\s+\((?P[\da-z]+\))?\s+(?P.+)$/i'; + /** * Get client list. * * @param mixed $limit_val result limit value * @param int $offset_val result offset value * @param array $criteria SQL criteria to get job list + * @param string $mode result mode * @return array clients or empty list if no client found */ - public function getClients($limit_val = 0, $offset_val = 0, $criteria = []) { + public function getClients($limit_val = 0, $offset_val = 0, $criteria = [], $mode = self::CLIENT_RESULT_MODE_NORMAL) { $limit = ''; if(is_int($limit_val) && $limit_val > 0) { $limit = ' LIMIT ' . $limit_val; @@ -55,17 +67,59 @@ class ClientManager extends APIModule { $sql = 'SELECT * FROM Client ' . $where['where'] . $offset . $limit; - $statement = Database::runQuery($sql, $where['params']); - return $statement->fetchAll(\PDO::FETCH_OBJ); + $result = $statement->fetchAll(\PDO::FETCH_OBJ); + $this->setSpecialFields($result); + if ($mode == self::CLIENT_RESULT_MODE_OVERVIEW) { + $sql = 'SELECT COUNT(1) AS count FROM Client ' . $where['where']; + $statement = Database::runQuery($sql, $where['params']); + $count = $statement->fetch(\PDO::FETCH_OBJ); + if (!is_object($count)) { + $count = (object)['count' => 0]; + } + $result = [ + 'clients' => $result, + 'count' => $count->count + ]; + } + return $result; + } + + /** + * Set special client properties. + * + * @param array $result client results (note: reference) + */ + private function setSpecialFields(&$result) { + for ($i = 0; $i < count($result); $i++) { + // Add operating system info and client version + $result[$i]->os = ''; + $result[$i]->version = ''; + if (preg_match(self::CLIENT_UNAME_PATTERN, $result[$i]->uname, $match) === 1) { + $result[$i]->os = $match['os']; + $result[$i]->version = $match['version']; + } + } } public function getClientByName($name) { - return ClientRecord::finder()->findByName($name); + $result = ClientRecord::finder()->findByName($name); + if (is_object($result)) { + $result = [$result]; + $this->setSpecialFields($result); + $result = array_shift($result); + } + return $result; } public function getClientById($id) { - return ClientRecord::finder()->findByclientid($id); + $result = ClientRecord::finder()->findByclientid($id); + if (is_object($result)) { + $result = [$result]; + $this->setSpecialFields($result); + $result = array_shift($result); + } + return $result; } } ?> diff --git a/gui/baculum/protected/API/Modules/ClientRecord.php b/gui/baculum/protected/API/Modules/ClientRecord.php index 47f80791c..df6052d28 100644 --- a/gui/baculum/protected/API/Modules/ClientRecord.php +++ b/gui/baculum/protected/API/Modules/ClientRecord.php @@ -40,6 +40,9 @@ class ClientRecord extends APIDbModule { public $jobretention; public $plugins; + public $os; + public $version; + public static function finder($className = __CLASS__) { return parent::finder($className); } diff --git a/gui/baculum/protected/API/Pages/API/Client.php b/gui/baculum/protected/API/Pages/API/Client.php index 56a39529b..9b0bfb4ed 100644 --- a/gui/baculum/protected/API/Pages/API/Client.php +++ b/gui/baculum/protected/API/Pages/API/Client.php @@ -35,8 +35,9 @@ class Client extends BaculumAPIServer { public function get() { $clientid = $this->Request->contains('id') ? intval($this->Request['id']) : 0; $client = null; + $cli_mod = $this->getModule('client'); if ($clientid > 0) { - $client = $this->getModule('client')->getClientById($clientid); + $client = $cli_mod->getClientById($clientid); } $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.client')); if ($result->exitcode === 0) { diff --git a/gui/baculum/protected/API/Pages/API/Clients.php b/gui/baculum/protected/API/Pages/API/Clients.php index 98e5136bf..2681bc5c7 100644 --- a/gui/baculum/protected/API/Pages/API/Clients.php +++ b/gui/baculum/protected/API/Pages/API/Clients.php @@ -21,6 +21,7 @@ */ use Baculum\API\Modules\BaculumAPIServer; +use Baculum\API\Modules\ClientManager; use Baculum\Common\Modules\Errors\ClientError; /** @@ -38,6 +39,7 @@ class Clients extends BaculumAPIServer { $limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : 0; $offset = $this->Request->contains('offset') && $misc->isValidInteger($this->Request['offset']) ? (int)$this->Request['offset'] : 0; $plugin = $this->Request->contains('plugin') && $misc->isValidAlphaNumeric($this->Request['plugin']) ? $this->Request['plugin'] : ''; + $mode = $this->Request->contains('overview') && $misc->isValidBooleanTrue($this->Request['overview']) ? ClientManager::CLIENT_RESULT_MODE_OVERVIEW : ClientManager::CLIENT_RESULT_MODE_NORMAL; $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.client')); if ($result->exitcode === 0) { $params = []; @@ -78,16 +80,10 @@ class Clients extends BaculumAPIServer { $clients = $this->getModule('client')->getClients( $limit, $offset, - $params + $params, + $mode ); - array_shift($result->output); - $clients_output = array(); - foreach($clients as $client) { - if(in_array($client->name, $result->output)) { - $clients_output[] = $client; - } - } - $this->output = $clients_output; + $this->output = $clients; $this->error = ClientError::ERROR_NO_ERRORS; } else { diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 615b51b76..adc7dd326 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -488,6 +488,15 @@ "schema": { "type": "string" } + }, + { + "name": "overview", + "in": "query", + "required": false, + "description": "If set, it provides the client list overview form. Offset and limit parameters do not apply to overview counts.", + "schema": { + "type": "boolean" + } } ] } @@ -10961,6 +10970,14 @@ "jobretention": { "description": "Retention time (in seconds) for jobs", "type": "integer" + }, + "os": { + "description": "Operating system information", + "type": "string" + }, + "version": { + "description": "Bacula client version", + "type": "string" } } },