]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Add enabled filter to clients show endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Tue, 19 Dec 2023 08:19:10 +0000 (09:19 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Thu, 18 Jan 2024 09:22:37 +0000 (10:22 +0100)
gui/baculum/protected/API/Modules/ConsoleOutputShowPage.php
gui/baculum/protected/API/Pages/API/ClientsShow.php
gui/baculum/protected/API/openapi_baculum.json

index 6eb80d09c08abd27f4105bf62f640cce6cf690eb..9388bcafc721eaa116a2e1c708be47717320397a 100644 (file)
@@ -69,11 +69,12 @@ abstract class ConsoleOutputShowPage extends ConsoleOutputPage {
         * Parse 'show' type command output for all resources given type.
         *
         * @param array $output 'show' command output
+        * @param array $filters filters in [key => value, ...] form
         * @return array parsed output
         */
-       protected function parseOutputAll(array $output) {
+       protected function parseOutputAll(array $output, array $filters = []) {
                $ret = $part = [];
-               $section = '';
+               $skip = false;
                for ($i = 0; $i < count($output); $i++) {
                        $scount = preg_match('/^[A-Za-z]+: name=.+/i', $output[$i]);
                        $mcount = preg_match_all('/(?<=\s)\w+=.*?(?=\s+\w+=.*?|$)/i', $output[$i], $matches);
@@ -88,10 +89,17 @@ abstract class ConsoleOutputShowPage extends ConsoleOutputPage {
                        for ($j = 0; $j < count($matches[0]); $j++) {
                                list($key, $value) = explode('=', $matches[0][$j], 2);
                                $key = strtolower($key);
+                               if (key_exists($key, $filters) && $filters[$key] != $value) {
+                                       // filter values that do not match
+                                       $skip = true;
+                               }
                                if ($i > 0 && $scount == 1 && count($part) > 0) {
-                                       $ret[] = $part;
+                                       if (!$skip) {
+                                               $ret[] = $part;
+                                       }
                                        $part = [];
                                        $scount = 0;
+                                       $skip = false;
                                }
                                if (key_exists($key, $part)) {
                                        /*
@@ -104,8 +112,11 @@ abstract class ConsoleOutputShowPage extends ConsoleOutputPage {
                        }
                }
                if (count($part) > 0) {
-                       $ret[] = $part;
+                       if (!$skip) {
+                               $ret[] = $part;
+                       }
                        $part = [];
+                       $skip = false;
                }
                return $ret;
        }
index e34fc2ce10bf6a22af64840d002dae7dda4703d0..8f113e766b1cb50e9b18d1a264ca7aa3080d408b 100644 (file)
@@ -34,7 +34,9 @@ use Baculum\Common\Modules\Errors\ClientError;
 class ClientsShow extends ConsoleOutputShowPage {
 
        public function get() {
+               $misc = $this->getModule('misc');
                $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW;
+               $enabled = $this->Request->contains('enabled') && $misc->isValidBoolean($this->Request['enabled']) ? (int) $this->Request['enabled'] : null;
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
                        ['.client'],
@@ -59,8 +61,13 @@ class ClientsShow extends ConsoleOutputShowPage {
                }
                $params = [];
                if (is_string($client)) {
-                       $params = ['client' => $client];
+                       $params['client'] = $client;
                }
+               $filters = [];
+               if (is_int($enabled)) {
+                       $filters['enabled'] = $enabled;
+               }
+
                $out = (object)[
                        'output' => [],
                        'exitcode' => 0
@@ -68,7 +75,7 @@ class ClientsShow extends ConsoleOutputShowPage {
                if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) {
                        $out = $this->getRawOutput($params);
                } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) {
-                       $out = $this->getJSONOutput($params);
+                       $out = $this->getJSONOutput($params, $filters);
                }
                $this->output = $out->output;
                $this->error = $out->exitcode;
@@ -97,9 +104,10 @@ class ClientsShow extends ConsoleOutputShowPage {
         * Get show client output in JSON format.
         *
         * @param array $params command parameters
+        * @param array $filter filters in [key => value, ...] form
         * @return StdClass object with output and exitcode
         */
-       protected function getJSONOutput($params = []) {
+       protected function getJSONOutput($params = [], $filters = []) {
                $result = (object)[
                        'output' => [],
                        'exitcode' => 0
@@ -110,7 +118,7 @@ class ClientsShow extends ConsoleOutputShowPage {
                        if (key_exists('client', $params)) {
                                $result->output = $this->parseOutput($output->output);
                        } else {
-                               $result->output = $this->parseOutputAll($output->output);
+                               $result->output = $this->parseOutputAll($output->output, $filters);
                        }
                }
                $result->exitcode = $output->exitcode;
index cf8946a6df74fec194eff247bc18ecb36afd35d9..4c19179e8f038b1d8d3a623f7fac16210cd959e4 100644 (file)
                                        },
                                        {
                                                "$ref": "#/components/parameters/Output"
+                                       },
+                                       {
+                                               "name": "enabled",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "Enabled clients filter. It does not work with 'name' parameter.",
+                                               "schema": {
+                                                       "type": "integer"
+                                               }
                                        }
                                ]
                        }