X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_policy_module%2Fcore%2Fconfig.py;fp=osm_policy_module%2Fcore%2Fconfig.py;h=dab409b13ac5abd27784e6573fbd44ce176a7584;hb=7f11ecff803667fb5cd0e79389eece83ddc96c86;hp=0000000000000000000000000000000000000000;hpb=40aaab6334ccb4a02366259b80a24b3b4e0ec47f;p=osm%2FPOL.git diff --git a/osm_policy_module/core/config.py b/osm_policy_module/core/config.py new file mode 100644 index 0000000..dab409b --- /dev/null +++ b/osm_policy_module/core/config.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- + +# Copyright 2018 Whitestack, LLC +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Whitestack, LLC + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# For those usages not covered by the Apache License, Version 2.0 please +# contact: bdiaz@whitestack.com or glavado@whitestack.com +## +"""Global configuration managed by environment variables.""" + +import logging +import os + +from collections import namedtuple + +import six + +from osm_policy_module.core.singleton import Singleton + +log = logging.getLogger(__name__) + + +class BadConfigError(Exception): + """Configuration exception.""" + + pass + + +class CfgParam(namedtuple('CfgParam', ['key', 'default', 'data_type'])): + """Configuration parameter definition.""" + + def value(self, data): + """Convert a string to the parameter type.""" + try: + return self.data_type(data) + except (ValueError, TypeError): + raise BadConfigError( + 'Invalid value "%s" for configuration parameter "%s"' % ( + data, self.key)) + + +@Singleton +class Config(object): + """Configuration object.""" + + _configuration = [ + CfgParam('OSMPOL_MESSAGE_DRIVER', "kafka", six.text_type), + CfgParam('OSMPOL_MESSAGE_HOST', "localhost", six.text_type), + CfgParam('OSMPOL_MESSAGE_PORT', 9092, int), + CfgParam('OSMPOL_DATABASE_DRIVER', "mongo", six.text_type), + CfgParam('OSMPOL_DATABASE_HOST', "mongo", six.text_type), + CfgParam('OSMPOL_DATABASE_PORT', 27017, int), + CfgParam('OSMPOL_SQL_DATABASE_URI', "sqlite:///mon_sqlite.db", six.text_type), + CfgParam('OSMPOL_LOG_LEVEL', "INFO", six.text_type), + ] + + _config_dict = {cfg.key: cfg for cfg in _configuration} + _config_keys = _config_dict.keys() + + def __init__(self): + """Set the default values.""" + for cfg in self._configuration: + setattr(self, cfg.key, cfg.default) + self.read_environ() + + def read_environ(self): + """Check the appropriate environment variables and update defaults.""" + for key in self._config_keys: + try: + val = self._config_dict[key].data_type(os.environ[key]) + setattr(self, key, val) + except KeyError as exc: + log.debug("Environment variable not present: %s", exc) + return