Unified way to provide configurations and env variables(proposal 1) 09/25709/2
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Fri, 9 Dec 2016 08:00:45 +0000 (16:00 +0800)
committerSerenaFeng <feng.xiaowei@zte.com.cn>
Thu, 15 Dec 2016 09:04:39 +0000 (17:04 +0800)
In this proposal, everything in os.environ can be accessed directly using const,
and with the exact name os.environ defined.

The name of a configuration is totally decided by config_functest.yaml,
which is the joint name of each level with '_'.

This version requires us to naming each level in yaml in a little cleancode way,
or else the configuration name would become ugly. Let me take the current
defination of general.directories as instance, the configuration name will be
'directories_dir_repo_functest', looks not very good. But if we make some adaption:
general:
   dir:
      repo_functest:
it will become 'dir_repo_functest', looks a little better.

For vIMs is well defined, let me take it for example, the configuration names will look like:
vIMS_clearwater_blueprint_url
vIMS_clearwater_blueprint_file_name
vIMS_clearwater_blueprint_name
vIMS_clearwater_blueprint_branch
vIMS_clearwater_blueprint_destination_folder
vIMS_clearwater_deployment-name

Change-Id: I18ec8686d9cfb2b80a370d422c6ad81a8800585c
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
functest/utils/config.py [new file with mode: 0644]
functest/utils/constants.py [new file with mode: 0644]
functest/utils/env.py [new file with mode: 0644]

diff --git a/functest/utils/config.py b/functest/utils/config.py
new file mode 100644 (file)
index 0000000..4cee634
--- /dev/null
@@ -0,0 +1,30 @@
+import os
+
+import yaml
+
+
+class Config(object):
+    def __init__(self):
+        if 'CONFIG_FUNCTEST_YAML' not in os.environ:
+            raise Exception('CONFIG_FUNCTEST_YAML not configed')
+        self.config_functest = os.environ['CONFIG_FUNCTEST_YAML']
+        try:
+            with open(self.config_functest) as f:
+                self.functest_yaml = yaml.safe_load(f)
+                self.parse(None, self.functest_yaml)
+        except:
+            raise Exception('Parse {} failed'.format(self.config_functest))
+
+    def parse(self, attr_now, left_parametes):
+        for param_n, param_v in left_parametes.iteritems():
+            attr_further = self.get_attr_further(attr_now, param_n)
+            if not isinstance(param_v, dict):
+                self.__setattr__(attr_further, param_v)
+            else:
+                self.parse(attr_further, param_v)
+
+    def get_attr_further(self, attr_now, next):
+        return attr_now if next == 'general' else (
+            '{}_{}'.format(attr_now, next) if attr_now else next)
+
+CONF = Config()
diff --git a/functest/utils/constants.py b/functest/utils/constants.py
new file mode 100644 (file)
index 0000000..2e8eb3f
--- /dev/null
@@ -0,0 +1,20 @@
+import config
+import env
+
+
+class Constants(object):
+    def __init__(self):
+        for attr_n, attr_v in config.CONF.__dict__.iteritems():
+            self.__setattr__(attr_n, attr_v)
+        for env_n, env_v in env.ENV.__dict__.iteritems():
+            self.__setattr__(env_n, env_v)
+
+
+CONST = Constants()
+
+if __name__ == '__main__':
+    print CONST.__dict__
+    print CONST.NODE_NAME
+    print CONST.vIMS_clearwater_blueprint_url
+    print CONST.vIMS_clearwater_blueprint_file_name
+    print CONST.vIMS_clearwater_blueprint_name
diff --git a/functest/utils/env.py b/functest/utils/env.py
new file mode 100644 (file)
index 0000000..b6af767
--- /dev/null
@@ -0,0 +1,18 @@
+import os
+
+default_envs = {
+    'NODE_NAME': 'unknown_pod',
+    'CI_DEBUG': 'true'
+}
+
+
+class Environment(object):
+    def __init__(self):
+        for k, v in os.environ.iteritems():
+            self.__setattr__(k, v)
+        for k, v in default_envs.iteritems():
+            if k not in os.environ:
+                self.__setattr__(k, v)
+
+
+ENV = Environment()