From: Marcin Haba Date: Tue, 30 May 2023 06:26:34 +0000 (+0200) Subject: baculum: Add director time endpoint X-Git-Tag: Release-13.0.4~87 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7a82cd0e2f847fcf75e6832d5230038a585b73ac;p=thirdparty%2Fbacula.git baculum: Add director time endpoint --- diff --git a/gui/baculum/protected/API/Modules/Bconsole.php b/gui/baculum/protected/API/Modules/Bconsole.php index b067c59dc..acfa03ef4 100644 --- a/gui/baculum/protected/API/Modules/Bconsole.php +++ b/gui/baculum/protected/API/Modules/Bconsole.php @@ -102,7 +102,8 @@ class Bconsole extends APIModule { '.jlist', '.search', '@putfile', - 'cloud' + 'cloud', + 'time' ); private $config; diff --git a/gui/baculum/protected/API/Modules/TimeManager.php b/gui/baculum/protected/API/Modules/TimeManager.php new file mode 100644 index 000000000..50885d27e --- /dev/null +++ b/gui/baculum/protected/API/Modules/TimeManager.php @@ -0,0 +1,64 @@ + + * @category Module + * @package Baculum API + */ +class TimeManager extends APIModule { + + /** + * Director time format. + * Example: '29-May-2023 09:01:34'. + */ + const DIRECTOR_TIME_FORMAT = 'j-M-Y h:i:s'; + + /** + * Parse time returned by Director time command. + * It is in form: '29-May-2023 09:01:34' + * @TODO: Make sure that 'May' is always returned in English + * + * @param string date/time string from the Director + * @return array split date and time single values or empty array if error or warning happens + */ + public function parseDirectorTime($time) { + list($dow, $date_time) = explode(' ', $time, 2); + $tres = date_parse_from_format(self::DIRECTOR_TIME_FORMAT, $date_time); + $ret = []; + if ($tres['warning_count'] === 0 && $tres['error_count'] === 0) { + $ret = [ + 'year' => $tres['year'], + 'month' => $tres['month'], + 'day' => $tres['day'], + 'hour' => $tres['hour'], + 'minute' => $tres['minute'], + 'second' => $tres['second'] + ]; + } + return $ret; + } +} diff --git a/gui/baculum/protected/API/Pages/API/DirectorTime.php b/gui/baculum/protected/API/Pages/API/DirectorTime.php new file mode 100644 index 000000000..5d119351b --- /dev/null +++ b/gui/baculum/protected/API/Pages/API/DirectorTime.php @@ -0,0 +1,77 @@ + + * @category API + * @package Baculum API + */ +class DirectorTime extends BaculumAPIServer { + + public function get() { + $misc = $this->getModule('misc'); + $director = $this->Request->contains('name') && $misc->isValidName($this->Request['name']) ? $this->Request['name'] : null; + + $dirs = []; + $result = $this->getModule('bconsole')->getDirectors(); + if ($result->exitcode === 0) { + $dirs = $result->output; + } + + if (is_null($director) || !in_array($director, $dirs)) { + // Invalid director + $this->output = BconsoleError::MSG_ERROR_INVALID_DIRECTOR; + $this->error = BconsoleError::ERROR_INVALID_DIRECTOR; + return; + } + + $result = $this->getModule('bconsole')->bconsoleCommand( + $director, + ['time'], + null, + true + ); + if ($result->exitcode === 0) { + $result->output = array_filter($result->output); + if (count($result->output) == 1) { + $this->output = $this->getModule('time')->parseDirectorTime($result->output[0]); + if (count($this->output) > 0) { + $this->error = TimeError::ERROR_NO_ERRORS; + } else { + $this->output = TimeError::MSG_ERROR_INVALID_DATE_TIME . ' Output=>' . $result->output[0]; + $this->error = TimeError::ERROR_INVALID_DATE_TIME; + } + } else { + $this->output = TimeError::MSG_ERROR_INVALID_DATE_TIME . ' Output=>' . implode('', $result->output); + $this->error = TimeError::ERROR_INVALID_DATE_TIME; + } + } else { + $this->output = TimeError::MSG_ERROR_WRONG_EXITCODE . ' Exitcode=' . $result->exitcode; + $this->error = TimeError::ERROR_WRONG_EXITCODE; + } + } +} diff --git a/gui/baculum/protected/API/Pages/API/config.xml b/gui/baculum/protected/API/Pages/API/config.xml index ffdfe6c3c..a7865a57c 100644 --- a/gui/baculum/protected/API/Pages/API/config.xml +++ b/gui/baculum/protected/API/Pages/API/config.xml @@ -55,6 +55,7 @@ + diff --git a/gui/baculum/protected/API/Pages/API/endpoints.xml b/gui/baculum/protected/API/Pages/API/endpoints.xml index 841fecf70..e923977d0 100644 --- a/gui/baculum/protected/API/Pages/API/endpoints.xml +++ b/gui/baculum/protected/API/Pages/API/endpoints.xml @@ -129,6 +129,7 @@ + diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index e0143e675..44ece1565 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -7106,6 +7106,72 @@ ] } }, + "/api/v2/directors/{director_name}/time": { + "get": { + "tags": ["directors"], + "summary": "Get director system time", + "description": "Get director system time", + "responses": { + "200": { + "description": "Director system time", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "output": { + "type": "object", + "properties": { + "year": { + "description": "Four digit year", + "type": "integer" + }, + "month": { + "description": "Month (1-12)", + "type": "integer" + }, + "day": { + "description": "Day (1-31)", + "type": "integer" + }, + "hour": { + "description": "Hour (0-23)", + "type": "integer" + }, + "minute": { + "description": "Minute (0-59)", + "type": "integer" + }, + "second": { + "description": "Second (0-59)", + "type": "integer" + } + } + }, + "error": { + "type": "integer", + "description": "Error code", + "enum": [0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 530, 1000] + } + } + } + } + } + } + }, + "parameters": [ + { + "name": "director_name", + "in": "path", + "description": "Director name", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, "/api/v2/oauth2/clients": { "get": { "tags": ["oauth2"], diff --git a/gui/baculum/protected/Common/Modules/Errors/TimeError.php b/gui/baculum/protected/Common/Modules/Errors/TimeError.php new file mode 100644 index 000000000..9dbf706e9 --- /dev/null +++ b/gui/baculum/protected/Common/Modules/Errors/TimeError.php @@ -0,0 +1,36 @@ + + * @category Errors + * @package Baculum Common + */ +class TimeError extends GenericError { + const ERROR_INVALID_DATE_TIME = 530; + + const MSG_ERROR_INVALID_DATE_TIME = 'Invalid date/time value.'; +}