]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add module for delete command
authorMarcin Haba <marcin.haba@bacula.pl>
Fri, 15 Sep 2023 08:20:12 +0000 (10:20 +0200)
committerEric Bollengier <eric@baculasystems.com>
Tue, 31 Oct 2023 15:00:30 +0000 (16:00 +0100)
gui/baculum/protected/API/Modules/Delete.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/config.xml

diff --git a/gui/baculum/protected/API/Modules/Delete.php b/gui/baculum/protected/API/Modules/Delete.php
new file mode 100644 (file)
index 0000000..b990224
--- /dev/null
@@ -0,0 +1,136 @@
+<?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.
+ */
+
+
+namespace Baculum\API\Modules;
+
+use Baculum\Common\Modules\Errors\BconsoleError;
+use Baculum\Common\Modules\Errors\JobError;
+
+/**
+ * Delete command module.
+ * The delete command removes records from the catalog.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category Module
+ * @package Baculum API
+ */
+class Delete extends APIModule {
+
+       /**
+        * Bacula catalog resource types.
+        */
+       const TYPE_VOLUME = 'volume';
+       const TYPE_POOL = 'pool';
+       const TYPE_JOBID = 'jobid';
+       const TYPE_SNAPSHOT = 'snapshot'; // not supported yet
+       const TYPE_CLIENT = 'client';
+       const TYPE_TAG = 'tag';           // not supported yet
+       const TYPE_OBJECT = 'object';
+
+       /**
+        * Delete Bacula resources from the catalog.
+        *
+        * @param string $director director name
+        * @param string $type resource type (client, pool, object...etc.)
+        * @param string $value resource identify value to delete
+        * @return array output and error code
+        */
+       public function delete($director, $type, $value) {
+               $result = [
+                       'output' => [],
+                       'error' => BconsoleError::ERROR_NO_ERRORS
+               ];
+               $types = $this->getTypes();
+               if (!in_array($type, $types)) {
+                       $result['output'] = BconsoleError::MSG_ERROR_INVALID_PROPERTY;
+                       $result['error'] = BconsoleError::ERROR_INVALID_PROPERTY;
+               } else {
+                       $cmd = ['delete'];
+                       switch ($type) {
+                               case self::TYPE_JOBID: {
+                                       $jobid = (int)$value;
+                                       $ret = $this->getModule('bconsole')->bconsoleCommand(
+                                               $director,
+                                               ['.jobs'],
+                                               null,
+                                               true
+                                       );
+                                       $job = $this->getModule('job')->getJobById($jobid);
+                                       if ($ret->exitcode === 0) {
+                                               if (is_object($job) && in_array($job->name, $ret->output)) {
+                                                       $cmd[] = "{$type}=\"{$value}\"";
+                                               } else {
+                                                       $result['output'] = JobError::MSG_ERROR_JOB_DOES_NOT_EXISTS;
+                                                       $result['error'] = JobError::ERROR_JOB_DOES_NOT_EXISTS;
+                                               }
+                                       } else {
+                                               $result['output'] = $ret->output;
+                                               $result['error'] = $ret->exitcode;
+                                       }
+                                       break;
+                               }
+                               case self::TYPE_VOLUME:
+                               case self::TYPE_POOL:
+                               case self::TYPE_CLIENT: {
+                                       $cmd[] = "{$type}=\"{$value}\"";
+                                       break;
+                               }
+                               case self::TYPE_OBJECT: {
+                                       $cmd[] = "{$type} objectid=\"{$value}\"";
+                                       break;
+                               }
+                               /** Deleting snapshots and tags is not supported by the API so far. **/
+                               /*case self::TYPE_SNAPSHOT:
+                               case self::TYPE_TAG: {
+                                       break;
+                               }*/
+                       }
+                       if (count($cmd) > 1 && $result['error'] === 0) {
+                               $cmd[] = 'yes';
+                               $ret = $this->getModule('bconsole')->bconsoleCommand(
+                                       $director,
+                                       $cmd
+                               );
+                               $result['output'] = $ret->output;
+                               $result['error'] = $ret->exitcode;
+                       } else {
+                               $result['output'] = BconsoleError::MSG_ERROR_INVALID_PROPERTY;
+                               $result['error'] = BconsoleError::ERROR_INVALID_PROPERTY;
+                       }
+               }
+               return $result;
+       }
+
+       public function getTypes() {
+               return [
+                       self::TYPE_VOLUME,
+                       self::TYPE_POOL,
+                       self::TYPE_JOBID,
+                       self::TYPE_SNAPSHOT,
+                       self::TYPE_CLIENT,
+                       self::TYPE_TAG,
+                       self::TYPE_OBJECT
+               ];
+       }
+}
+?>
index 3971e013650275b3c177dd20f27b4cc08d5fd766..a37b9b0eb55a787d0d44adb9e3bbcc51782810e6 100644 (file)
@@ -56,6 +56,7 @@
                <module id="comp_actions" class="Baculum\API\Modules\ComponentActions" />
                <!-- bconsole command modules -->
                <module id="ls" class="Baculum\API\Modules\Ls" />
+               <module id="delete" class="Baculum\API\Modules\Delete" />
                <module id="list" class="Baculum\API\Modules\BList" />
                <module id="time" class="Baculum\API\Modules\TimeManager" />
                <!-- changer command modules -->