]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add restore plugin options endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Fri, 23 Sep 2022 13:14:03 +0000 (15:14 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Thu, 17 Nov 2022 09:05:10 +0000 (10:05 +0100)
gui/baculum/protected/API/Modules/Bconsole.php
gui/baculum/protected/API/Modules/ConsoleOutputLlistPage.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/LlistPluginRestoreConf.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/endpoints.xml
gui/baculum/protected/API/openapi_baculum.json

index c16e56f9dec09336e156958ffc4fa2e483f3ce70..dc380e149f20ac124e9b75306c1baef28de044cb 100644 (file)
@@ -64,6 +64,7 @@ class Bconsole extends APIModule {
                'version',
                'status',
                'list',
+               'llist',
                'messages',
                'show',
                'mount',
diff --git a/gui/baculum/protected/API/Modules/ConsoleOutputLlistPage.php b/gui/baculum/protected/API/Modules/ConsoleOutputLlistPage.php
new file mode 100644 (file)
index 0000000..eda9d21
--- /dev/null
@@ -0,0 +1,54 @@
+<?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.
+ */
+
+namespace Baculum\API\Modules;
+
+/**
+ * Get console output for llist type bconsole commands.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category API
+ * @package Baculum API
+ */
+abstract class ConsoleOutputLlistPage extends ConsoleOutputPage {
+
+       /**
+        * Parse 'llist' type command output in "key: value" form.
+        *
+        * @param array $output command output
+        * @return array parsed output
+        */
+       protected function parseOutput(array $output) {
+               $ret = $vals = [];
+               $out_len = count($output);
+               for ($i = 0; $i < $out_len; $i++) {
+                       if (preg_match('/^\s*(?P<key>\w+)\s*:\s*(?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/LlistPluginRestoreConf.php b/gui/baculum/protected/API/Pages/API/LlistPluginRestoreConf.php
new file mode 100644 (file)
index 0000000..235d5ce
--- /dev/null
@@ -0,0 +1,92 @@
+<?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;
+
+/**
+ * Get console output for llist pluginrestoreconf bconsole command.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category API
+ * @package Baculum API
+ */
+class LlistPluginRestoreConf extends ConsoleOutputLlistPage {
+
+       public function get() {
+               $misc = $this->getModule('misc');
+               $jobids = $this->Request->contains('jobids') && $misc->isValidIdsList($this->Request['jobids']) ? $this->Request['jobids'] : 0;
+               $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW;
+
+               if ($jobids === 0) {
+                       $this->output = JobError::MSG_ERROR_JOB_DOES_NOT_EXISTS;
+                       $this->error = JobError::ERROR_JOB_DOES_NOT_EXISTS;
+                       return;
+               }
+
+               if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) {
+                       $out = $this->getRawOutput(['jobids' => $jobids]);
+               } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) {
+                       $out = $this->getJSONOutput(['jobids' => $jobids]);
+               }
+               $this->output = $out->output;
+               $this->error = $out->exitcode;
+       }
+
+       /**
+        * Get llist 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,
+                       [
+                               'llist',
+                               'pluginrestoreconf',
+                               'jobid="' . $params['jobids'] . '"'
+                       ]
+               );
+       }
+
+       /**
+        * Get llist 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 f0bdf9f618aa1e2b1dbba6578b5e1426db6eedda..83cb532c1923578fd145c8a07713cc9a430a4ddc 100644 (file)
@@ -77,6 +77,7 @@
        <url ServiceParameter="JobListFiles" pattern="api/v2/jobs/{id}/files/" parameters.id="\d+" />
        <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" />
        <!-- bvfs endpoints-->
        <url ServiceParameter="BVFSUpdate" pattern="api/v2/bvfs/update/" />
        <url ServiceParameter="BVFSLsDirs" pattern="api/v2/bvfs/lsdirs/" />
index 3c5af0a56f4ae330ccfe373a1677d97d5b3b1bc9..f0a3dd73d989c26486cdbcd0fca7a143408589ea 100644 (file)
                                                "schema": {
                                                        "type": "string"
                                                }
+                                       },
+                                       {
+                                               "name": "plugin_options",
+                                               "in": "body",
+                                               "description": "Pluging options in JSON form: {objectid1: {option1: value1, option2: value2}, objectid2: {option3: value3}...}",
+                                               "required": false,
+                                               "schema": {
+                                                       "type": "object"
+                                               }
+                                       }
+                               ]
+                       }
+               },
+               "/api/v2/jobs/restore/plugin/config": {
+                       "get": {
+                               "tags": ["jobs"],
+                               "summary": "Plugin configuration used for restore",
+                               "consumes": [ "application/json" ],
+                               "description": "Backup job plugin configuration used to restore.",
+                               "responses": {
+                                       "200": {
+                                               "description": "Plugin configuration",
+                                               "content": {
+                                                       "application/json": {
+                                                               "schema": {
+                                                                       "type": "object",
+                                                                       "properties": {
+                                                                               "output": {
+                                                                                       "type": "array",
+                                                                                       "items": {
+                                                                                               "type": "object",
+                                                                                               "properties": {
+                                                                                                       "jobid": {
+                                                                                                               "type": "string",
+                                                                                                               "description": "Job identifier"
+                                                                                                       },
+                                                                                                       "restoreobjectid": {
+                                                                                                               "type": "string",
+                                                                                                               "description": "Restore object identifier"
+                                                                                                       },
+                                                                                                       "objectname": {
+                                                                                                               "type": "string",
+                                                                                                               "description": "Object name"
+                                                                                                       },
+                                                                                                       "pluginname": {
+                                                                                                               "type": "string",
+                                                                                                               "description": "Plugin line with the plugin definition"
+                                                                                                       },
+                                                                                                       "objecttype": {
+                                                                                                               "type": "string",
+                                                                                                               "description": "Object type"
+                                                                                                       }
+                                                                                               }
+                                                                                       }
+                                                                               },
+                                                                               "error": {
+                                                                                       "type": "integer",
+                                                                                       "description": "Error code",
+                                                                                       "enum": [0, 1, 4, 5, 6, 7, 11, 50, 1000]
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               },
+                               "parameters": [
+                                       {
+                                               "name": "jobids",
+                                               "in": "query",
+                                               "description": "Job identifiers",
+                                               "required": true,
+                                               "schema": {
+                                                       "type": "string"
+                                               }
                                        }
                                ]
                        }