From: Marcin Haba Date: Tue, 19 Dec 2023 08:19:10 +0000 (+0100) Subject: Add enabled filter to clients show endpoint X-Git-Tag: Beta-15.0.1~79 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1810dbd6d6eeaad94403b691cd60580ce99fae2a;p=thirdparty%2Fbacula.git Add enabled filter to clients show endpoint --- diff --git a/gui/baculum/protected/API/Modules/ConsoleOutputShowPage.php b/gui/baculum/protected/API/Modules/ConsoleOutputShowPage.php index 6eb80d09c..9388bcafc 100644 --- a/gui/baculum/protected/API/Modules/ConsoleOutputShowPage.php +++ b/gui/baculum/protected/API/Modules/ConsoleOutputShowPage.php @@ -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; } diff --git a/gui/baculum/protected/API/Pages/API/ClientsShow.php b/gui/baculum/protected/API/Pages/API/ClientsShow.php index e34fc2ce1..8f113e766 100644 --- a/gui/baculum/protected/API/Pages/API/ClientsShow.php +++ b/gui/baculum/protected/API/Pages/API/ClientsShow.php @@ -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; diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index cf8946a6d..4c19179e8 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -977,6 +977,15 @@ }, { "$ref": "#/components/parameters/Output" + }, + { + "name": "enabled", + "in": "query", + "required": false, + "description": "Enabled clients filter. It does not work with 'name' parameter.", + "schema": { + "type": "integer" + } } ] }