Updated the OpenStack plugins
[osm/MON.git] / plugins / OpenStack / settings.py
1 # Copyright 2017 Intel Research and Development Ireland Limited
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: helena.mcgough@intel.com or adrian.hoban@intel.com
21 ##
22 """Configurations for the OpenStack plugins."""
23
24 from __future__ import unicode_literals
25
26 import logging as log
27 import os
28
29 from collections import namedtuple
30
31 from plugins.OpenStack.singleton import Singleton
32
33 import six
34
35 __author__ = "Helena McGough"
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 """Plugin confguration."""
60
61 _configuration = [
62 CfgParam('OS_AUTH_URL', None, six.text_type),
63 CfgParam('OS_IDENTITY_API_VERSION', "3", six.text_type),
64 CfgParam('OS_USERNAME', None, six.text_type),
65 CfgParam('OS_PASSWORD', "password", six.text_type),
66 CfgParam('OS_TENANT_NAME', "service", six.text_type),
67 ]
68
69 _config_dict = {cfg.key: cfg for cfg in _configuration}
70 _config_keys = _config_dict.keys()
71
72 def __init__(self):
73 """Set the default values."""
74 for cfg in self._configuration:
75 setattr(self, cfg.key, cfg.default)
76
77 def read_environ(self, service):
78 """Check the appropriate environment variables and update defaults."""
79 for key in self._config_keys:
80 if (key == "OS_IDENTITY_API_VERSION" or key == "OS_PASSWORD"):
81 val = str(os.environ[key])
82 setattr(self, key, val)
83 elif (key == "OS_AUTH_URL"):
84 val = str(os.environ[key]) + "/v3"
85 setattr(self, key, val)
86 else:
87 # Default username for a service is it's name
88 setattr(self, 'OS_USERNAME', service)
89 log.info("Configuration complete!")
90 return