From 8d8cc8b5e0dcca200fc5bd7f732bf95370f5c2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Diot?= Date: Thu, 13 Jun 2024 12:10:57 +0200 Subject: [PATCH] chore: Fix config checks in web UI (post optimization) --- src/ui/main.py | 42 ++++++++---------------------------------- src/ui/src/Config.py | 34 +++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 45 deletions(-) diff --git a/src/ui/main.py b/src/ui/main.py index 5cf286f4f..0d9510d67 100755 --- a/src/ui/main.py +++ b/src/ui/main.py @@ -998,15 +998,6 @@ def services(): # Edit check fields and remove already existing ones for variable, value in deepcopy(variables).items(): - if variable == "IS_DRAFT" or variable.endswith("SCHEMA"): - del variables[variable] - continue - - if value == "on": - value = "yes" - elif value == "off": - value = "no" - if ( variable in variables and variable != "SERVER_NAME" @@ -1014,6 +1005,8 @@ def services(): ): del variables[variable] + variables = app.config["CONFIG"].check_variables(variables) + if ( was_draft == is_draft and request.form["operation"] == "edit" @@ -1026,26 +1019,19 @@ def services(): elif request.form["operation"] == "new" and not variables: return redirect_flash_error("The service was not created because all values had the default value.", "services", True) - error = app.config["CONFIG"].check_variables(variables) - - if error: - error_message("The config variable checks returned error") - # Delete if request.form["operation"] == "delete": is_request_params(["SERVER_NAME"], "services", True) - error = app.config["CONFIG"].check_variables({"SERVER_NAME": request.form["SERVER_NAME"]}) + variables = app.config["CONFIG"].check_variables({"SERVER_NAME": request.form["SERVER_NAME"]}) - if error: + if not variables: error_message(f"Error while deleting the service {request.form['SERVER_NAME']}") if config.get(f"{request.form['SERVER_NAME'].split(' ')[0]}_SERVER_NAME", {"method": "scheduler"})["method"] != "ui": return redirect_flash_error("The service cannot be deleted because it has not been created with the UI.", "services", True) - error = 0 - curr_changes = app.config["DB"].check_changes() old_server_name = request.form.get("OLD_SERVER_NAME", "") @@ -1155,27 +1141,15 @@ def global_config(): config = app.config["CONFIG"].get_config(with_drafts=True, filtered_settings=variables.keys()) services = config["SERVER_NAME"]["value"].split(" ") for variable, value in variables.copy().items(): - if variable in ("AUTOCONF_MODE", "SWARM_MODE", "KUBERNETES_MODE", "SERVER_NAME", "IS_LOADING", "IS_DRAFT") or variable.endswith("SCHEMA"): - del variables[variable] - continue - - if value == "on": - value = "yes" - elif value == "off": - value = "no" - setting = config.get(variable, {"value": None, "global": True}) if setting["global"] and value == setting["value"]: del variables[variable] + variables = app.config["CONFIG"].check_variables(variables, config) + if not variables: return redirect_flash_error("The global configuration was not edited because no values were changed.", "global_config", True) - error = app.config["CONFIG"].check_variables(variables) - - if error: - return redirect_flash_error("The global configuration variable checks returned error", "global_config", True) - for variable, value in variables.copy().items(): for service in services: setting = config.get(f"{service}_{variable}", None) @@ -1219,7 +1193,7 @@ def global_config(): ) # Display global config - global_config = app.config["CONFIG"].get_config(global_only=True) + global_config = app.config["DB"].get_config(global_only=True, methods=True) return render_template("global_config.html", username=current_user.get_id(), global_config=global_config, dumped_global_config=dumps(global_config)) @@ -1756,7 +1730,7 @@ def custom_plugin(plugin: str): if plugin_id is None: return error_message("Plugin not found"), 404 - config = app.config["CONFIG"].get_config(methods=False) + config = app.config["DB"].get_config() # Check if we are using metrics for service in config.get("SERVER_NAME", "").split(" "): diff --git a/src/ui/src/Config.py b/src/ui/src/Config.py index 43bf29a07..aca411b4f 100644 --- a/src/ui/src/Config.py +++ b/src/ui/src/Config.py @@ -104,7 +104,7 @@ class Config: """ return self.__db.get_services_settings(methods=methods, with_drafts=with_drafts) - def check_variables(self, variables: dict) -> int: + def check_variables(self, variables: dict, config: dict) -> dict: """Testify that the variables passed are valid Parameters @@ -117,32 +117,44 @@ class Config: int Return the error code """ - error = 0 plugins_settings = self.get_plugins_settings() - for k, v in variables.items(): + for k, v in variables.copy().items(): check = False + if k.endswith("SCHEMA"): + variables.pop(k) + continue + if k in plugins_settings: setting = k else: setting = k[0 : k.rfind("_")] # noqa: E203 if setting not in plugins_settings or "multiple" not in plugins_settings[setting]: - error = 1 flash(f"Variable {k} is not valid.", "error") + variables.pop(k) continue + if setting in ("AUTOCONF_MODE", "SWARM_MODE", "KUBERNETES_MODE", "IS_LOADING", "IS_DRAFT"): + flash(f"Variable {k} is not editable, ignoring it", "error") + variables.pop(k) + continue + elif setting not in config and plugins_settings[setting]["default"] == v: + variables.pop(k) + continue + try: if re_search(plugins_settings[setting]["regex"], v): check = True - except RegexError: - self.__db.logger.warning(f"Invalid regex for setting {setting} : {plugins_settings[setting]['regex']}, ignoring regex check") - - if not check: - error = 1 - flash(f"Variable {k} is not valid.", "error") + except RegexError as e: + flash(f"Invalid regex for setting {setting} : {plugins_settings[setting]['regex']}, ignoring regex check:{e}", "error") + variables.pop(k) continue - return error + if not check: + flash(f"Variable {k} is not valid.", "error") + variables.pop(k) + + return variables def new_service(self, variables: dict, is_draft: bool = False) -> Tuple[str, int]: """Creates a new service from the given variables