From: Marcin Haba Date: Tue, 18 Oct 2022 13:45:18 +0000 (+0200) Subject: baculum: Add restore plugin option fields endpoint X-Git-Tag: Release-13.0.2~61 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5fcbdcc089221a3c41dc56f318607916af1b40c9;p=thirdparty%2Fbacula.git baculum: Add restore plugin option fields endpoint --- diff --git a/gui/baculum/protected/API/Modules/ConsoleOutputLlistPage.php b/gui/baculum/protected/API/Modules/ConsoleOutputLlistPage.php index eda9d212f..b848dbb8a 100644 --- a/gui/baculum/protected/API/Modules/ConsoleOutputLlistPage.php +++ b/gui/baculum/protected/API/Modules/ConsoleOutputLlistPage.php @@ -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\w+)="?(?P.*?)"?$/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 index 000000000..72290c687 --- /dev/null +++ b/gui/baculum/protected/API/Pages/API/LlistPluginRestoreConfFields.php @@ -0,0 +1,107 @@ + + * @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; + } +} diff --git a/gui/baculum/protected/API/Pages/API/endpoints.xml b/gui/baculum/protected/API/Pages/API/endpoints.xml index 83cb532c1..ae505ad03 100644 --- a/gui/baculum/protected/API/Pages/API/endpoints.xml +++ b/gui/baculum/protected/API/Pages/API/endpoints.xml @@ -78,6 +78,7 @@ + diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index f0a3dd73d..e4c6db3b7 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -2066,6 +2066,65 @@ "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" } ] } diff --git a/gui/baculum/protected/Common/Modules/Errors/PluginError.php b/gui/baculum/protected/Common/Modules/Errors/PluginError.php index 480bf6acb..60e2e3ca6 100644 --- a/gui/baculum/protected/Common/Modules/Errors/PluginError.php +++ b/gui/baculum/protected/Common/Modules/Errors/PluginError.php @@ -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.'; }