From: Plamen Dimitrov Date: Tue, 5 Apr 2022 03:29:01 +0000 (+0300) Subject: Replace the mk_config with a sample minimal cnfvar templates module X-Git-Tag: v1.7.1~3^2~7 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=b5abc93acace9a248ebc891229bb04146ae91237;p=pyi2ncommon Replace the mk_config with a sample minimal cnfvar templates module The following commit provides an example for turning the partially developed (and always incomplete for new use cases) mk_config functions into ones that only provide a minimal set of defaults and can still be used as one-liners by client/user code. The example implementation at the moment is for a simple and a user template cnfvars and will later on be extended to groups and all other major cnfvars. --- diff --git a/src/cnfvar/__init__.py b/src/cnfvar/__init__.py index 3d60a25..9529af2 100644 --- a/src/cnfvar/__init__.py +++ b/src/cnfvar/__init__.py @@ -1,6 +1,7 @@ from .model import Cnf, CnfList from .binary import CnfBinary from .store import CnfStore, BinaryCnfStore, CommitException +from . import templates __all__ = ["Cnf", "CnfList", "CnfBinary", "CnfStore", "BinaryCnfStore", "CommitException"] diff --git a/src/mk_config.py b/src/cnfvar/templates.py similarity index 88% rename from src/mk_config.py rename to src/cnfvar/templates.py index 47860d9..2567058 100644 --- a/src/mk_config.py +++ b/src/cnfvar/templates.py @@ -20,23 +20,14 @@ """ -SUMMARY +summary ------------------------------------------------------ -Utility for one-step dynamic cnfvar configuration. +Module for one-step dynamic cnfvar configuration using minimal templates. -.. note:: DEPRECATED! Please do not extend this or add new uses of this module, - use :py:mod:`pyi2ncommon.arnied_api` or :py:mod:`pyi2ncommon.cnfvar` - instead! +.. codeauthor:: Intra2net -Copyright: Intra2net AG - -CONTENTS ------------------------------------------------------- -Contains general as well as specialized versions of some of the main -configurations performed by our tests. - -INTERFACE +interface ------------------------------------------------------ """ @@ -45,60 +36,47 @@ import time import logging # custom imports -from . import arnied_wrapper as aw -from .arnied_wrapper import Delete, Update, Add, Child, batch_update_cnf, build_cnf -from .cnfline import build_cnfvar, build_group, build_intraclient -from .cnfline import build_nic, build_provider, build_user +from .model import Cnf, CnfList + + +log = logging.getLogger('pyi2ncommon.cnfvar.templates') -log = logging.getLogger('pyi2ncommon.mk_config') ############################################################################### # MINOR CONFIGURATION ############################################################################### -def simple(varname, data, filename): +def simple(varname, data, instance=0): """ - Generate and save a single-variable configuration file. + Generate a minimal simple cnf variable in terms of required and validated attributes. :param str varname: cnf variable name - :param str data: cnf variable data - :param str filename: config name - :returns: generated config filename - :rtype: str + :param str data: cnf variable value + :param int instance: cnf variable instance number + :returns: generated cnf variable + :rtype: :py:class:`Cnf` """ - log.info("Create single-variable configuration file") - tmp = build_cnfvar.BuildCnfVar(name=varname, data=data) - [filename] = aw.prep_config_paths([filename], aw.DUMP_CONFIG_DIR) - logging.info("Saving simple configuration to %s", filename) - tmp.save(filename) - return filename + log.info(f"Generating a simple {varname} cnfvar") + return Cnf(varname, value=data, instance=instance) -def user(username="admin", instance=1, suffix="host"): +def user(username, password, instance=-1): """ - Generate and save a user configuration file. + Generate a minimal user cnf variable in terms of required and validated attributes. - :param str username: username for the user variable - :param int instance: instance number (for multiple users, -1 for next available) - :param str suffix: optional suffix to use for config identification - :returns: generated config filename - :rtype: str + :param str username: username for the user + :param str password: password for the user + :param int instance: instance number for the user + :returns: generated cnf variable + :rtype: :py:class:`Cnf` """ - log.info("Create arnied user configuration") - user_obj = batch_update_cnf( - build_user.BuildUser(data=username, instance=instance, line_no=1), - [(Update, ("USER_FULLNAME", 0, username)), - (Update, ("USER_GROUP_MEMBER_REF", 0, "1")), - (Add, ("USER_GROUP_MEMBER_REF", 1, "2")), - (Delete, "USER_WEBMAIL_MESSAGES_PER_PAGE"), - (Delete, "USER_LOCALE"), - (Delete, "USER_TRASH_DELETEDAYS"), - (Delete, "USER_WEBMAIL_SIGNATURE")]) - user_cnf = "user-%d-%s.cnf" % (time.time(), suffix) - [user_cnf] = aw.prep_config_paths([user_cnf], aw.DUMP_CONFIG_DIR) - logging.info("Saving user configuration to %s", user_cnf) - user_obj.save(user_cnf) + log.info(f"Generating a minimal user {username} cnfvar") + user_cnf = Cnf("user", value=username, instance=instance) + user_cnf.add_children( + ("user_fullname", username.capitalize()), + ("user_group_member_ref", 2), + ("user_password", password)) return user_cnf