--- /dev/null
+<?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;
+ }
+}
+?>
* 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
*/
Prado::using('Application.API.Class.ConsoleOutputPage');
+Prado::using('Application.API.Class.ConsoleOutputShowPage');
/**
* Client show command endpoint.
* @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;
* @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;
}
/**
* @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;
* 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
* 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'];
$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;
}
}
?>
* 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
* 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'],
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;
$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;
+ }
}
?>
* 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
* 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'],
$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;
}
}
* 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
* 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;
+ }
+}
?>
*
* 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'];
$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;
}
}
?>
* 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
*/
Prado::using('Application.API.Class.ConsoleOutputPage');
+Prado::using('Application.API.Class.ConsoleOutputShowPage');
/**
* Show storage command endpoint.
* @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
);
$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;
* @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;
}
/**
$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;
* 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
*/
Prado::using('Application.API.Class.ConsoleOutputPage');
+Prado::using('Application.API.Class.ConsoleOutputShowPage');
/**
* Show storages command endpoint.
* @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
);
$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;
* @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;
}
/**
* @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;
"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": {