]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add restore plugin option fields endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Tue, 18 Oct 2022 13:45:18 +0000 (15:45 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Thu, 17 Nov 2022 09:05:10 +0000 (10:05 +0100)
gui/baculum/protected/API/Modules/ConsoleOutputLlistPage.php
gui/baculum/protected/API/Pages/API/LlistPluginRestoreConfFields.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/endpoints.xml
gui/baculum/protected/API/openapi_baculum.json
gui/baculum/protected/Common/Modules/Errors/PluginError.php

index eda9d212f1660588d8f164e5e35c2f155f680f89..b848dbb8aaa22a0692190604a606add15195ee00 100644 (file)
@@ -51,4 +51,25 @@ abstract class ConsoleOutputLlistPage extends ConsoleOutputPage {
                }
                return $ret;
        }
+
+       /**
+        * Parse list plugin configuration fields.
+        *
+        * @param array $output command output
+        * @return array parsed output
+        */
+       protected function parseFields(array $output) {
+               $ret = $vals = [];
+               $out_len = count($output);
+               for ($i = 0; $i < $out_len; $i++) {
+                       if (preg_match('/^(?P<key>\w+)="?(?P<value>.*?)"?$/i', $output[$i], $matches) === 1) {
+                               $vals[$matches['key']] = $matches['value'];
+                       }
+                       if ((empty($output[$i]) || ($i == ($out_len - 1))) && count($vals) > 0) {
+                               $ret[] = $vals;
+                               $vals = [];
+                       }
+               }
+               return $ret;
+       }
 }
diff --git a/gui/baculum/protected/API/Pages/API/LlistPluginRestoreConfFields.php b/gui/baculum/protected/API/Pages/API/LlistPluginRestoreConfFields.php
new file mode 100644 (file)
index 0000000..72290c6
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2022 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.
+ */
+
+use Baculum\API\Modules\ConsoleOutputPage;
+use Baculum\API\Modules\ConsoleOutputLlistPage;
+use Baculum\Common\Modules\Errors\JobError;
+use Baculum\Common\Modules\Errors\PluginError;
+
+/**
+ * Get console output for list pluginrestoreconf bconsole command to display plugin fields.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category API
+ * @package Baculum API
+ */
+class LlistPluginRestoreConfFields extends ConsoleOutputLlistPage {
+
+       public function get() {
+               $misc = $this->getModule('misc');
+               $jobid = $this->Request->contains('jobid') && $misc->isValidInteger($this->Request['jobid']) ? (int)$this->Request['jobid'] : 0;
+               $restoreobjectid = $this->Request->contains('restoreobjectid') && $misc->isValidInteger($this->Request['restoreobjectid']) ? (int)$this->Request['restoreobjectid'] : 0;
+               $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW;
+
+               if ($jobid === 0) {
+                       $this->output = JobError::MSG_ERROR_JOB_DOES_NOT_EXISTS;
+                       $this->error = JobError::ERROR_JOB_DOES_NOT_EXISTS;
+                       return;
+               }
+
+               if ($restoreobjectid === 0) {
+                       $this->output = PluginError::MSG_ERROR_WRONG_PLUGIN_OPTION;
+                       $this->error = PluginError::ERROR_WRONG_PLUGIN_OPTION;
+                       return;
+               }
+
+               if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) {
+                       $out = $this->getRawOutput([
+                               'jobid' => $jobid,
+                               'restoreobjectid' => $restoreobjectid
+                       ]);
+               } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) {
+                       $out = $this->getJSONOutput([
+                               'jobid' => $jobid,
+                               'restoreobjectid' => $restoreobjectid
+                       ]);
+               }
+               $this->output = $out->output;
+               $this->error = $out->exitcode;
+       }
+
+       /**
+        * Get list 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,
+                       [
+                               'list',
+                               'pluginrestoreconf',
+                               'restoreobjectid="' . $params['restoreobjectid'] . '"',
+                               'jobid="' . $params['jobid'] . '"'
+                       ]
+               );
+       }
+
+       /**
+        * Get list 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->parseFields($output->output);
+               }
+               $result->exitcode = $output->exitcode;
+               return $result;
+       }
+}
index 83cb532c1923578fd145c8a07713cc9a430a4ddc..ae505ad032e9882aa93763d8498badc48c241fe1 100644 (file)
@@ -78,6 +78,7 @@
        <url ServiceParameter="JobFiles" pattern="api/v2/jobs/files/" />
        <url ServiceParameter="RestoreRun" pattern="api/v2/jobs/restore/" />
        <url ServiceParameter="LlistPluginRestoreConf" pattern="api/v2/jobs/restore/plugin/config" />
+       <url ServiceParameter="LlistPluginRestoreConfFields" pattern="api/v2/jobs/restore/plugin/config/fields" />
        <!-- bvfs endpoints-->
        <url ServiceParameter="BVFSUpdate" pattern="api/v2/bvfs/update/" />
        <url ServiceParameter="BVFSLsDirs" pattern="api/v2/bvfs/lsdirs/" />
index f0a3dd73d989c26486cdbcd0fca7a143408589ea..e4c6db3b7e56d8ba17b5d9380f9ebd1e6fa8f1f6 100644 (file)
                                                "schema": {
                                                        "type": "string"
                                                }
+                                       },
+                                       {
+                                               "$ref": "#/components/parameters/Output"
+                                       }
+                               ]
+                       }
+               },
+               "/api/v2/jobs/restore/plugin/config/fields": {
+                       "get": {
+                               "tags": ["jobs"],
+                               "summary": "Plugin configuration in fields form used for restore",
+                               "consumes": [ "application/json" ],
+                               "description": "Backup job plugin configuration in fields form used to restore.",
+                               "responses": {
+                                       "200": {
+                                               "description": "Plugin configuration",
+                                               "content": {
+                                                       "application/json": {
+                                                               "schema": {
+                                                                       "type": "object",
+                                                                       "properties": {
+                                                                               "output": {
+                                                                                       "type": "array",
+                                                                                       "items": {
+                                                                                               "type": "string"
+                                                                                       }
+                                                                               },
+                                                                               "error": {
+                                                                                       "type": "integer",
+                                                                                       "description": "Error code",
+                                                                                       "enum": [0, 1, 4, 5, 6, 7, 11, 50, 151, 1000]
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               },
+                               "parameters": [
+                                       {
+                                               "name": "jobid",
+                                               "in": "query",
+                                               "description": "Job identifier",
+                                               "required": true,
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "restoreobjectid",
+                                               "in": "query",
+                                               "description": "Restore object identifier",
+                                               "required": true,
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "$ref": "#/components/parameters/Output"
                                        }
                                ]
                        }
index 480bf6acb9f7ddd4b0ffd8058e9d60370b54ff0b..60e2e3ca62782b936ebb04b88a251f8aa01f76f8 100644 (file)
@@ -31,6 +31,8 @@ namespace Baculum\Common\Modules\Errors;
  */
 class PluginError extends GenericError {
        const ERROR_EXECUTING_PLUGIN_QUERY_COMMAND = 150;
+       const ERROR_WRONG_PLUGIN_OPTION = 151;
 
        const MSG_ERROR_EXECUTING_PLUGIN_QUERY_COMMAND = 'Error executing plugin query command.';
+       const MSG_ERROR_WRONG_PLUGIN_OPTION = 'Wrong plugin option.';
 }