]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add voltype parameter to volumes endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Tue, 18 Apr 2023 10:57:20 +0000 (12:57 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Thu, 20 Apr 2023 10:00:26 +0000 (12:00 +0200)
gui/baculum/protected/API/Modules/VolumeManager.php
gui/baculum/protected/API/Pages/API/Volumes.php
gui/baculum/protected/API/openapi_baculum.json
gui/baculum/protected/Common/Modules/Miscellaneous.php

index 5c0a8bd6423d3de46a3500ca12d633ca80204925..e8a8c5af7548691f0c2cc014a7316735bdd3e79d 100644 (file)
@@ -33,6 +33,13 @@ use PDO;
  */
 class VolumeManager extends APIModule {
 
+       /**
+        * Vol type groups
+        */
+       const VOLTYPE_GROUP_DISK = 'disk';
+       const VOLTYPE_GROUP_TAPE = 'tape';
+       const VOLTYPE_GROUP_CLOUD = 'cloud';
+
        /**
         * Volume types (voltype property)
         */
@@ -96,7 +103,7 @@ class VolumeManager extends APIModule {
                ];
        }
 
-       public function getVolumes($criteria = array(), $limit_val = 0, $offset_val = 0) {
+       public function getVolumes($criteria = array(), $props = [], $limit_val = 0, $offset_val = 0) {
                $order_pool_id = 'PoolId';
                $order_volume = 'VolumeName';
                $db_params = $this->getModule('api_config')->getConfig('db');
@@ -105,6 +112,32 @@ class VolumeManager extends APIModule {
                    $order_volume = strtolower($order_volume);
                }
                $order = " ORDER BY $order_pool_id ASC, $order_volume ASC ";
+               if (key_exists('voltype', $props)) {
+                       $voltypes = [];
+                       switch ($props['voltype']) {
+                               case self::VOLTYPE_GROUP_DISK: {
+                                       $voltypes = $this->getDiskVolTypes();
+                                       break;
+                               }
+                               case self::VOLTYPE_GROUP_TAPE: {
+                                       $voltypes = $this->getTapeVolTypes();
+                                       break;
+                               }
+                               case self::VOLTYPE_GROUP_CLOUD: {
+                                       $voltypes = $this->getCloudVolTypes();
+                                       break;
+                               }
+                       }
+                       if (count($voltypes) > 0) {
+                               if (!key_exists('Media.VolType', $criteria)) {
+                                       $criteria['Media.VolType'] = [];
+                               }
+                               $criteria['Media.VolType'][] = [
+                                       'vals' => $voltypes,
+                                       'operator' => 'IN'
+                               ];
+                       }
+               }
 
                $limit = '';
                if(is_int($limit_val) && $limit_val > 0) {
@@ -297,7 +330,7 @@ LEFT JOIN Storage USING (StorageId)
                                'vals' => [$poolid],
                                'operator' => 'AND'
                        ]]
-               ), 1);
+               ), [], 1);
                if (is_array($volume) && count($volume) > 0) {
                        $volume = array_shift($volume);
                }
@@ -311,7 +344,7 @@ LEFT JOIN Storage USING (StorageId)
                                'vals' => [$volume_name],
                                'operator' => 'AND'
                        ]]
-               ), 1);
+               ), [], 1);
                if (is_array($volume) && count($volume) > 0) {
                        $volume = array_shift($volume);
                }
index a6c0a10eb9f5b87c4003780ab29a846b7a3914f2..4eb97596519cf2db83217f9951f7fba3b5ca58bf 100644 (file)
@@ -35,7 +35,19 @@ class Volumes extends BaculumAPIServer {
                $misc = $this->getModule('misc');
                $limit = $this->Request->contains('limit') && $misc->isValidInteger($this->Request['limit']) ? (int)$this->Request['limit'] : 0;
                $offset = $this->Request->contains('offset') && $misc->isValidInteger($this->Request['offset']) ? (int)$this->Request['offset'] : 0;
-               $result = $this->getModule('volume')->getVolumes(array(), $limit, $offset);
+               $voltype = $this->Request->contains('voltype') && $misc->isValidVolType($this->Request['voltype']) ? $this->Request['voltype'] : null;
+
+               $props = [];
+               if (is_string($voltype)) {
+                       $props['voltype'] = $voltype;
+               }
+
+               $result = $this->getModule('volume')->getVolumes(
+                       [],
+                       $props,
+                       $limit,
+                       $offset
+               );
                $this->output = $result;
                $this->error = VolumeError::ERROR_NO_ERRORS;
        }
index 8e9f7a67559c5f87e61f7c93b3f4d4e6dfd4d0d5..494363fa29f7da6fdec74d0aad2063b4aefc151b 100644 (file)
                                        },
                                        {
                                                "$ref": "#/components/parameters/Offset"
+                                       },
+                                       {
+                                               "name": "voltype",
+                                               "in": "query",
+                                               "description": "Volume type name. Available types: 'disk', 'tape' and 'cloud'.",
+                                               "required": false,
+                                               "schema": {
+                                                       "type": "string"
+                                               }
                                        }
                                ]
                        }
index a380cd60e1faa9bf6818d2be99630372b1140669..7673855612bed2fe738e06205c7f8bb6d2a66ea5 100644 (file)
@@ -321,6 +321,10 @@ class Miscellaneous extends TModule {
                return (preg_match('/^(basic|full)$/', $view) === 1);
        }
 
+       public function isValidVolType($voltype) {
+               $voltypes = ['disk', 'tape', 'cloud'];
+               return in_array($voltype, $voltypes);
+       }
 
        public function escapeCharsToConsole($path) {
                return preg_replace('/([$])/', '\\\${1}', $path);