]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add output parameter to run restore endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Thu, 9 Mar 2023 09:18:12 +0000 (10:18 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Sat, 11 Mar 2023 21:32:26 +0000 (22:32 +0100)
Other changes:
 - provide new queued restore jobid in separate property for output=json
 - return appropriate error code if new queued restore jobid is absent

gui/baculum/protected/API/Pages/API/RestoreRun.php
gui/baculum/protected/API/openapi_baculum.json

index fcc1f218def9b978ac08f91d7f0d9be2886224a5..a79d628aff389abc75077f955fee80e20674c210 100644 (file)
@@ -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
+               ];
        }
 }
 ?>
index c1a20368dbb743c730c0c4ee8289953d907e0e92..4f27230b4f865daf88ca6353dca5d0dc86fc6e3e 100644 (file)
                                        }
                                },
                                "parameters": [
+                                       {
+                                               "$ref": "#/components/parameters/Output"
+                                       },
                                        {
                                                "name": "id",
                                                "in": "body",