Initial commit
[ta/config-manager.git] / cmdatahandlers / src / cmdatahandlers / openstack / config.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from cmdatahandlers.api import configerror
16 from cmdatahandlers.api import config
17
18 class Config(config.Config):
19     def __init__(self, confman):
20         super(Config, self).__init__(confman)
21         self.ROOT = 'cloud.openstack'
22         self.DOMAIN = 'openstack'
23
24     def init(self):
25         pass
26
27     def _getopenstack(self):
28         hostsconf = self.confman.get_hosts_config_handler()
29         return hostsconf.get_service_profile_hosts('controller')
30
31     def validate(self):
32         if self._getopenstack():
33             self.validate_root()
34             self._validate_storage_backend()
35             self.get_admin_password()
36
37     def _validate_storage_backend(self):
38         backend = self.get_storage_backend()
39         storageconf = self.confman.get_storage_config_handler()
40         backends = storageconf.get_storage_backends()
41         if backend not in backends:
42             raise configerror.ConfigError('Invalid storage backend %s' % backend)
43
44     def get_admin_password(self):
45         """ get the admin password
46
47             Return:
48
49             The admin password
50
51             Raise:
52
53             ConfigError in-case of an error
54         """
55         if not self._getopenstack():
56             return ''
57
58         self.validate_root()
59
60         if 'admin_password' not in self.config[self.ROOT]:
61             raise configerror.ConfigError('No admin_password specified')
62
63         return self.config[self.ROOT]['admin_password']
64
65     def get_storage_backend(self):
66         """ get the openstack storage backend
67
68             Return:
69
70             The openstack storage backend name
71
72             Raise:
73
74             ConfigError in-case of an error
75         """
76         if not self._getopenstack():
77             storageconf = self.confman.get_storage_config_handler()
78             enabled_backends = storageconf.get_enabled_backends()
79             if enabled_backends:
80                 return enabled_backends[0]
81             return ''
82
83         self.validate_root()
84
85         if 'storage_backend'  not in self.config[self.ROOT]:
86             raise configerror.ConfigError('No storage backend configured')
87
88         return self.config[self.ROOT]['storage_backend']
89
90     def get_instance_default_backend(self):
91         """ get the openstack instance backend
92
93             Return:
94
95             The openstack instance backend name
96
97             Raise:
98
99             ConfigError in-case of an error
100         """
101         if not self._getopenstack():
102             return "default"
103
104         self.validate_root()
105
106         if 'instance_default_backend' not in self.config[self.ROOT]:
107             return "default"
108
109         return self.config[self.ROOT]['instance_default_backend']
110
111     def mask_sensitive_data(self):
112         self.config[self.ROOT]['admin_password'] = self.MASK