Openstack-Aodh plugin
[osm/MON.git] / plugins / OpenStack / settings.py
1 """Configurations for the Aodh plugin."""
2
3 from __future__ import unicode_literals
4
5 from plugins.Openstack.singleton import Singleton
6
7 from collections import namedtuple
8 import six
9 import os
10
11
12 class BadConfigError(Exception):
13 """Configuration exception"""
14 pass
15
16
17 class CfgParam(namedtuple('CfgParam', ['key', 'default', 'data_type'])):
18 """Configuration parameter definition"""
19
20 def value(self, data):
21 """Convert a string to the parameter type"""
22
23 try:
24 return self.data_type(data)
25 except (ValueError, TypeError) as exc:
26 raise BadConfigError(
27 'Invalid value "%s" for configuration parameter "%s"' % (
28 data, self.key))
29
30
31 @Singleton
32 class Config(object):
33 """Plugin confguration"""
34
35 _configuration = [
36 CfgParam('OS_AUTH_URL', None, six.text_type),
37 CfgParam('OS_IDENTITY_API_VERSION', "3", six.text_type),
38 CfgParam('OS_USERNAME', "aodh", six.text_type),
39 CfgParam('OS_PASSWORD', "password", six.text_type),
40 CfgParam('OS_TENANT_NAME', "service", six.text_type),
41 ]
42
43 _config_dict = {cfg.key: cfg for cfg in _configuration}
44 _config_keys = _config_dict.keys()
45
46 def __init__(self):
47 """Set the default values"""
48 for cfg in self._configuration:
49 setattr(self, cfg.key, cfg.default)
50
51 def read_environ(self):
52 """Check the appropriate environment variables and update defaults."""
53
54 for key in self._config_keys:
55 if (key == "OS_IDENTITY_API_VERSION" or key == "OS_PASSWORD"):
56 val = str(os.environ[key])
57 setattr(self, key, val)
58 elif (key == "OS_AUTH_URL"):
59 val = str(os.environ[key]) + "/v3"
60 setattr(self, key, val)
61 else:
62 # TODO: Log errors and no config updates required
63 return