From: Marcin Haba Date: Fri, 22 Dec 2023 14:27:39 +0000 (+0100) Subject: Add client plugin list endpoint X-Git-Tag: Beta-15.0.1~77 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=09fd32f8b5d6fad86ce525b2de6bb2e266ce0c31;p=thirdparty%2Fbacula.git Add client plugin list endpoint --- diff --git a/gui/baculum/protected/API/Modules/ClientManager.php b/gui/baculum/protected/API/Modules/ClientManager.php index be5e2d7ea..1265fa987 100644 --- a/gui/baculum/protected/API/Modules/ClientManager.php +++ b/gui/baculum/protected/API/Modules/ClientManager.php @@ -22,7 +22,8 @@ namespace Baculum\API\Modules; -use Prado\Data\ActiveRecord\TActiveRecordCriteria; +use Baculum\API\Modules\Database; +use PDO; /** * Client manager module. @@ -171,5 +172,65 @@ FROM } return $result; } + + /** + * Get unique client plugin list from all clients for given director. + * + * @param array $criteria SQL criteria to get job list + * @param bool $extended extended mode, if true it returns plugin versions too + * @return array unique client plugin list + */ + public function getClientsPlugins($criteria, $extended = false) { + $plugins = []; + $params['Client.Plugins'] = []; + $params['Client.Plugins'][] = [ + 'operator' => '!=', + 'vals' => '' + ]; + $where = Database::getWhere($criteria); + $db_params = $this->getModule('api_config')->getConfig('db'); + if ($db_params['type'] === Database::PGSQL_TYPE) { + $sql = ' + SELECT + DISTINCT regexp_split_to_table(Plugins, \',\') AS plugins + FROM + Client + ' . $where['where']; + $statement = Database::runQuery($sql, $where['params']); + $result = $statement->fetchAll(PDO::FETCH_COLUMN); + $plugins = $result; + } elseif ($db_params['type'] === Database::MYSQL_TYPE) { + $sql = ' + SELECT + Plugins AS plugins + FROM + Client + ' . $where['where']; + $statement = Database::runQuery($sql, $where['params']); + $result = $statement->fetchAll(PDO::FETCH_OBJ); + for ($i = 0; $i < count($result); $i++) { + $spl = explode(',', $result[$i]->plugins); + $plugins = array_merge($plugins, $spl); + } + } + $items = []; + for ($i = 0; $i < count($plugins); $i++) { + if (preg_match('/^(?P[\w\-]+)\((?P[\d\.]+)\)$/', $plugins[$i], $match) === 1) { + if ($extended) { + $items[] = [ + 'plugin' => $match['plugin'], + 'version' => $match['version'] + ]; + } else { + $items[$match['plugin']] = $match['plugin']; + } + } + } + if (!$extended) { + $items = array_values($items); + sort($items); + } + return $items; + } } ?> diff --git a/gui/baculum/protected/API/Pages/API/ClientPluginNames.php b/gui/baculum/protected/API/Pages/API/ClientPluginNames.php new file mode 100644 index 000000000..0eabc5ab4 --- /dev/null +++ b/gui/baculum/protected/API/Pages/API/ClientPluginNames.php @@ -0,0 +1,68 @@ + + * @category API + * @package Baculum API + */ +class ClientPluginNames extends BaculumAPIServer { + public function get() { + $misc = $this->getModule('misc'); + $extended = $this->Request->contains('extended') && $misc->isValidBoolean($this->Request['extended']) ? (bool)$this->Request['extended'] : false; + $result = $this->getModule('bconsole')->bconsoleCommand( + $this->director, + ['.client'], + null, + true + ); + if ($result->exitcode === 0) { + $params = []; + $clients = array_filter($result->output); + if (count($clients) == 0) { + // no $clients criteria means that user has no client resource assigned. + $this->output = []; + $this->error = ClientError::ERROR_NO_ERRORS; + return; + } + + $params['Client.Name'] = []; + $params['Client.Name'][] = [ + 'operator' => 'IN', + 'vals' => $clients + ]; + + $this->output = $this->getModule('client')->getClientsPlugins($params, $extended); + $this->error = ClientError::ERROR_NO_ERRORS; + } else { + + $this->output = $result->output . ', Exitcode=>' . $result->exitcode; + $this->error = ClientError::ERROR_WRONG_EXITCODE; + } + + } +} diff --git a/gui/baculum/protected/API/Pages/API/endpoints.xml b/gui/baculum/protected/API/Pages/API/endpoints.xml index 007a89772..00a6e9026 100644 --- a/gui/baculum/protected/API/Pages/API/endpoints.xml +++ b/gui/baculum/protected/API/Pages/API/endpoints.xml @@ -148,12 +148,13 @@ - + + diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 69541a70f..184dcbeb3 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -10252,10 +10252,54 @@ ] } }, + "/api/v2/plugins/client/names": { + "get": { + "tags": ["plugins"], + "summary": "Get unique client plugin list from all clients", + "description": "Get unique client plugin list from all clients for current director.", + "responses": { + "200": { + "description": "List unique client plugin list from all clients for current director", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "output": { + "type": "array", + "items": { + "description": "Unique client plugin list", + "type": "string" + } + }, + "error": { + "type": "integer", + "description": "Error code", + "enum": [0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 1000] + } + } + } + } + } + } + }, + "parameters": [ + { + "name": "extended", + "in": "query", + "description": "Extended list with plugin versions. Note it displays multiple the same client names if version for each of them is different.", + "required": false, + "schema": { + "type": "boolean" + } + } + ] + } + }, "/api/v2/plugins/core/{storageid}/storage/ls": { "get": { "tags": ["plugins"], - "summary": "List files and directories on the storage daemon host",, + "summary": "List files and directories on the storage daemon host", "description": "Get list of files and directories on the storage daemon host.", "responses": { "200": {