From b397ec2e42ce327f459806d700df815179152bf8 Mon Sep 17 00:00:00 2001 From: Marcin Haba Date: Thu, 9 Mar 2023 10:18:12 +0100 Subject: [PATCH] baculum: Add output parameter to run restore endpoint Other changes: - provide new queued restore jobid in separate property for output=json - return appropriate error code if new queued restore jobid is absent --- .../protected/API/Pages/API/RestoreRun.php | 65 ++++++++++++++++++- .../protected/API/openapi_baculum.json | 3 + 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/gui/baculum/protected/API/Pages/API/RestoreRun.php b/gui/baculum/protected/API/Pages/API/RestoreRun.php index fcc1f218d..a79d628af 100644 --- a/gui/baculum/protected/API/Pages/API/RestoreRun.php +++ b/gui/baculum/protected/API/Pages/API/RestoreRun.php @@ -20,6 +20,7 @@ * Bacula(R) is a registered trademark of Kern Sibbald. */ +use Baculum\API\Modules\ConsoleOutputPage; use Baculum\API\Modules\BaculumAPIServer; use Baculum\Common\Modules\Errors\JobError; use Baculum\Common\Modules\Logging; @@ -32,11 +33,15 @@ use Prado\Prado; * @category API * @package Baculum API */ -class RestoreRun extends BaculumAPIServer { +class RestoreRun extends ConsoleOutputPage { public function create($params) { $misc = $this->getModule('misc'); $jobid = property_exists($params, 'id') && $misc->isValidInteger($params->id) ? intval($params->id) : null; + $out_format = parent::OUTPUT_FORMAT_RAW; // default output format + if ($this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output'])) { + $out_format = $this->Request['output']; + } $client = null; if (property_exists($params, 'clientid')) { $clientid = intval($params->clientid); @@ -180,6 +185,35 @@ class RestoreRun extends BaculumAPIServer { $restore = $this->getModule('bconsole')->bconsoleCommand($this->director, $command); + if ($restore->exitcode == 0) { + // exit code OK, check output + $queued_jobid = $this->getModule('misc')->findJobIdStartedJob($restore->output); + if (is_null($queued_jobid)) { + // new jobid is not detected, error + $this->error = JobError::ERROR_INVALID_PROPERTY; + $this->output = JobError::MSG_ERROR_INVALID_PROPERTY; + } else { + // new jobid is detected, check output format + if ($out_format == parent::OUTPUT_FORMAT_JSON) { + // JSON format, return output and new jobid + $this->output = $this->getJSONOutput([ + 'output' => $restore->output, + 'queued_jobid' => $queued_jobid + ]); + } elseif ($out_format == parent::OUTPUT_FORMAT_RAW) { + // RAW format, return output + $this->output = $this->getRawOutput([ + 'output' => $restore->output + ]); + } + $this->error = JobError::ERROR_NO_ERRORS; + } + } else { + // exit code WRONG + $this->output = implode(PHP_EOL, $restore->output) . ' Exitcode => ' . $restore->exitcode; + $this->error = JobError::ERROR_WRONG_EXITCODE; + } + // remove temporary plugin files for ($i = 0; $i < count($plugin_files); $i++) { if (!unlink($plugin_files[$i])) { @@ -189,9 +223,34 @@ class RestoreRun extends BaculumAPIServer { ); } } + } + + /** + * Get raw output from restore run command. + * This method will not be called without param. + * + * @param array output parameter + * @return array restore command output + */ + protected function getRawOutput($params = []) { + // Not too much to do here. Required by abstract method. + return $params['output']; + } - $this->output = $restore->output; - $this->error = $restore->exitcode; + /** + * Get parsed JSON output from run restore run command. + * This method will not be called without params. + * + * @param array output parameter and queued jobid + * @return array output and jobid queued job + */ + protected function getJSONOutput($params = []) { + $output = implode(PHP_EOL, $params['output']); + $jobid = (int) $params['queued_jobid']; + return [ + 'output' => $output, + 'jobid' => $jobid + ]; } } ?> diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index c1a20368d..4f27230b4 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -2451,6 +2451,9 @@ } }, "parameters": [ + { + "$ref": "#/components/parameters/Output" + }, { "name": "id", "in": "body", -- 2.47.3