Adds use of OSMPOL_SQL_DATABASE_URI config param to connect to DB
[osm/POL.git] / osm_policy_module / core / config.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 ##
24 """Global configuration managed by environment variables."""
25
26 import logging
27 import os
28
29 from collections import namedtuple
30
31 import six
32
33 from osm_policy_module.core.singleton import Singleton
34
35 log = logging.getLogger(__name__)
36
37
38 class BadConfigError(Exception):
39 """Configuration exception."""
40
41 pass
42
43
44 class CfgParam(namedtuple('CfgParam', ['key', 'default', 'data_type'])):
45 """Configuration parameter definition."""
46
47 def value(self, data):
48 """Convert a string to the parameter type."""
49 try:
50 return self.data_type(data)
51 except (ValueError, TypeError):
52 raise BadConfigError(
53 'Invalid value "%s" for configuration parameter "%s"' % (
54 data, self.key))
55
56
57 @Singleton
58 class Config(object):
59 """Configuration object."""
60
61 _configuration = [
62 CfgParam('OSMPOL_MESSAGE_DRIVER', "kafka", six.text_type),
63 CfgParam('OSMPOL_MESSAGE_HOST', "localhost", six.text_type),
64 CfgParam('OSMPOL_MESSAGE_PORT', 9092, int),
65 CfgParam('OSMPOL_DATABASE_DRIVER', "mongo", six.text_type),
66 CfgParam('OSMPOL_DATABASE_HOST', "mongo", six.text_type),
67 CfgParam('OSMPOL_DATABASE_PORT', 27017, int),
68 CfgParam('OSMPOL_SQL_DATABASE_URI', "sqlite:///policy_module.db", six.text_type),
69 CfgParam('OSMPOL_LOG_LEVEL', "INFO", six.text_type),
70 CfgParam('OSMPOL_KAFKA_LOG_LEVEL', "WARN", six.text_type),
71 ]
72
73 _config_dict = {cfg.key: cfg for cfg in _configuration}
74 _config_keys = _config_dict.keys()
75
76 def __init__(self):
77 """Set the default values."""
78 for cfg in self._configuration:
79 setattr(self, cfg.key, cfg.default)
80 self.read_environ()
81
82 def read_environ(self):
83 """Check the appropriate environment variables and update defaults."""
84 for key in self._config_keys:
85 try:
86 val = self._config_dict[key].data_type(os.environ[key])
87 setattr(self, key, val)
88 except KeyError as exc:
89 log.debug("Environment variable not present: %s", exc)
90 return