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