]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add director show command API endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Sun, 4 Jul 2021 19:05:00 +0000 (21:05 +0200)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:03:25 +0000 (09:03 +0100)
gui/baculum/protected/API/Pages/API/DirectorShow.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/endpoints.xml

diff --git a/gui/baculum/protected/API/Pages/API/DirectorShow.php b/gui/baculum/protected/API/Pages/API/DirectorShow.php
new file mode 100644 (file)
index 0000000..70cd305
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2021 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.
+ */
+
+Prado::using('Application.API.Class.ConsoleOutputPage');
+Prado::using('Application.API.Class.ConsoleOutputShowPage');
+
+/**
+ * Director show command endpoint.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category API
+ * @package Baculum API
+ */
+class DirectorShow extends ConsoleOutputShowPage {
+
+       public function get() {
+               $director = $this->Request->contains('name') && $this->getModule('misc')->isValidName($this->Request['name']) ? $this->Request['name'] : null;
+               $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : ConsoleOutputPage::OUTPUT_FORMAT_RAW;
+               $dirs = [];
+               $result = $this->getModule('bconsole')->getDirectors();
+               if ($result->exitcode === 0) {
+                       $dirs = $result->output;
+               }
+
+               if (is_string($director) && in_array($director, $dirs)) {
+                       $out = (object)[
+                               'output' => [],
+                               'exitcode' => 0
+                       ];
+                       if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) {
+                               $out = $this->getRawOutput(['director' => $director]);
+                       } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) {
+                               $out = $this->getJSONOutput(['director' => $director]);
+                       }
+                       $this->output = $out->output;
+                       $this->error = $out->exitcode;
+               } else {
+                       $this->output = BconsoleError::MSG_ERROR_INVALID_DIRECTOR;
+                       $this->error = BconsoleError::ERROR_INVALID_DIRECTOR;
+               }
+       }
+
+       /**
+        * Get show director 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(
+                       $params['director'],
+                       ['show', 'director']
+               );
+       }
+
+       /**
+        * Get show director 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 5f3bbc43376e9845b6517f168323abff5b7ccda9..6e54bf0569beedf4d0655e521ac98bd7c24b0d23 100644 (file)
        <url ServiceParameter="Config" pattern="api/v2/config/{component_type}/{resource_type}/" parameters.component_type="[a-z]+" parameters.resource_type="[a-zA-Z]+" />
        <url ServiceParameter="Config" pattern="api/v2/config/{component_type}/{resource_type}/{resource_name}/" parameters.component_type="[a-z]+" parameters.resource_type="[a-zA-Z]+" parameters.resource_name="[a-zA-Z0-9:.\-_ ]+" />
        <!-- director endpoints -->
+       <url ServiceParameter="DirectorShow" pattern="api/v2/directors/{name}/show/" parameters.name="[a-zA-Z0-9:.\-_ ]+" />
        <url ServiceParameter="DirectorStatus" pattern="api/v2/directors/{name}/status/" parameters.name="[a-zA-Z0-9:.\-_ ]+" />
        <!-- actions endpoints -->
        <url ServiceParameter="Actions" pattern="api/v2/actions/{component}/{action}/" parameters.component="(director|storage|client)" parameters.action="(start|stop|restart)" />