]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add order_by and order_direction parameters to volumes endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Tue, 25 Apr 2023 07:41:54 +0000 (09:41 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Mon, 1 May 2023 11:18:25 +0000 (13:18 +0200)
gui/baculum/protected/API/Modules/VolumeManager.php
gui/baculum/protected/API/Pages/API/Volumes.php
gui/baculum/protected/API/openapi_baculum.json

index af124c23571308a86861b508e2ff4338640a812c..0eb22e79072d1eb5bc9956d5552fbe4932a3ece3 100644 (file)
@@ -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;
        }
index 090ec6bdbf38a97a6aab2ae19a7fccceef517244..98c246c974506de7855539fdb1c0bf50016a1b7a 100644 (file)
@@ -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;
index 6a888301001ef2d5039ca795753616cab78e8910..512eae0229408ce2e405a580ff26b0bc79d38388 100644 (file)
                                                        "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"]
+                                               }
                                        }
                                ]
                        }