]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add VMware vSphere restore host list endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Fri, 10 Mar 2023 12:20:41 +0000 (13:20 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Sat, 11 Mar 2023 21:32:26 +0000 (22:32 +0100)
gui/baculum/protected/API/Modules/ConsoleOutputQueryPage.php
gui/baculum/protected/API/Pages/API/PluginVSphereListRestoreHosts.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/endpoints.xml
gui/baculum/protected/API/openapi_baculum.json

index 0c014532369786155423e90a96250fc909ddcd52..c32f174ab3ef06971610437c0a4b2137391f6022 100644 (file)
@@ -61,5 +61,22 @@ abstract class ConsoleOutputQueryPage extends ConsoleOutputPage {
                }
                return $ret;
        }
+
+       /**
+        * Analyze the query command output for errors.
+        *
+        * @param array $output query command raw output
+        * @return bool true if error found, otherwise false
+        */
+       protected function isError(array $output) {
+               $error = false;
+               while (count($output) > 0 && preg_match('/^[\w\d]+=/', $output[0]) === 0  && strpos($output[0], '[DE') === false) {
+                       array_shift($output);
+               }
+               if (count($output) > 0 && (stripos($output[0], 'error') !== false || strpos($output[0], '[DE') !== false)) {
+                       $error = true;
+               }
+               return $error;
+       }
 }
 ?>
diff --git a/gui/baculum/protected/API/Pages/API/PluginVSphereListRestoreHosts.php b/gui/baculum/protected/API/Pages/API/PluginVSphereListRestoreHosts.php
new file mode 100644 (file)
index 0000000..f386993
--- /dev/null
@@ -0,0 +1,155 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2023 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\ConsoleOutputQueryPage;
+use Baculum\Common\Modules\Logging;
+use Baculum\Common\Modules\Errors\BconsoleError;
+use Baculum\Common\Modules\Errors\ClientError;
+use Baculum\Common\Modules\Errors\PluginVSphereError;
+
+/**
+ * List vSphere plugin restore hosts.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category API
+ * @package Baculum API
+ */
+class PluginVSphereListRestoreHosts extends ConsoleOutputQueryPage {
+
+       public function get() {
+               $client = null;
+               $clientid = $this->Request->contains('id') ? (int)$this->Request['id'] : 0;
+               $result = $this->getModule('bconsole')->bconsoleCommand(
+                       $this->director,
+                       ['.client'],
+                       null,
+                       true
+               );
+               if ($result->exitcode === 0) {
+                       $client_val = $this->getModule('client')->getClientById($clientid);
+                       if (is_object($client_val) && in_array($client_val->name, $result->output)) {
+                               $client = $client_val->name;
+                       } else {
+                               $this->output = ClientError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS;
+                               $this->error = ClientError::ERROR_CLIENT_DOES_NOT_EXISTS;
+                               return;
+                       }
+               } else {
+                       $this->output = PluginVSphereError::MSG_ERROR_WRONG_EXITCODE;
+                       $this->error = PluginVSphereError::ERROR_WRONG_EXITCODE;
+                       return;
+               }
+
+               $misc = $this->getModule('misc');
+               $out_format = ConsoleOutputPage::OUTPUT_FORMAT_RAW;
+               if ($this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output'])) {
+                       $out_format = $this->Request['output'];
+               }
+               $server = $this->Request->contains('server') && $misc->isValidName($this->Request['server'])? $this->Request['server'] : '';
+
+               $plugin = 'vsphere:';
+               if (!empty($server)) {
+                       $plugin .= ' server="' . $server . '"';
+               }
+               $out = new \StdClass;
+               $out->output = [];
+               $params = ['client' => $client, 'plugin' => $plugin];
+               if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) {
+                       $out = $this->getRawOutput($params);
+               } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) {
+                       $out = $this->getJSONOutput($params);
+               }
+
+               if ($out->exitcode !== 0) {
+                       $out->error = PluginVSphereError::ERROR_EXECUTING_PLUGIN_QUERY_COMMAND;
+                       $out->output = PluginVSphereError::MSG_ERROR_EXECUTING_PLUGIN_QUERY_COMMAND . implode(PHP_EOL, $out->output);
+                       $this->getModule('logging')->log(
+                               Logging::CATEGORY_EXECUTE,
+                               $out->output . ", Error={$out->error}"
+                       );
+               } else {
+                       $out->error = BconsoleError::ERROR_NO_ERRORS;
+               }
+               $this->output = $out->output;
+               $this->error = $out->error;
+       }
+
+       /**
+        * Get vSphere host list output from console in raw format.
+        *
+        * @param array $params command parameters
+        * @return StdClass object with output and exitcode
+        */
+       protected function getRawOutput($params = []) {
+               $ret = $this->getModule('bconsole')->bconsoleCommand(
+                       $this->director,
+                       [
+                               '.query',
+                               'plugin="' . $params['plugin'] . '"',
+                               'client="' . $params['client'] . '"',
+                               'parameter="restore_host"'
+                       ]
+               );
+               if ($ret->exitcode != 0) {
+                       $this->getModule('logging')->log(
+                               Logging::CATEGORY_EXECUTE,
+                               'Wrong output from vSphere RAW restore host list: ' . implode(PHP_EOL, $ret->output)
+                       );
+                       $ret->output = []; // don't provide errors to output, only in logs
+               } elseif ($this->isError($ret->output)) {
+                       $ret->exitcode = PluginVSphereError::ERROR_EXECUTING_PLUGIN_QUERY_COMMAND;
+               }
+               return $ret;
+       }
+
+       /**
+        * Get vSphere restore host list output from console in JSON format.
+        *
+        * @param array $params command parameters
+        * @return StdClass object with output and exitcode
+        */
+       protected function getJSONOutput($params = []) {
+               $result = $this->getRawOutput($params);
+               if ($result->exitcode === 0) {
+                       $rows = $this->getRestoreHostRows($result->output);
+                       $result->output = $this->parseOutputKeyValue($rows);
+               }
+               return $result;
+       }
+
+       /**
+        * Filter rows with restore host items.
+        *
+        * @param array $output dot query command output
+        * @return array
+        */
+       private function getRestoreHostRows(array $output) {
+               $out = [];
+               for ($i = 0; $i < count($output); $i++) {
+                       if (preg_match('/^restore_host=/', $output[$i]) === 1) {
+                               $out[] = $output[$i];
+                       }
+               }
+               return $out;
+       }
+}
index 9e50080d5dc81a4cdf60a50992dac9b0cbfd88ea..e9c6055bf1352f36ff0f5454ad0ba7faaf6cae65 100644 (file)
        <url ServiceParameter="PluginVSphereListServers" pattern="api/v2/plugins/vsphere/{id}/servers" parameters.id="\d+" />
        <url ServiceParameter="PluginVSphereListHosts" pattern="api/v2/plugins/vsphere/{id}/hosts" parameters.id="\d+" />
        <url ServiceParameter="PluginVSphereListDatastores" pattern="api/v2/plugins/vsphere/{id}/datastores" parameters.id="\d+" />
