From: Marcin Haba Date: Tue, 18 Apr 2023 10:57:20 +0000 (+0200) Subject: baculum: Add voltype parameter to volumes endpoint X-Git-Tag: Release-13.0.3~72 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=40d2ae17eaf8d2511ca7ee7f3b4cab941f361de2;p=thirdparty%2Fbacula.git baculum: Add voltype parameter to volumes endpoint --- diff --git a/gui/baculum/protected/API/Modules/VolumeManager.php b/gui/baculum/protected/API/Modules/VolumeManager.php index 5c0a8bd64..e8a8c5af7 100644 --- a/gui/baculum/protected/API/Modules/VolumeManager.php +++ b/gui/baculum/protected/API/Modules/VolumeManager.php @@ -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); } diff --git a/gui/baculum/protected/API/Pages/API/Volumes.php b/gui/baculum/protected/API/Pages/API/Volumes.php index a6c0a10eb..4eb975965 100644 --- a/gui/baculum/protected/API/Pages/API/Volumes.php +++ b/gui/baculum/protected/API/Pages/API/Volumes.php @@ -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; } diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 8e9f7a675..494363fa2 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -3805,6 +3805,15 @@ }, { "$ref": "#/components/parameters/Offset" + }, + { + "name": "voltype", + "in": "query", + "description": "Volume type name. Available types: 'disk', 'tape' and 'cloud'.", + "required": false, + "schema": { + "type": "string" + } } ] } diff --git a/gui/baculum/protected/Common/Modules/Miscellaneous.php b/gui/baculum/protected/Common/Modules/Miscellaneous.php index a380cd60e..767385561 100644 --- a/gui/baculum/protected/Common/Modules/Miscellaneous.php +++ b/gui/baculum/protected/Common/Modules/Miscellaneous.php @@ -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);