]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add endpoint to create directory on storage daemon host
authorMarcin Haba <marcin.haba@bacula.pl>
Fri, 10 Nov 2023 12:30:20 +0000 (13:30 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Fri, 10 Nov 2023 12:30:20 +0000 (13:30 +0100)
gui/baculum/protected/API/Pages/API/PluginCoreDirCreate.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/endpoints.xml
gui/baculum/protected/API/openapi_baculum.json

diff --git a/gui/baculum/protected/API/Pages/API/PluginCoreDirCreate.php b/gui/baculum/protected/API/Pages/API/PluginCoreDirCreate.php
new file mode 100644 (file)
index 0000000..187c8c4
--- /dev/null
@@ -0,0 +1,161 @@
+<?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\PluginError;
+use Baculum\Common\Modules\Errors\StorageError;
+
+/**
+ * Create directory on host with Bacula component.
+ * So far creating directory is possible to do for storage daemon host only.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category API
+ * @package Baculum API
+ */
+class PluginCoreDirCreate extends ConsoleOutputQueryPage {
+
+       public function get() {
+               $misc = $this->getModule('misc');
+               $storageid = $this->Request->contains('id') && $misc->isValidInteger($this->Request['id']) ? (int)$this->Request['id'] : 0;
+               $path = $this->Request->contains('path') && $misc->isValidPath($this->Request['path']) ? $this->Request['path'] : '';
+
+               if (empty($path)) {
+                       $this->output = PluginError::MSG_ERROR_INVALID_PATH;
+                       $this->error = PluginError::ERROR_INVALID_PATH;
+                       return;
+               }
+
+               $result = $this->getModule('bconsole')->bconsoleCommand(
+                       $this->director,
+                       ['.storage'],
+                       null,
+                       true
+               );
+               if ($result->exitcode === 0) {
+                       $storage_val = $this->getModule('storage')->getStorageById($storageid);
+                       if (is_object($storage_val) && in_array($storage_val->name, $result->output)) {
+                               $storage = $storage_val->name;
+                       } else {
+                               $this->output = StorageError::MSG_ERROR_STORAGE_DOES_NOT_EXISTS;
+                               $this->error = StorageError::ERROR_STORAGE_DOES_NOT_EXISTS;
+                               return;
+                       }
+               } else {
+                       $this->output = PluginError::MSG_ERROR_WRONG_EXITCODE;
+                       $this->error = PluginError::ERROR_WRONG_EXITCODE;
+                       return;
+               }
+               $params = [
+                       'storage' => $storage,
+                       'path' => $path
+               ];
+
+
+               $out_format = ConsoleOutputPage::OUTPUT_FORMAT_RAW;
+               if ($this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output'])) {
+                       $out_format = $this->Request['output'];
+               }
+
+               $out = new \StdClass;
+               $out->output = [];
+               if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) {
+                       $out = $this->getRawOutput($params);
+               } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) {
+                       $out = $this->getJSONOutput($params);
+               }
+               $this->output = $out->output;
+               if ($out->exitcode != PluginError::ERROR_EXECUTING_PLUGIN_QUERY_COMMAND) {
+                       if ($out->exitcode != 0) {
+                               $this->error = PluginError::ERROR_WRONG_EXITCODE;
+                               $this->output = PluginError::MSG_ERROR_WRONG_EXITCODE;
+                       } else {
+                               $this->error = PluginError::ERROR_NO_ERRORS;
+                       }
+               }
+       }
+
+       /**
+        * Get dircreate command output 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="core: dirname=\"' . $params['path'] . '\""',
+                               'storage="' . $params['storage'] . '"',
+                               'parameter="dircreate"'
+                       ],
+                       null,
+                       true
+               );
+               if ($ret->exitcode != 0) {
+                       $this->getModule('logging')->log(
+                               Logging::CATEGORY_EXECUTE,
+                               'Wrong output from RAW .query dircreate: ' . implode(PHP_EOL, $ret->output)
+                       );
+                       $ret->output = []; // don't provide errors to output, only in logs
+               } elseif ($this->isError($ret->output)) {
+                       $ret->exitcode = PluginError::ERROR_EXECUTING_PLUGIN_QUERY_COMMAND;
+               }
+               return $ret;
+       }
+
+       /**
+        * Get query dircreate command output 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) {
+                       $result->output = $this->getItemRow($result->output);
+               }
+               return $result;
+       }
+       /**
+        * Parse and get row with item from output.
+        *
+        * @param array $output dot query command output
+        * @return array parsed output
+        */
+       private function getItemRow(array $output) {
+               $out = [];
+               $pattern = '/^dirname=(?P<path>.+)$/i';
+               for ($i = 0; $i < count($output); $i++) {
+                       if (preg_match($pattern, $output[$i], $match) === 1) {
+                               $out = [
+                                       'path' => $match['path']
+                               ];
+                               break;
+                       }
+               }
+               return $out;
+       }
+}
index 020c175351a07612d70a0afb0a0d83810d237be9..9943b5f4855f45423f44eae2d7c7da293646bd96 100644 (file)
        <!-- M365 Plugin -->
        <url ServiceParameter="PluginCoreDirFileList" pattern="api/v2/plugins/core/{id}/storage/ls/" parameters.id="\d+" />
        <url ServiceParameter="PluginCoreDiskPerf" pattern="api/v2/plugins/core/{id}/storage/diskperf/" parameters.id="\d+" />
+       <url ServiceParameter="PluginCoreDirCreate" pattern="api/v2/plugins/core/{id}/storage/dircreate/" parameters.id="\d+" />
        <url ServiceParameter="PluginM365ListLoggedUsers" pattern="api/v2/plugins/m365/{id}/users/" parameters.id="\d+" />
        <url ServiceParameter="PluginM365ListLoggedUsers" pattern="api/v2/plugins/m365/{id}/{tenantid}/users/" parameters.id="\d+" parameters.tenantid="[a-zA-Z0-9:.\-_ ]+" />
        <url ServiceParameter="PluginM365EmailList" pattern="api/v2/plugins/m365/{id}/{tenantid}/emails/" parameters.id="\d+" parameters.tenantid="[a-zA-Z0-9:.\-_ ]+" />
index 40d1a2c9c45053238720ce2e77facc4a5e78cbec..c36297ef77bb94fe2095698f7d9f836909388bcb 100644 (file)
                                ]
                        }
                },
+               "/api/v2/plugins/core/{storageid}/storage/dircreate": {
+                       "get": {
+                               "tags": ["plugins"],
+                               "summary": "Create directory on the storage daemon host",
+                               "description": "Create directory on the storage daemon host",
+                               "responses": {
+                                       "200": {
+                                               "description": "Create directory on the storage daemon host",
+                                               "content": {
+                                                       "application/json": {
+                                                               "schema": {
+                                                                       "type": "object",
+                                                                       "properties": {
+                                                                               "output": {
+                                                                                       "type": "array",
+                                                                                       "items": {
+                                                                                               "description": "Create directory result",
+                                                                                               "type": "string"
+                                                                                       }
+                                                                               },
+                                                                               "error": {
+                                                                                       "type": "integer",
+                                                                                       "description": "Error code",
+                                                                                       "enum": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 20, 150, 1000]
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               },
+                               "parameters": [
+                                       {
+                                               "$ref": "#/components/parameters/StorageId"
+                                       },
+                                       {
+                                               "$ref": "#/components/parameters/Output"
+                                       }
+                               ]
+                       }
+               },
                "/api/v2/plugins/m365/{clientid}/tenants": {
                        "get": {
                                "tags": ["plugins"],