]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add to jobs endpoint filtering by client and clientid
authorMarcin Haba <marcin.haba@bacula.pl>
Tue, 7 Aug 2018 19:27:35 +0000 (21:27 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Tue, 7 Aug 2018 19:28:43 +0000 (21:28 +0200)
gui/baculum/protected/API/Pages/API/Jobs.php
gui/baculum/protected/API/openapi_baculum.json

index d27e6651d56f0a4a3fa81404bcb24056821c69e8..f5960caea1d23091f3ed6f4824b2ca80c0ab9e47 100644 (file)
@@ -26,6 +26,18 @@ class Jobs extends BaculumAPIServer {
                $jobstatus = $this->Request->contains('jobstatus') ? $this->Request['jobstatus'] : '';
                $misc = $this->getModule('misc');
                $jobname = $this->Request->contains('name') && $misc->isValidName($this->Request['name']) ? $this->Request['name'] : '';
+               $clientid = $this->Request->contains('clientid') ? $this->Request['clientid'] : '';
+               if (!empty($clientid) && !$misc->isValidId($clientid)) {
+                       $this->output = JobError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS;
+                       $this->error = JobError::ERROR_CLIENT_DOES_NOT_EXISTS;
+                       return;
+               }
+               $client = $this->Request->contains('client') ? $this->Request['client'] : '';
+               if (!empty($client) && !$misc->isValidName($client)) {
+                       $this->output = JobError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS;
+                       $this->error = JobError::ERROR_CLIENT_DOES_NOT_EXISTS;
+                       return;
+               }
                $params = array();
                $jobstatuses = array_keys($misc->getJobState());
                $sts = str_split($jobstatus);
@@ -49,9 +61,39 @@ class Jobs extends BaculumAPIServer {
                        }
                        $params['name']['operator'] = 'OR';
                        $params['name']['vals'] = $vals;
-                       $jobs = $this->getModule('job')->getJobs($limit, $params);
-                       $this->output = $jobs;
-                       $this->error = JobError::ERROR_NO_ERRORS;
+
+                       $error = false;
+                       // Client name and clientid filter
+                       if (!empty($client) || !empty($clientid)) {
+                               $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.client'));
+                               if ($result->exitcode === 0) {
+                                       array_shift($result->output);
+                                       $cli = null;
+                                       if (!empty($client)) {
+                                               $cli = $this->getModule('client')->getClientByName($client);
+                                       } elseif (!empty($clientid)) {
+                                               $cli = $this->getModule('client')->getClientById($clientid);
+                                       }
+                                       if (is_object($cli) && in_array($cli->name, $result->output)) {
+                                               $params['clientid']['operator'] = 'AND';
+                                               $params['clientid']['vals'] = array($cli->clientid);
+                                       } else {
+                                               $error = true;
+                                               $this->output = JobError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS;
+                                               $this->error = JobError::ERROR_CLIENT_DOES_NOT_EXISTS;
+                                       }
+                               } else {
+                                       $error = true;
+                                       $this->output = $result->output;
+                                       $this->error = $result->exitcode;
+                               }
+                       }
+
+                       if ($error === false) {
+                               $jobs = $this->getModule('job')->getJobs($limit, $params);
+                               $this->output = $jobs;
+                               $this->error = JobError::ERROR_NO_ERRORS;
+                       }
                } else {
                        $this->output = $result->output;
                        $this->error = $result->exitcode;
index 9d3d79b91f2db1d0dc7a515e2a79b7f24c36e791..8e906f8f9a41112f0eb813c8d5f4af5f037dc7ac 100644 (file)
                                                                                "error": {
                                                                                        "type": "integer",
                                                                                        "description": "Error code",
-                                                                                       "enum": [0, 1, 2, 3, 4, 5, 6, 7, 1000]
+                                                                                       "enum": [0, 1, 2, 3, 4, 5, 6, 7, 53, 1000]
                                                                                }
                                                                        }
                                                                }
                                                "schema": {
                                                        "type": "string"
                                                }
+                                       },
+                                       {
+                                               "name": "clientid",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "Client identifier",
+                                               "schema": {
+                                                       "type": "integer"
+                                               }
+                                       },
+                                       {
+                                               "name": "client",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "Client name",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
                                        }
                                ]
                        }