From: Marcin Haba Date: Wed, 29 Mar 2023 13:50:26 +0000 (+0200) Subject: baculum: Add endpoint to list m365 jobs by email X-Git-Tag: Release-13.0.3~97 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7c50e3e436a38b9cb8719c0bc31057ea3890f49f;p=thirdparty%2Fbacula.git baculum: Add endpoint to list m365 jobs by email --- diff --git a/gui/baculum/protected/API/Modules/PluginM365Manager.php b/gui/baculum/protected/API/Modules/PluginM365Manager.php new file mode 100644 index 000000000..a6aaa773b --- /dev/null +++ b/gui/baculum/protected/API/Modules/PluginM365Manager.php @@ -0,0 +1,91 @@ + + * @category Module + * @package Baculum API + */ +class PluginM365Manager extends APIModule { + + /** + * Get Microsoft 365 jobs by tenant and email owner. + * + * @param string $tenant tenant name + * @param string $owner email owner + * @param array $criteria SQL criteria + * @return array job list or empty array if no job found + */ + public function getJobsByTenantAndOwner($tenant, $owner, $criteria = []) { + $params = count($criteria) > 0 ? $criteria : []; + + // email owner + $params['MetaEmail.EmailOwner'] = []; + $params['MetaEmail.EmailOwner'][] = [ + 'operator' => 'AND', + 'vals' => [$owner] + ]; + + // email tenant + $params['MetaEmail.EmailTenant'] = []; + $params['MetaEmail.EmailTenant'][] = [ + 'operator' => 'AND', + 'vals' => [$tenant] + ]; + + // job types + $params['Job.Type'] = []; + $params['Job.Type'][] = [ + 'operator' => 'IN', + 'vals' => ['B'] + ]; + + // job status + $params['Job.JobStatus'] = []; + $params['Job.JobStatus'][] = [ + 'operator' => 'IN', + 'vals' => ['T', 'f', 'E', 'A', 'e'] + ]; + + $where = Database::getWhere($params); + + $sql = 'SELECT DISTINCT Job.JobId AS jobid, + Job.Name AS name, + Job.StartTime AS starttime, + Job.EndTime AS endtime, + Job.JobFiles AS jobfiles, + Job.JobBytes AS jobbytes + FROM Job + LEFT JOIN MetaEmail USING (JobId) + ' . $where['where'] . ' + ORDER BY Job.EndTime DESC'; + + $statement = Database::runQuery($sql, $where['params']); + return $statement->fetchAll(PDO::FETCH_OBJ); + } +} diff --git a/gui/baculum/protected/API/Pages/API/PluginM365EmailJobList.php b/gui/baculum/protected/API/Pages/API/PluginM365EmailJobList.php new file mode 100644 index 000000000..3e9572f3a --- /dev/null +++ b/gui/baculum/protected/API/Pages/API/PluginM365EmailJobList.php @@ -0,0 +1,99 @@ + + * @category API + * @package Baculum API + */ +class PluginM365EmailJobList extends BaculumAPIServer { + + public function get() { + $misc = $this->getModule('misc'); + $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 = PluginError::MSG_ERROR_WRONG_EXITCODE; + $this->error = PluginError::ERROR_WRONG_EXITCODE; + return; + } + $tenantid = $this->Request->contains('tenantid') ? $this->Request['tenantid'] : ''; + $fd_plugin_cfg = $this->getModule('fd_plugin_cfg')->getConfig('m365', $client, $tenantid); + if (!empty($tenantid) && (!$misc->isValidUUID($tenantid) || count($fd_plugin_cfg) == 0)) { + $this->output = PluginM365Error::MSG_ERROR_TENANT_DOES_NOT_EXISTS; + $this->error = PluginM365Error::ERROR_TENANT_DOES_NOT_EXISTS; + return; + } + $tenant = $fd_plugin_cfg['tenant_name']; + $emailowner = $this->Request->contains('emailowner') && $misc->isValidNameExt($this->Request['emailowner']) ? $this->Request['emailowner'] : ''; + if (empty($emailowner)) { + $this->output = PluginM365Error::MSG_ERROR_EMAIL_DOES_NOT_EXISTS; + $this->error = PluginM365Error::ERROR_EMAIL_DOES_NOT_EXISTS; + return; + } + + $result = $this->getModule('bconsole')->bconsoleCommand( + $this->director, + ['.jobs'], + null, + true + ); + if ($result->exitcode === 0) { + $params['Job.Name'] = []; + $params['Job.Name'][] = [ + 'operator' => 'OR', + 'vals' => $result->output + ]; + $result = $this->getModule('m365')->getJobsByTenantAndOwner( + $tenant, + $emailowner, + $params + ); + $this->output = $result; + $this->error = PluginM365Error::ERROR_NO_ERRORS; + } else { + $this->output = PluginM365Error::MSG_ERROR_WRONG_EXITCODE . ', Error => ' . $result->exitcode . ' Output => ' . implode(PHP_EOL, $result->output); + $this->error = PluginM365Error::ERROR_WRONG_EXITCODE; + } + } +} diff --git a/gui/baculum/protected/API/Pages/API/config.xml b/gui/baculum/protected/API/Pages/API/config.xml index bc367f080..ffdfe6c3c 100644 --- a/gui/baculum/protected/API/Pages/API/config.xml +++ b/gui/baculum/protected/API/Pages/API/config.xml @@ -59,5 +59,7 @@ + + diff --git a/gui/baculum/protected/API/Pages/API/endpoints.xml b/gui/baculum/protected/API/Pages/API/endpoints.xml index 32cd41602..82400e8bf 100644 --- a/gui/baculum/protected/API/Pages/API/endpoints.xml +++ b/gui/baculum/protected/API/Pages/API/endpoints.xml @@ -141,6 +141,7 @@ + diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 2796d0db6..3101901be 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -8589,6 +8589,81 @@ ] } }, + "/api/v2/plugins/m365/{clientid}/{tenantid}/jobs": { + "get": { + "tags": ["plugins"], + "summary": "Microsoft 365 plugin job list for specific email account.", + "description": "Get Microsoft 365 plugin job list for specific email account.", + "responses": { + "200": { + "description": "List of Microsoft 365 jobs for given email", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "output": { + "type": "array", + "items": { + "type": "object", + "properties": { + "jobid": { + "type": "integer", + "description": "Job identifier" + }, + "name": { + "type": "string", + "description": "Job name" + }, + "starttime": { + "type": "string", + "description": "Start time" + }, + "endtime": { + "type": "string", + "description": "Start time" + }, + "jobfiles": { + "type": "integer", + "description": "Job files" + }, + "jobbytes": { + "type": "integer", + "description": "Job bytes" + } + } + } + }, + "error": { + "type": "integer", + "description": "Error code", + "enum": [0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 160, 161, 1000] + } + } + } + } + } + } + }, + "parameters": [ + { + "$ref": "#/components/parameters/ClientId" + }, + { + "$ref": "#/components/parameters/TenantId" + }, + { + "name": "emailowner", + "in": "query", + "required": true, + "description": "Email to get jobs", + "schema": { + "type": "string" + } + } + ] + } + }, "/api/v2/plugins/vsphere/{clientid}/servers": { "get": { "tags": ["plugins"], diff --git a/gui/baculum/protected/Common/Modules/Errors/PluginM365Error.php b/gui/baculum/protected/Common/Modules/Errors/PluginM365Error.php index 9b3f1edf0..508f2afc8 100644 --- a/gui/baculum/protected/Common/Modules/Errors/PluginM365Error.php +++ b/gui/baculum/protected/Common/Modules/Errors/PluginM365Error.php @@ -31,6 +31,8 @@ namespace Baculum\Common\Modules\Errors; */ class PluginM365Error extends PluginError { const ERROR_TENANT_DOES_NOT_EXISTS = 160; + const ERROR_EMAIL_DOES_NOT_EXISTS = 161; const MSG_ERROR_TENANT_DOES_NOT_EXISTS = 'Tenant does not exist.'; + const MSG_ERROR_EMAIL_DOES_NOT_EXISTS = 'Email does not exist.'; }