From: Marcin Haba Date: Tue, 25 Apr 2023 07:41:54 +0000 (+0200) Subject: baculum: Add order_by and order_direction parameters to volumes endpoint X-Git-Tag: Release-13.0.3~54 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c06c9de0c62ed08a2193a7403d2fd3b47efc600d;p=thirdparty%2Fbacula.git baculum: Add order_by and order_direction parameters to volumes endpoint --- diff --git a/gui/baculum/protected/API/Modules/VolumeManager.php b/gui/baculum/protected/API/Modules/VolumeManager.php index af124c235..0eb22e790 100644 --- a/gui/baculum/protected/API/Modules/VolumeManager.php +++ b/gui/baculum/protected/API/Modules/VolumeManager.php @@ -23,6 +23,7 @@ namespace Baculum\API\Modules; use PDO; +use Baculum\Common\Modules\Errors\VolumeError; /** * Volume manager module. @@ -103,7 +104,7 @@ class VolumeManager extends APIModule { ]; } - public function getVolumes($criteria = array(), $props = [], $limit_val = 0, $offset_val = 0) { + public function getVolumes($criteria = array(), $props = [], $limit_val = 0, $offset_val = 0, $order_by = null, $order_direction = 'DESC') { $order_pool_id = 'PoolId'; $order_volume = 'VolumeName'; $db_params = $this->getModule('api_config')->getConfig('db'); @@ -111,7 +112,15 @@ class VolumeManager extends APIModule { $order_pool_id = strtolower($order_pool_id); $order_volume = strtolower($order_volume); } + + // default sorting $order = " ORDER BY $order_pool_id ASC, $order_volume ASC "; + + // custom sorting + if (is_string($order_by)) { + $order = " ORDER BY $order_by $order_direction "; + } + if (key_exists('voltype', $props)) { $voltypes = []; switch ($props['voltype']) { @@ -172,7 +181,8 @@ LEFT JOIN Pool AS pool2 ON Media.ScratchPoolId = pool2.PoolId LEFT JOIN Pool AS pool3 ON Media.RecyclePoolId = pool3.PoolId LEFT JOIN Storage USING (StorageId) ' . $where['where'] . $order . $limit . $offset; - $volumes = VolumeRecord::finder()->findAllBySql($sql, $where['params']); + $statement = Database::runQuery($sql, $where['params']); + $volumes = $statement->fetchAll(\PDO::FETCH_OBJ); $this->setExtraVariables($volumes); return $volumes; } diff --git a/gui/baculum/protected/API/Pages/API/Volumes.php b/gui/baculum/protected/API/Pages/API/Volumes.php index 090ec6bdb..98c246c97 100644 --- a/gui/baculum/protected/API/Pages/API/Volumes.php +++ b/gui/baculum/protected/API/Pages/API/Volumes.php @@ -44,6 +44,8 @@ class Volumes extends BaculumAPIServer { $voltype = $this->Request->contains('voltype') && $misc->isValidVolType($this->Request['voltype']) ? $this->Request['voltype'] : null; $pool = $this->Request->contains('pool') && $misc->isValidName($this->Request['pool']) ? $this->Request['pool'] : null; $storage = $this->Request->contains('storage') && $misc->isValidName($this->Request['storage']) ? $this->Request['storage'] : null; + $order_by = $this->Request->contains('order_by') && $misc->isValidColumn($this->Request['order_by']) ? $this->Request['order_by']: null; + $order_direction = $this->Request->contains('order_direction') && $misc->isValidOrderDirection($this->Request['order_direction']) ? $this->Request['order_direction'] : 'DESC'; $params = $props = []; @@ -77,11 +79,34 @@ class Volumes extends BaculumAPIServer { $props['storage'] = $storage; } + if (is_string($order_by)) { + $jr = new \ReflectionClass('Baculum\API\Modules\VolumeRecord'); + $sort_cols = $jr->getProperties(); + $order_by_lc = strtolower($order_by); + $cols_excl = ['whenexpire']; + $columns = []; + foreach ($sort_cols as $cols) { + $name = $cols->getName(); + // skip columns not existing in the catalog + if (in_array($name, $cols_excl)) { + continue; + } + $columns[] = $name; + } + if (!in_array($order_by_lc, $columns)) { + $this->output = VolumeError::MSG_ERROR_INVALID_PROPERTY; + $this->error = VolumeError::ERROR_INVALID_PROPERTY; + return; + } + } + $result = $this->getModule('volume')->getVolumes( $params, $props, $limit, - $offset + $offset, + $order_by, + $order_direction ); $this->output = $result; $this->error = VolumeError::ERROR_NO_ERRORS; diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 6a8883010..512eae022 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -4008,6 +4008,25 @@ "type": "string", "enum": ["Append", "Archive", "Disabled", "Full", "Used", "Cleaning", "Purged", "Recycle", "Read-Only", "Error"] } + }, + { + "name": "order_by", + "in": "query", + "required": false, + "description": "Sort by selected volume property (default poolid ASC, volumename ASC). There can be any volume property (mediaid, volumename, slot ...etc.) except whenexpire.", + "schema": { + "type": "string" + } + }, + { + "name": "order_direction", + "in": "query", + "required": false, + "description": "Order direction. It can be 'asc' (ascending order) or 'desc' (descending order - default)", + "schema": { + "type": "string", + "enum": ["asc", "desc"] + } } ] }