+       <url ServiceParameter="PluginVSphereListRestoreHosts" pattern="api/v2/plugins/vsphere/{id}/restore-hosts" parameters.id="\d+" />
 
 
        <!-- OLD API v1 -->
index 4f27230b4f865daf88ca6353dca5d0dc86fc6e3e..e1c3685c37814e750921aed75b3ee8b3df0ff10e 100644 (file)
                                        },
                                        {
                                                "$ref": "#/components/parameters/Output"
+                                       },
+                                       {
+                                               "name": "server",
+                                               "in": "query",
+                                               "required": true,
+                                               "description": "VMware server name",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
                                        }
                                ]
                        }
                                ]
                        }
                },
+               "/api/v2/plugins/vsphere/{clientid}/restore-hosts": {
+                       "get": {
+                               "tags": ["plugins"],
+                               "summary": "VMware vSphere restore host list",
+                               "description": "VMware vSphere restore host list.",
+                               "responses": {
+                                       "200": {
+                                               "description": "List of VMware vSphere restore hosts",
+                                               "content": {
+                                                       "application/json": {
+                                                               "schema": {
+                                                                       "type": "object",
+                                                                       "properties": {
+                                                                               "output": {
+                                                                                       "type": "array",
+                                                                                       "items": {
+                                                                                               "type": "object",
+                                                                                               "properties": {
+                                                                                                       "restore_host": {
+                                                                                                               "description": "VMware vSphere restore host",
+                                                                                                               "type": "string"
+                                                                                                       }
+                                                                                               }
+                                                                                       }
+                                                                               },
+                                                                               "error": {
+                                                                                       "type": "integer",
+                                                                                       "description": "Error code",
+                                                                                       "enum": [0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 150, 1000]
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               },
+                               "parameters": [
+                                       {
+                                               "$ref": "#/components/parameters/ClientId"
+                                       },
+                                       {
+                                               "$ref": "#/components/parameters/Output"
+                                       },
+                                       {
+                                               "name": "server",
+                                               "in": "query",
+                                               "required": true,
+                                               "description": "VMware server name",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       }
+                               ]
+                       }
+               },
                "/api/v2/search": {
                        "get": {
                                "tags": ["tools"],