]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add name parameter to storages endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Wed, 22 Nov 2023 08:21:26 +0000 (09:21 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Wed, 22 Nov 2023 08:22:52 +0000 (09:22 +0100)
gui/baculum/protected/API/Pages/API/Storages.php
gui/baculum/protected/API/openapi_baculum.json

index 2c3dc427693f014585b8d9c23f516158e7c7be69..2646bd7fb0d3cc10042e1f122c9d0d49da092822 100644 (file)
@@ -34,19 +34,36 @@ class Storages extends BaculumAPIServer {
 
        public function get() {
                $misc = $this->getModule('misc');
+               $storage = $this->Request->contains('name') && $misc->isValidName($this->Request['name']) ? $this->Request['name'] : '';
                $limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : null;
                $offset = $this->Request->contains('offset') && $misc->isValidInteger($this->Request['offset']) ? (int)$this->Request['offset'] : null;
                $storages = $this->getModule('storage')->getStorages();
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.storage')
+                       ['.storage'],
+                       null,
+                       true
                );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
-                       $storages_output = array();
+                       $storages_output = [];
+
+                       if (!empty($storage)) {
+                               if (in_array($storage, $result->output)) {
+                                       $storages_output = [$storage];
+                               } else {
+                                       // storage name provided but not found in the configuration
+                                       $this->output = StorageError::MSG_ERROR_STORAGE_DOES_NOT_EXISTS;
+                                       $this->error = StorageError::ERROR_STORAGE_DOES_NOT_EXISTS;
+                                       return;
+                               }
+                       } else {
+                               $storages_output = $result->output;
+                       }
+
+                       $sds = [];
                        foreach($storages as $storage) {
-                               if(in_array($storage->name, $result->output)) {
-                                       $storages_output[] = $storage;
+                               if(in_array($storage->name, $storages_output)) {
+                                       $sds[] = $storage;
                                }
                        }
                        if (!is_int($offset) || $offset < 0) {
@@ -59,8 +76,8 @@ class Storages extends BaculumAPIServer {
                         * Slice needs to be done here instead in the db because in the catalog can be storages
                         * that do not longer exist in the configuration.
                         */
-                       $storages_output = array_slice($storages_output, $offset, $limit);
-                       $this->output = $storages_output;
+                       $sds = array_slice($sds, $offset, $limit);
+                       $this->output = $sds;
                        $this->error = StorageError::ERROR_NO_ERRORS;
                } else {
                        $this->output = $result->output;
index fd8f95641c6125af6b2533dcb221e5954a8d14e9..e8b9289fd305a1f9c88e1bbf142bab3a3b790a73 100644 (file)
                                        },
                                        {
                                                "$ref": "#/components/parameters/Offset"
+                                       },
+                                       {
+                                               "name": "name",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "Storage name",
+                                               "schema": {
+                                                       "type": "string",
+                                                       "pattern": "[a-zA-Z0-9:.-_ ]+"
+                                               }
                                        }
                                ]
                        }