]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add JSON output parameter to show client(s), show job(s), show pool(s) API...
authorMarcin Haba <marcin.haba@bacula.pl>
Sat, 26 Jun 2021 08:05:38 +0000 (10:05 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Sat, 26 Jun 2021 08:10:48 +0000 (10:10 +0200)
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

gui/baculum/protected/API/Class/ConsoleOutputShowPage.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/ClientShow.php
gui/baculum/protected/API/Pages/API/ClientsShow.php
gui/baculum/protected/API/Pages/API/JobShow.php
gui/baculum/protected/API/Pages/API/JobsShow.php
gui/baculum/protected/API/Pages/API/PoolShow.php
gui/baculum/protected/API/Pages/API/PoolsShow.php
gui/baculum/protected/API/Pages/API/StorageShow.php
gui/baculum/protected/API/Pages/API/StoragesShow.php
gui/baculum/protected/API/openapi_baculum.json

diff --git a/gui/baculum/protected/API/Class/ConsoleOutputShowPage.php b/gui/baculum/protected/API/Class/ConsoleOutputShowPage.php
new file mode 100644 (file)
index 0000000..c0f26e4
--- /dev/null
@@ -0,0 +1,103 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2021 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+Prado::using('Application.API.Class.ConsoleOutputPage');
+
+/**
+ * Get console output for 'show' type commands.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @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;
+       }
+}
+?>
index 99f463dfc6cbeeef5317d7b0ab5571756ae8093f..cbc99e58fb0f9d373e4caa4515b9aec8f1a9a25e 100644 (file)
@@ -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;
index d8c587a19c52a30de806aa4f864197b757f9a005..28cd618f4bbcb6daa907b087a85b3e47f8328c24 100644 (file)
@@ -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.
  *
  * @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;
        }
 }
 ?>
index 5d6b8380c860ce8db8a5dc2f2ffe96f207202fee..0750431edcb9dc1bc48f0e6037817cccbd0a8359 100644 (file)
@@ -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.
  *
  * @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;
+       }
 }
 ?>
index eb4b140d2696970636a81a67900482e057b7b16c..f2ee23a96d2b282ec0f95372b17f2ee87c1082ca 100644 (file)
@@ -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.
  *
  * @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;
        }
 }
 
index 2c38d4c9247d0a1ea2af2791e1832cdf38173a39..7ec1171b5eddede17722edb69bbec282932f8956 100644 (file)
@@ -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.
  *
  * @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;
+       }
+}
 ?>
index c346f21b6d4af9470eb57886e0302d7b2f4f2f94..a7ed29d4cdfa19183d33dcced6740bd7fb5436fa 100644 (file)
  *
  * 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.
  *
  * @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;
        }
 }
 ?>
index 4bfc4caf8aeba5a67f93ccac7bed60660d39340f..ed45c969a0ba79434e7fa297da42654d78d8d67b 100644 (file)
@@ -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;
index be449c6455ea69c29e74ca3368247731a2897be1..c963a984bfa960117bbc6ea60c9a3b607881dae7 100644 (file)
@@ -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;
index 7ead2ea10976e6b8f44179e19a5382906f65ad86..ba18c41954ae39f98b630cf729a85deab58487da 100644 (file)
                                                        "type": "string",
                                                        "pattern": "[a-zA-Z0-9:.-_ ]+"
                                                }
+                                       },
+                                       {
+                                               "$ref": "#/components/parameters/Output"
                                        }
                                ]
                        }
                                                "schema": {
                                                        "type": "integer"
                                                }
+                                       },
+                                       {
+                                               "$ref": "#/components/parameters/Output"
                                        }
                                ]
                        }
                                                        "type": "string",
                                                        "pattern": "[a-zA-Z0-9:.-_ ]+"
                                                }
+                                       },
+                                       {
+                                               "$ref": "#/components/parameters/Output"
                                        }
                                ]
                        }
                                                }
                                        }
                                },
-                               "parameters": [{
-                                       "$ref": "#/components/parameters/PoolId"
-                               }]
+                               "parameters": [
+                                       {
+                                               "$ref": "#/components/parameters/PoolId"
+                                       },
+                                       {
+                                               "$ref": "#/components/parameters/Output"
+                                       }
+                               ]
                        }
                },
                "/api/v2/pools/{poolid}/update": {