From 1288b6afe66ffc9e8c5421801a7f8ce456fc0e6c Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Fri, 3 Mar 2023 18:23:44 -0500 Subject: [PATCH] Add and remove schdules through api --- app/classes/shared/tasks.py | 1 + .../routes/api/servers/server/tasks/index.py | 90 ++++++++++++++++++- .../api/servers/server/tasks/task/index.py | 28 +++++- 3 files changed, 116 insertions(+), 3 deletions(-) diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index 3a57cf90..65636d56 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -420,6 +420,7 @@ class TasksManager: ) for item in jobs: logger.info(f"JOB: {item}") + return task.schedule_id def remove_all_server_tasks(self, server_id): schedules = HelpersManagement.get_schedules_by_server(server_id) diff --git a/app/classes/web/routes/api/servers/server/tasks/index.py b/app/classes/web/routes/api/servers/server/tasks/index.py index 64ca3bef..1ed8f2bb 100644 --- a/app/classes/web/routes/api/servers/server/tasks/index.py +++ b/app/classes/web/routes/api/servers/server/tasks/index.py @@ -1,16 +1,102 @@ # TODO: create and read +import json import logging +from jsonschema import ValidationError, validate +from app.classes.models.server_permissions import EnumPermissionsServer from app.classes.web.base_api_handler import BaseApiHandler logger = logging.getLogger(__name__) +new_task_schema = { + "type": "object", + "properties": { + "name": {"type": "string"}, + "enabled": { + "type": "boolean", + "default": True, + }, + "action": { + "type": "string", + }, + "interval": {"type": "integer"}, + "interval_type": { + "type": "string", + "enum": [ + # Basic tasks + "hours", + "minutes", + "days", + # Chain reaction tasks: + "reaction", + # CRON tasks: + "", + ], + }, + "start_time": {"type": "string", "pattern": r"\d{1,2}:\d{1,2}"}, + "command": {"type": ["string", "null"]}, + "one_time": {"type": "boolean", "default": False}, + "cron_string": {"type": "string", "default": ""}, + "parent": {"type": ["integer", "null"]}, + "delay": {"type": "integer", "default": 0}, + }, + "additionalProperties": False, + "minProperties": 1, +} class ApiServersServerTasksIndexHandler(BaseApiHandler): def get(self, server_id: str, task_id: str): pass - def post(self, server_id: str, task_id: str): - pass + def post(self, server_id: str): + auth_data = self.authenticate_user() + if not auth_data: + return + + try: + data = json.loads(self.request.body) + except json.decoder.JSONDecodeError as e: + return self.finish_json( + 400, {"status": "error", "error": "INVALID_JSON", "error_data": str(e)} + ) + + try: + validate(data, new_task_schema) + except ValidationError as e: + return self.finish_json( + 400, + { + "status": "error", + "error": "INVALID_JSON_SCHEMA", + "error_data": str(e), + }, + ) + + if server_id not in [str(x["server_id"]) for x in auth_data[0]]: + # if the user doesn't have access to the server, return an error + return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"}) + + if ( + EnumPermissionsServer.SCHEDULE + not in self.controller.server_perms.get_user_id_permissions_list( + auth_data[4]["user_id"], server_id + ) + ): + # if the user doesn't have Schedule permission, return an error + return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"}) + data["server_id"] = server_id + if not data.get("start_time"): + data["start_time"] = "00:00" + task_id = self.tasks_manager.schedule_job(data) + + self.controller.management.add_to_audit_log( + auth_data[4]["user_id"], + f"Edited server {server_id}: added schedule", + server_id, + self.get_remote_ip(), + ) + self.tasks_manager.reload_schedule_from_db() + + self.finish_json(200, {"status": "ok", "task_id": task_id}) diff --git a/app/classes/web/routes/api/servers/server/tasks/task/index.py b/app/classes/web/routes/api/servers/server/tasks/task/index.py index 72bbf7b0..06a8009d 100644 --- a/app/classes/web/routes/api/servers/server/tasks/task/index.py +++ b/app/classes/web/routes/api/servers/server/tasks/task/index.py @@ -52,7 +52,33 @@ class ApiServersServerTasksTaskIndexHandler(BaseApiHandler): pass def delete(self, server_id: str, task_id: str): - pass + auth_data = self.authenticate_user() + if not auth_data: + return + if ( + EnumPermissionsServer.SCHEDULE + not in self.controller.server_perms.get_user_id_permissions_list( + auth_data[4]["user_id"], server_id + ) + ): + # if the user doesn't have Schedule permission, return an error + return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"}) + + try: + self.tasks_manager.remove_job(task_id) + except Exception: + return self.finish_json( + 400, {"status": "error", "error": "NO SCHEDULE FOUND"} + ) + self.controller.management.add_to_audit_log( + auth_data[4]["user_id"], + f"Edited server {server_id}: removed schedule", + server_id, + self.get_remote_ip(), + ) + self.tasks_manager.reload_schedule_from_db() + + return self.finish_json(200, {"status": "ok"}) def patch(self, server_id: str, task_id: str): auth_data = self.authenticate_user()