From 13b13a6b5b132fccdac673bcc78a67a5e13181cd Mon Sep 17 00:00:00 2001 From: Marcin Haba Date: Thu, 1 Jun 2023 15:49:51 +0200 Subject: [PATCH] baculum: Add volume names endpoint --- .../protected/API/Modules/VolumeManager.php | 25 ++++++++++- .../protected/API/Pages/API/VolumeNames.php | 42 +++++++++++++++++++ .../protected/API/Pages/API/endpoints.xml | 1 + .../protected/API/openapi_baculum.json | 38 +++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 gui/baculum/protected/API/Pages/API/VolumeNames.php diff --git a/gui/baculum/protected/API/Modules/VolumeManager.php b/gui/baculum/protected/API/Modules/VolumeManager.php index 0eb22e790..bc4c29abf 100644 --- a/gui/baculum/protected/API/Modules/VolumeManager.php +++ b/gui/baculum/protected/API/Modules/VolumeManager.php @@ -457,11 +457,34 @@ LEFT JOIN Storage USING (StorageId) */ public function getVolumesKeys($criteria = array(), $limit_val = 0) { $volumes = []; - $vols = $this->getVolumes($criteria, $limit_val); + $vols = $this->getVolumes($criteria, [], $limit_val); for ($i = 0; $i < count($vols); $i++) { $volumes[$vols[$i]->volumename] = $vols[$i]; } return $volumes; } + + /** + * Get volumes names. + * + * @param integer $limit_val limit results value + * @return array volume name list + */ + public function getVolumeNames($limit_val = null) { + $limit = ''; + if(is_int($limit_val) && $limit_val > 0) { + $limit = sprintf( + ' LIMIT %d', + $limit_val + ); + } + $sql = 'SELECT VolumeName as volumename + FROM Media + ORDER BY VolumeName ' . $limit; + $statement = Database::runQuery($sql); + $result = $statement->fetchAll(\PDO::FETCH_GROUP); + $volumes = array_keys($result); + return $volumes; + } } ?> diff --git a/gui/baculum/protected/API/Pages/API/VolumeNames.php b/gui/baculum/protected/API/Pages/API/VolumeNames.php new file mode 100644 index 000000000..b2355d78c --- /dev/null +++ b/gui/baculum/protected/API/Pages/API/VolumeNames.php @@ -0,0 +1,42 @@ + + * @category API + * @package Baculum API + */ +class VolumeNames extends BaculumAPIServer { + + public function get() { + $misc = $this->getModule('misc'); + $limit = $this->Request->contains('limit') && $misc->isValidInteger($this->Request['limit']) ? (int)$this->Request['limit'] : 0; + $volumes = $this->getModule('volume')->getVolumeNames($limit); + $this->output = $volumes; + $this->error = VolumeError::ERROR_NO_ERRORS; + } +} diff --git a/gui/baculum/protected/API/Pages/API/endpoints.xml b/gui/baculum/protected/API/Pages/API/endpoints.xml index 8ff16209d..a1dabdde0 100644 --- a/gui/baculum/protected/API/Pages/API/endpoints.xml +++ b/gui/baculum/protected/API/Pages/API/endpoints.xml @@ -61,6 +61,7 @@ + diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 8632fa775..e1bf1f41a 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -5200,6 +5200,44 @@ ] } }, + "/api/v2/volumes/names": { + "get": { + "tags": ["volumes"], + "summary": "Volume names", + "description": "Get volume names.", + "responses": { + "200": { + "description": "Volume names.", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "output": { + "type": "array", + "items": { + "type": "string", + "description": "Volume name" + } + }, + "error": { + "type": "integer", + "description": "Error code", + "enum": [0, 1, 2, 3, 6, 7, 1000] + } + } + } + } + } + } + }, + "parameters": [ + { + "$ref": "#/components/parameters/Limit" + } + ] + } + }, "/api/v2/filesets": { "get": { "tags": ["filesets"], -- 2.47.3