From: Marcin Haba Date: Tue, 21 Feb 2023 12:26:45 +0000 (+0100) Subject: baculum: Add VMware vSphere datastore list endpoint X-Git-Tag: Release-13.0.3~130 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=267839654ee6e64fb205cbe6faddc2c359e07dae;p=thirdparty%2Fbacula.git baculum: Add VMware vSphere datastore list endpoint --- diff --git a/gui/baculum/protected/API/Pages/API/PluginVSphereListDatastores.php b/gui/baculum/protected/API/Pages/API/PluginVSphereListDatastores.php new file mode 100644 index 000000000..93ac01a2c --- /dev/null +++ b/gui/baculum/protected/API/Pages/API/PluginVSphereListDatastores.php @@ -0,0 +1,144 @@ + + * @category API + * @package Baculum API + */ +class PluginVSphereListDatastores 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; + } + + $out_format = ConsoleOutputPage::OUTPUT_FORMAT_RAW; + if ($this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output'])) { + $out_format = $this->Request['output']; + } + + $plugin = 'vsphere: '; + $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 . $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 datastore 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="datastore"' + ] + ); + if ($ret->exitcode != 0) { + $this->getModule('logging')->log( + Logging::CATEGORY_EXECUTE, + 'Wrong output from vSphere RAW datastore list: ' . implode(PHP_EOL, $ret->output) + ); + $ret->output = []; // don't provide errors to output, only in logs + } + return $ret; + } + + /** + * Get vSphere datastore 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 = iterator_to_array($this->getHostRows($result->output)); + $result->output = $this->parseOutputKeyValue($rows); + } + return $result; + } + + /** + * Filter rows with datastore items. + * + * @param array $output dot query command output + * @return none + */ + private function getHostRows(array $output) { + for ($i = 0; $i < count($output); $i++) { + if (preg_match('/^datastore=/', $output[$i]) === 1) { + yield $output[$i]; + } + } + } +} diff --git a/gui/baculum/protected/API/Pages/API/endpoints.xml b/gui/baculum/protected/API/Pages/API/endpoints.xml index 1ce620a04..29136cc8a 100644 --- a/gui/baculum/protected/API/Pages/API/endpoints.xml +++ b/gui/baculum/protected/API/Pages/API/endpoints.xml @@ -142,6 +142,7 @@ + diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index c1e2f488d..de36d1ca1 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -8218,6 +8218,9 @@ "parameters": [ { "$ref": "#/components/parameters/ClientId" + }, + { + "$ref": "#/components/parameters/Output" } ] } @@ -8265,6 +8268,59 @@ "parameters": [ { "$ref": "#/components/parameters/ClientId" + }, + { + "$ref": "#/components/parameters/Output" + } + ] + } + }, + "/api/v2/plugins/vsphere/{clientid}/datastores": { + "get": { + "tags": ["plugins"], + "summary": "VMware vSphere datastore list", + "description": "VMware vSphere datastore list.", + "responses": { + "200": { + "description": "List of VMware vSphere datastores", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "output": { + "type": "array", + "items": { + "type": "object", + "properties": { + "datastore": { + "description": "VMware vSphere datastores", + "type": "array", + "items": { + "description": "VMware vSphere datastores", + "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" } ] }