]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add os, version properties and overview parameter to clients endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Mon, 25 Sep 2023 14:21:14 +0000 (16:21 +0200)
committerEric Bollengier <eric@baculasystems.com>
Tue, 31 Oct 2023 15:00:30 +0000 (16:00 +0100)
gui/baculum/protected/API/Modules/ClientManager.php
gui/baculum/protected/API/Modules/ClientRecord.php
gui/baculum/protected/API/Pages/API/Client.php
gui/baculum/protected/API/Pages/API/Clients.php
gui/baculum/protected/API/openapi_baculum.json

index 1ee3bd4e348e8e9bab2eddae35c4f27ef2a3e082..305526824be78a4f2d12d8bf938799d3c846e72e 100644 (file)
@@ -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<version>[\w\d\.]+)\s+\((?P<release_date>[\da-z]+\))?\s+(?P<os>.+)$/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;
        }
 }
 ?>
index 47f80791cc3dd1865d2e99a630d9f3b11621c864..df6052d28215e5471a7947dfc90cb372fda25d8d 100644 (file)
@@ -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);
        }
index 56a39529bdbaca75a98d343f030e3ca6cee3b20e..9b0bfb4ed2d4ad5d08aade32cf0abae912be8295 100644 (file)
@@ -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) {
index 98e5136bf56089a536d5ee660c2685c359982885..2681bc5c78c917a0e5f89084a8a88cd62f59d832 100644 (file)
@@ -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 {
 
index 615b51b76306f974b5e2452a276790edd28e9bd0..adc7dd326b7fea46b15cfe5dc182f80ebddcf874 100644 (file)
                                                "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"
+                                               }
                                        }
                                ]
                        }
                                "jobretention": {
                                        "description": "Retention time (in seconds) for jobs",
                                        "type": "integer"
+                               },
+                               "os": {
+                                       "description": "Operating system information",
+                                       "type": "string"
+                               },
+                               "version": {
+                                       "description": "Bacula client version",
+                                       "type": "string"
                                }
                        }
                },