From: Marcin Haba Date: Sat, 26 Jun 2021 08:05:38 +0000 (+0200) Subject: baculum: Add JSON output parameter to show client(s), show job(s), show pool(s) API... X-Git-Tag: Release-11.3.2~218 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b587f11572d56fa67d8d862dc5e21e9b0cc1e080;p=thirdparty%2Fbacula.git baculum: Add JSON output parameter to show client(s), show job(s), show pool(s) API endpoints Changes: - Added output=json/raw to API endpoints: = /clients/show = /clients/{clientid}/show = /jobs/show = /jobs/{jobid}/show = /pools/show = /pools/{poolid}/show - Adapt endpoints to use new ConsoleOutputShowPage class - Update Baculum OpenAPI documentation --- diff --git a/gui/baculum/protected/API/Class/ConsoleOutputShowPage.php b/gui/baculum/protected/API/Class/ConsoleOutputShowPage.php new file mode 100644 index 000000000..c0f26e403 --- /dev/null +++ b/gui/baculum/protected/API/Class/ConsoleOutputShowPage.php @@ -0,0 +1,103 @@ + + * @category API + * @package Baculum API + */ +abstract class ConsoleOutputShowPage extends ConsoleOutputPage { + + /** + * Parse 'show' type command output for specific resource. + * + * @param array $output 'show' command output + * @return array parsed output + */ + protected function parseOutput(array $output) { + $ret = []; + for ($i = 0; $i < count($output); $i++) { + $mcount = preg_match_all('/(?<=\s)\w+=.+?(?=\s+\w+=.+|$)/i', $output[$i], $matches); + if ($mcount === 0) { + continue; + } + for ($j = 0; $j < count($matches[0]); $j++) { + list($key, $value) = explode('=', $matches[0][$j], 2); + $key = strtolower($key); + if (key_exists($key, $ret)) { + /* + * The most important options are in first lines. + * If keys are double skip the second ones + */ + continue; + } + $ret[$key] = $value; + } + } + return $ret; + } + + /** + * Parse 'show' type command output for all resources given type. + * + * @param array $output 'show' command output + * @return array parsed output + */ + protected function parseOutputAll(array $output) { + $ret = $part = []; + $section = ''; + for ($i = 0; $i < count($output); $i++) { + $scount = preg_match('/^[A-Za-z]+: name=.+/i', $output[$i], $match); + $mcount = preg_match_all('/(?<=\s)\w+=.*?(?=\s+\w+=.*?|$)/i', $output[$i], $matches); + if ($mcount == 0) { + continue; + } + for ($j = 0; $j < count($matches[0]); $j++) { + list($key, $value) = explode('=', $matches[0][$j], 2); + $key = strtolower($key); + if ($i > 0 && $scount == 1 && count($part) > 0) { + $ret[] = $part; + $part = []; + $scount = 0; + } + if (key_exists($key, $part)) { + /* + * The most important options are in first lines. + * If keys are double skip the second ones + */ + continue; + } + $part[$key] = $value; + } + } + if (count($part) > 0) { + $ret[] = $part; + $part = []; + } + return $ret; + } +} +?> diff --git a/gui/baculum/protected/API/Pages/API/ClientShow.php b/gui/baculum/protected/API/Pages/API/ClientShow.php index 99f463dfc..cbc99e58f 100644 --- a/gui/baculum/protected/API/Pages/API/ClientShow.php +++ b/gui/baculum/protected/API/Pages/API/ClientShow.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2019 Kern Sibbald + * Copyright (C) 2013-2021 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -21,6 +21,7 @@ */ Prado::using('Application.API.Class.ConsoleOutputPage'); +Prado::using('Application.API.Class.ConsoleOutputShowPage'); /** * Client show command endpoint. @@ -29,24 +30,28 @@ Prado::using('Application.API.Class.ConsoleOutputPage'); * @category API * @package Baculum API */ -class ClientShow extends ConsoleOutputPage { +class ClientShow extends ConsoleOutputShowPage { public function get() { $clientid = $this->Request->contains('id') ? intval($this->Request['id']) : 0; - $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : parent::OUTPUT_FORMAT_RAW; + $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW; $result = $this->getModule('bconsole')->bconsoleCommand( $this->director, - array('.client') + ['.client'], + null, + true ); if ($result->exitcode === 0) { - array_shift($result->output); $client = $this->getModule('client')->getClientById($clientid); if (is_object($client) && in_array($client->name, $result->output)) { - $out = (object)array('output' => array(), 'exitcode' => 0); - if ($out_format === parent::OUTPUT_FORMAT_RAW) { - $out = $this->getRawOutput(array('client' => $client->name)); - } elseif($out_format === parent::OUTPUT_FORMAT_JSON) { - $out = $this->getJSONOutput(array('client' => $client->name)); + $out = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) { + $out = $this->getRawOutput(['client' => $client->name]); + } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) { + $out = $this->getJSONOutput(['client' => $client->name]); } $this->output = $out->output; $this->error = $out->exitcode; @@ -67,11 +72,10 @@ class ClientShow extends ConsoleOutputPage { * @return StdClass object with output and exitcode */ protected function getRawOutput($params = []) { - $result = $this->getModule('bconsole')->bconsoleCommand( + return $this->getModule('bconsole')->bconsoleCommand( $this->director, - array('show', 'client="' . $params['client'] . '"') + ['show', 'client="' . $params['client'] . '"'] ); - return $result; } /** @@ -81,25 +85,14 @@ class ClientShow extends ConsoleOutputPage { * @return StdClass object with output and exitcode */ protected function getJSONOutput($params = []) { - $result = (object)array('output' => array(), 'exitcode' => 0); + $result = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; $output = $this->getRawOutput($params); if ($output->exitcode === 0) { array_shift($output->output); - for ($i = 0; $i < count($output->output); $i++) { - if (preg_match_all('/(?<=\s)\w+=.+?(?=\s+\w+=.+|$)/i', $output->output[$i], $matches) > 0) { - for ($j = 0; $j < count($matches[0]); $j++) { - list($key, $value) = explode('=', $matches[0][$j], 2); - if (key_exists($key, $result->output)) { - /* - * The most important options are in first lines. - * If keys are double skip the second ones - */ - continue; - } - $result->output[strtolower($key)] = $value; - } - } - } + $result->output = $this->parseOutput($output->output); } $result->exitcode = $output->exitcode; return $result; diff --git a/gui/baculum/protected/API/Pages/API/ClientsShow.php b/gui/baculum/protected/API/Pages/API/ClientsShow.php index d8c587a19..28cd618f4 100644 --- a/gui/baculum/protected/API/Pages/API/ClientsShow.php +++ b/gui/baculum/protected/API/Pages/API/ClientsShow.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2019 Kern Sibbald + * Copyright (C) 2013-2021 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -20,6 +20,9 @@ * Bacula(R) is a registered trademark of Kern Sibbald. */ +Prado::using('Application.API.Class.ConsoleOutputPage'); +Prado::using('Application.API.Class.ConsoleOutputShowPage'); + /** * Clients show command endpoint. * @@ -27,16 +30,18 @@ * @category API * @package Baculum API */ -class ClientsShow extends BaculumAPIServer { +class ClientsShow extends ConsoleOutputShowPage { public function get() { + $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW; $result = $this->getModule('bconsole')->bconsoleCommand( $this->director, - array('.client') + ['.client'], + null, + true ); $client = null; if ($result->exitcode === 0) { - array_shift($result->output); if ($this->Request->contains('name')) { if (in_array($this->Request['name'], $result->output)) { $client = $this->Request['name']; @@ -51,18 +56,64 @@ class ClientsShow extends BaculumAPIServer { $this->error = $result->exitcode; return; } - $cmd = array('show'); + $params = []; if (is_string($client)) { - $cmd[] = 'client="' . $client . '"'; + $params = ['client' => $client]; + } + $out = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) { + $out = $this->getRawOutput($params); + } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) { + $out = $this->getJSONOutput($params); + } + $this->output = $out->output; + $this->error = $out->exitcode; + } + + /** + * Get show clients output from console in raw format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getRawOutput($params = []) { + $cmd = ['show']; + if (key_exists('client', $params)) { + $cmd[] = 'client="' . $params['client'] . '"'; } else { $cmd[] = 'clients'; } - $result = $this->getModule('bconsole')->bconsoleCommand( + return $this->getModule('bconsole')->bconsoleCommand( $this->director, $cmd ); - $this->output = $result->output; - $this->error = $result->exitcode; + } + + /** + * Get show client output in JSON format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getJSONOutput($params = []) { + $result = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + $output = $this->getRawOutput($params); + if ($output->exitcode === 0) { + array_shift($output->output); + if (key_exists('client', $params)) { + $result->output = $this->parseOutput($output->output); + } else { + $result->output = $this->parseOutputAll($output->output); + } + } + $result->exitcode = $output->exitcode; + return $result; } } ?> diff --git a/gui/baculum/protected/API/Pages/API/JobShow.php b/gui/baculum/protected/API/Pages/API/JobShow.php index 5d6b8380c..0750431ed 100644 --- a/gui/baculum/protected/API/Pages/API/JobShow.php +++ b/gui/baculum/protected/API/Pages/API/JobShow.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2019 Kern Sibbald + * Copyright (C) 2013-2021 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -20,6 +20,9 @@ * Bacula(R) is a registered trademark of Kern Sibbald. */ +Prado::using('Application.API.Class.ConsoleOutputPage'); +Prado::using('Application.API.Class.ConsoleOutputShowPage'); + /** * Job show endpoint. * @@ -27,9 +30,10 @@ * @category API * @package Baculum API */ -class JobShow extends BaculumAPIServer { +class JobShow extends ConsoleOutputShowPage { public function get() { $jobid = $this->Request->contains('id') ? intval($this->Request['id']) : 0; + $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW; $result = $this->getModule('bconsole')->bconsoleCommand( $this->director, ['.jobs'], @@ -39,12 +43,17 @@ class JobShow extends BaculumAPIServer { if ($result->exitcode === 0) { $job = $this->getModule('job')->getJobById($jobid); if (is_object($job) && in_array($job->name, $result->output)) { - $result = $this->getModule('bconsole')->bconsoleCommand( - $this->director, - array('show', 'job="' . $job->name . '"') - ); - $this->output = $result->output; - $this->error = $result->exitcode; + $out = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) { + $out = $this->getRawOutput(['job' => $job->name]); + } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) { + $out = $this->getJSONOutput(['job' => $job->name]); + } + $this->output = $out->output; + $this->error = $out->exitcode; } else { $this->output = JobError::MSG_ERROR_JOB_DOES_NOT_EXISTS; $this->error = JobError::ERROR_JOB_DOES_NOT_EXISTS; @@ -54,5 +63,38 @@ class JobShow extends BaculumAPIServer { $this->error = $result->exitcode; } } + + /** + * Get show job output from console in raw format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getRawOutput($params = []) { + return $this->getModule('bconsole')->bconsoleCommand( + $this->director, + ['show', 'job="' . $params['job'] . '"'] + ); + } + + /** + * Get show job output in JSON format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getJSONOutput($params = []) { + $result = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + $output = $this->getRawOutput($params); + if ($output->exitcode === 0) { + array_shift($output->output); + $result->output = $this->parseOutput($output->output); + } + $result->exitcode = $output->exitcode; + return $result; + } } ?> diff --git a/gui/baculum/protected/API/Pages/API/JobsShow.php b/gui/baculum/protected/API/Pages/API/JobsShow.php index eb4b140d2..f2ee23a96 100644 --- a/gui/baculum/protected/API/Pages/API/JobsShow.php +++ b/gui/baculum/protected/API/Pages/API/JobsShow.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2019 Kern Sibbald + * Copyright (C) 2013-2021 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -20,6 +20,9 @@ * Bacula(R) is a registered trademark of Kern Sibbald. */ +Prado::using('Application.API.Class.ConsoleOutputPage'); +Prado::using('Application.API.Class.ConsoleOutputShowPage'); + /** * Show jobs command endpoint. * @@ -27,9 +30,10 @@ * @category API * @package Baculum API */ -class JobsShow extends BaculumAPIServer { +class JobsShow extends ConsoleOutputShowPage { public function get() { + $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW; $result = $this->getModule('bconsole')->bconsoleCommand( $this->director, ['.jobs'], @@ -52,18 +56,65 @@ class JobsShow extends BaculumAPIServer { $this->error = $result->exitcode; return; } - $cmd = array('show'); + + $params = []; if (is_string($job)) { - $cmd[] = 'job="' . $job . '"'; + $params = ['job' => $job]; + } + $out = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) { + $out = $this->getRawOutput($params); + } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) { + $out = $this->getJSONOutput($params); + } + $this->output = $out->output; + $this->error = $out->exitcode; + } + + /** + * Get show job output from console in raw format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getRawOutput($params = []) { + $cmd = ['show']; + if (key_exists('job', $params)) { + $cmd[] = 'job="' . $params['job'] . '"'; } else { $cmd[] = 'jobs'; } - $result = $this->getModule('bconsole')->bconsoleCommand( + return $this->getModule('bconsole')->bconsoleCommand( $this->director, $cmd ); - $this->output = $result->output; - $this->error = $result->exitcode; + } + + /** + * Get show job output in JSON format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getJSONOutput($params = []) { + $result = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + $output = $this->getRawOutput($params); + if ($output->exitcode === 0) { + array_shift($output->output); + if (key_exists('job', $params)) { + $result->output = $this->parseOutput($output->output); + } else { + $result->output = $this->parseOutputAll($output->output); + } + } + $result->exitcode = $output->exitcode; + return $result; } } diff --git a/gui/baculum/protected/API/Pages/API/PoolShow.php b/gui/baculum/protected/API/Pages/API/PoolShow.php index 2c38d4c92..7ec1171b5 100644 --- a/gui/baculum/protected/API/Pages/API/PoolShow.php +++ b/gui/baculum/protected/API/Pages/API/PoolShow.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2019 Kern Sibbald + * Copyright (C) 2013-2021 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -20,6 +20,9 @@ * Bacula(R) is a registered trademark of Kern Sibbald. */ +Prado::using('Application.API.Class.ConsoleOutputPage'); +Prado::using('Application.API.Class.ConsoleOutputShowPage'); + /** * Show pool command endpoint. * @@ -27,22 +30,61 @@ * @category API * @package Baculum API */ -class PoolShow extends BaculumAPIServer { +class PoolShow extends ConsoleOutputShowPage { + public function get() { $poolid = $this->Request->contains('id') ? intval($this->Request['id']) : 0; + $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW; $pool = $this->getModule('pool')->getPoolById($poolid); if (is_object($pool)) { - $result = $this->getModule('bconsole')->bconsoleCommand( - $this->director, - array('show', 'pool="' . $pool->name . '"') - ); - $this->output = $result->output; - $this->error = $result->exitcode; + $out = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) { + $out = $this->getRawOutput(['pool' => $pool->name]); + } elseif ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) { + $out = $this->getJSONOutput(['pool' => $pool->name]); + } + $this->output = $out->output; + $this->error = $out->exitcode; } else { $this->output = PoolError::MSG_ERROR_POOL_DOES_NOT_EXISTS; $this->error = PoolError::ERROR_POOL_DOES_NOT_EXISTS; } } -} + /** + * Get show pool output from console in raw format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getRawOutput($params = []) { + return $this->getModule('bconsole')->bconsoleCommand( + $this->director, + ['show', 'pool="' . $params['pool'] . '"'] + ); + } + + /** + * Get show pool output in JSON format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getJSONOutput($params = []) { + $result = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + $output = $this->getRawOutput($params); + if ($output->exitcode === 0) { + array_shift($output->output); + $result->output = $this->parseOutput($output->output); + } + $result->exitcode = $output->exitcode; + return $result; + } +} ?> diff --git a/gui/baculum/protected/API/Pages/API/PoolsShow.php b/gui/baculum/protected/API/Pages/API/PoolsShow.php index c346f21b6..a7ed29d4c 100644 --- a/gui/baculum/protected/API/Pages/API/PoolsShow.php +++ b/gui/baculum/protected/API/Pages/API/PoolsShow.php @@ -19,7 +19,10 @@ * * Bacula(R) is a registered trademark of Kern Sibbald. */ - + +Prado::using('Application.API.Class.ConsoleOutputPage'); +Prado::using('Application.API.Class.ConsoleOutputShowPage'); + /** * Show pools command endpoint. * @@ -27,17 +30,19 @@ * @category API * @package Baculum API */ -class PoolsShow extends BaculumAPIServer { +class PoolsShow extends ConsoleOutputShowPage { public function get() { + $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW; $result = $this->getModule('bconsole')->bconsoleCommand( $this->director, - array('.pool') + ['.pool'], + null, + true ); $pool = null; if ($result->exitcode === 0) { - array_shift($result->output); if ($this->Request->contains('name')) { if (in_array($this->Request['name'], $result->output)) { $pool = $this->Request['name']; @@ -52,20 +57,64 @@ class PoolsShow extends BaculumAPIServer { $this->error = $result->exitcode; return; } - - $cmd = array('show'); + $params = []; if (is_string($pool)) { - $cmd[] = 'pool="' . $pool . '"'; + $params = ['pool' => $pool]; + } + $out = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) { + $out = $this->getRawOutput($params); + } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) { + $out = $this->getJSONOutput($params); + } + $this->output = $out->output; + $this->error = $out->exitcode; + } + + /** + * Get show pools output from console in raw format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getRawOutput($params = []) { + $cmd = ['show']; + if (key_exists('pool', $params)) { + $cmd[] = 'pool="' . $params['pool'] . '"'; } else { $cmd[] = 'pools'; } - - $result = $this->getModule('bconsole')->bconsoleCommand( + return $this->getModule('bconsole')->bconsoleCommand( $this->director, $cmd ); - $this->output = $result->output; - $this->error = $result->exitcode; + } + + /** + * Get show pools output in JSON format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getJSONOutput($params = []) { + $result = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + $output = $this->getRawOutput($params); + if ($output->exitcode === 0) { + array_shift($output->output); + if (key_exists('pool', $params)) { + $result->output = $this->parseOutput($output->output); + } else { + $result->output = $this->parseOutputAll($output->output); + } + } + $result->exitcode = $output->exitcode; + return $result; } } ?> diff --git a/gui/baculum/protected/API/Pages/API/StorageShow.php b/gui/baculum/protected/API/Pages/API/StorageShow.php index 4bfc4caf8..ed45c969a 100644 --- a/gui/baculum/protected/API/Pages/API/StorageShow.php +++ b/gui/baculum/protected/API/Pages/API/StorageShow.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2020 Kern Sibbald + * Copyright (C) 2013-2021 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -21,6 +21,7 @@ */ Prado::using('Application.API.Class.ConsoleOutputPage'); +Prado::using('Application.API.Class.ConsoleOutputShowPage'); /** * Show storage command endpoint. @@ -29,13 +30,13 @@ Prado::using('Application.API.Class.ConsoleOutputPage'); * @category API * @package Baculum API */ -class StorageShow extends ConsoleOutputPage { +class StorageShow extends ConsoleOutputShowPage { public function get() { $storageid = $this->Request->contains('id') ? intval($this->Request['id']) : 0; - $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : parent::OUTPUT_FORMAT_RAW; + $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW; $result = $this->getModule('bconsole')->bconsoleCommand( $this->director, - array('.storage'), + ['.storage'], null, true ); @@ -43,9 +44,9 @@ class StorageShow extends ConsoleOutputPage { $storage = $this->getModule('storage')->getStorageById($storageid); if (is_object($storage) && in_array($storage->name, $result->output)) { $out = (object)['output' => [], 'exitcode' => 0]; - if ($out_format === parent::OUTPUT_FORMAT_RAW) { + if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) { $out = $this->getRawOutput(['storage' => $storage->name]); - } elseif($out_format === parent::OUTPUT_FORMAT_JSON) { + } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) { $out = $this->getJSONOutput(['storage' => $storage->name]); } $this->output = $out->output; @@ -67,13 +68,12 @@ class StorageShow extends ConsoleOutputPage { * @return StdClass object with output and exitcode */ protected function getRawOutput($params = []) { - $result = $this->getModule('bconsole')->bconsoleCommand( + return $this->getModule('bconsole')->bconsoleCommand( $this->director, - array('show', 'storage="' . $params['storage'] . '"'), + ['show', 'storage="' . $params['storage'] . '"'], null, true ); - return $result; } /** @@ -86,21 +86,7 @@ class StorageShow extends ConsoleOutputPage { $result = (object)['output' => [], 'exitcode' => 0]; $output = $this->getRawOutput($params); if ($output->exitcode === 0) { - for ($i = 0; $i < count($output->output); $i++) { - if (preg_match_all('/(?<=\s)\w+=.+?(?=\s+\w+=.+|$)/i', $output->output[$i], $matches) > 0) { - for ($j = 0; $j < count($matches[0]); $j++) { - list($key, $value) = explode('=', $matches[0][$j], 2); - if (key_exists($key, $result->output)) { - /* - * The most important options are in first lines. - * If keys are double skip the second ones - */ - continue; - } - $result->output[strtolower($key)] = $value; - } - } - } + $result->output = $this->parseOutput($output->output); } $result->exitcode = $output->exitcode; return $result; diff --git a/gui/baculum/protected/API/Pages/API/StoragesShow.php b/gui/baculum/protected/API/Pages/API/StoragesShow.php index be449c645..c963a984b 100644 --- a/gui/baculum/protected/API/Pages/API/StoragesShow.php +++ b/gui/baculum/protected/API/Pages/API/StoragesShow.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2020 Kern Sibbald + * Copyright (C) 2013-2021 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -21,6 +21,7 @@ */ Prado::using('Application.API.Class.ConsoleOutputPage'); +Prado::using('Application.API.Class.ConsoleOutputShowPage'); /** * Show storages command endpoint. @@ -29,13 +30,13 @@ Prado::using('Application.API.Class.ConsoleOutputPage'); * @category API * @package Baculum API */ -class StoragesShow extends ConsoleOutputPage { +class StoragesShow extends ConsoleOutputShowPage { public function get() { - $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : parent::OUTPUT_FORMAT_RAW; + $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW; $result = $this->getModule('bconsole')->bconsoleCommand( $this->director, - array('.storage'), + ['.storage'], null, true ); @@ -55,12 +56,19 @@ class StoragesShow extends ConsoleOutputPage { $this->error = $result->exitcode; return; } + $params = []; + if (is_string($storage)) { + $params = ['storage' => $storage]; + } - $out = (object)['output' => [], 'exitcode' => 0]; - if ($out_format === parent::OUTPUT_FORMAT_RAW) { - $out = $this->getRawOutput(['storage' => $storage]); - } elseif($out_format === parent::OUTPUT_FORMAT_JSON) { - $out = $this->getJSONOutput(['storage' => $storage]); + $out = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; + if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) { + $out = $this->getRawOutput($params); + } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) { + $out = $this->getJSONOutput($params); } $this->output = $out->output; @@ -74,18 +82,17 @@ class StoragesShow extends ConsoleOutputPage { * @return StdClass object with output and exitcode */ protected function getRawOutput($params = []) { - $cmd = array('show'); - if (is_string($params['storage'])) { + $cmd = ['show']; + if (key_exists('storage', $params)) { $cmd[] = 'storage="' . $params['storage'] . '"'; } else { $cmd[] = 'storages'; } - $result = $this->getModule('bconsole')->bconsoleCommand( + return $this->getModule('bconsole')->bconsoleCommand( $this->director, $cmd, true ); - return $result; } /** @@ -95,32 +102,17 @@ class StoragesShow extends ConsoleOutputPage { * @return StdClass object with output and exitcode */ protected function getJSONOutput($params = []) { - $result = (object)['output' => [], 'exitcode' => 0]; + $result = (object)[ + 'output' => [], + 'exitcode' => 0 + ]; $output = $this->getRawOutput($params); if ($output->exitcode === 0) { - $part = []; - for ($i = 0; $i < count($output->output); $i++) { - if (preg_match_all('/(?<=\s)\w+=.+?(?=\s+\w+=.+|$)/i', $output->output[$i], $matches) > 0) { - for ($j = 0; $j < count($matches[0]); $j++) { - list($key, $value) = explode('=', $matches[0][$j], 2); - if (key_exists($key, $result->output)) { - /* - * The most important options are in first lines. - * If keys are double skip the second ones - */ - continue; - } - if ($key == 'name' && count($part) > 0) { - $result->output[] = $part; - $part = []; - } - $part[strtolower($key)] = $value; - } - } - } - if (count($part) > 0) { - $result->output[] = $part; - $part = []; + array_shift($output->output); + if (key_exists('storage', $params)) { + $result->output = $this->parseOutput($output->output); + } else { + $result->output = $this->parseOutputAll($output->output); } } $result->exitcode = $output->exitcode; diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 7ead2ea10..ba18c4195 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -715,6 +715,9 @@ "type": "string", "pattern": "[a-zA-Z0-9:.-_ ]+" } + }, + { + "$ref": "#/components/parameters/Output" } ] } @@ -975,6 +978,9 @@ "schema": { "type": "integer" } + }, + { + "$ref": "#/components/parameters/Output" } ] } @@ -1717,6 +1723,9 @@ "type": "string", "pattern": "[a-zA-Z0-9:.-_ ]+" } + }, + { + "$ref": "#/components/parameters/Output" } ] } @@ -2381,9 +2390,14 @@ } } }, - "parameters": [{ - "$ref": "#/components/parameters/PoolId" - }] + "parameters": [ + { + "$ref": "#/components/parameters/PoolId" + }, + { + "$ref": "#/components/parameters/Output" + } + ] } }, "/api/v2/pools/{poolid}/update": {