Replace the mk_config with a sample minimal cnfvar templates module
authorPlamen Dimitrov <plamen.dimitrov@intra2net.com>
Tue, 5 Apr 2022 03:29:01 +0000 (06:29 +0300)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 19 May 2022 09:13:27 +0000 (11:13 +0200)
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.

src/cnfvar/__init__.py
src/cnfvar/templates.py [moved from src/mk_config.py with 88% similarity]

index 3d60a25..9529af2 100644 (file)
@@ -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"]
similarity index 88%
rename from src/mk_config.py
rename to src/cnfvar/templates.py
index 47860d9..2567058 100644 (file)
 
 """
 
-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