Initial commit
[ta/config-manager.git] / cmdatahandlers / src / cmdatahandlers / storage_profiles / 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 from cmdatahandlers.api import utils
18
19
20 class Config(config.Config):
21     def __init__(self, confman):
22         super(Config, self).__init__(confman)
23         self.ROOT = 'cloud.storage_profiles'
24         self.DOMAIN = 'storage_profiles'
25
26     def init(self):
27         pass
28
29     def validate(self):
30         self.validate_root()
31         self._validate_storage_profiles()
32
33     def _validate_storage_profiles(self):
34         profiles = self.get_storage_profiles()
35         utils.validate_list_items_unique(profiles)
36         for profile in profiles:
37             self._validate_storage_profile(profile)
38
39     def _validate_storage_profile(self, profile):
40         backend = self.get_profile_backend(profile)
41         storageconf = self.confman.get_storage_config_handler()
42         backends = storageconf.get_storage_backends()
43         if backend not in backends:
44             raise configerror.ConfigError(
45                 'Invalid backend %s provided in profile %s' % (backend, profile))
46         if backend == 'ceph':
47             self.get_profile_nr_of_ceph_osd_disks(profile)
48         elif backend == 'lvm':
49             self.get_profile_lvm_cinder_storage_partitions(profile)
50             self.get_profile_lvm_instance_storage_partitions(profile)
51             self.get_profile_lvm_instance_cow_lv_storage_percentage(profile)
52             self.get_profile_instance_storage_percentage(profile)
53
54     def is_valid_profile(self, profile):
55         self.validate_root()
56         profiles = self.get_storage_profiles()
57         if profile not in profiles:
58             raise configerror.ConfigError('Invalid profile name %s' % profile)
59
60     def get_storage_profiles(self):
61         """ get the storage profiles list
62
63             Return:
64
65             A list of storage profile(s) names
66
67             Raise:
68
69             ConfigError in-case of an error
70         """
71         return self.config[self.ROOT].keys()
72
73     def get_profile_ceph_osd_disks(self, profile):
74         """ get the ceph osd disks
75
76             Argument:
77
78             profile name
79
80             Return:
81
82             The ceph osd disks
83
84             Raise:
85
86             ConfigError in-case of an error
87         """
88         return self._get_attribute(profile, 'ceph_osd_disks')
89
90     def get_profile_ceph_osd_journal_disk(self, profile):
91         """ get the ceph osd journal disk
92
93             Argument:
94
95             profile name
96
97             Return:
98
99             The ceph osd journal disk
100
101             Raise:
102
103             ConfigError in-case of an error
104         """
105         return self._get_attribute(profile, 'ceph_osd_journal_disk')
106
107     def get_profile_ceph_openstack_pg_proportion(self, profile):
108         openstack_pg_ratio, caas_pg_ratio = self._get_ceph_pg_share_ratios(profile)
109         return openstack_pg_ratio / (openstack_pg_ratio + caas_pg_ratio)
110
111     def _get_ceph_pg_share_ratios(self, profile):
112         pg_share_ratios = self.get_profile_ceph_openstack_caas_pg_ratio(profile).split(':')
113         return map(lambda r: float(r), pg_share_ratios)
114
115     def get_profile_ceph_caas_pg_proportion(self, profile):
116         openstack_pg_ratio, caas_pg_ratio = self._get_ceph_pg_share_ratios(profile)
117         return caas_pg_ratio / (openstack_pg_ratio + caas_pg_ratio)
118
119     def get_profile_ceph_openstack_caas_pg_ratio(self, profile):
120         """ get the ceph openstack-caas pg share ratio
121
122             Argument:
123
124             profile name
125
126             Return:
127
128             The ceph osd share ratio
129
130             Raise:
131
132             ConfigError in-case of an error
133         """
134         return self._get_optional_attribute(
135             profile, 'ceph_pg_openstack_caas_share_ratio', "1:0")
136
137     def get_profile_nr_of_ceph_osd_disks(self, profile):
138         """ get the number of ceph osd disks
139
140             Argument:
141
142             profile name
143
144             Return:
145
146             The number of ceph osd disks
147
148             Raise:
149
150             ConfigError in-case of an error
151         """
152         self.validate_root()
153         self.is_valid_profile(profile)
154
155         if 'nr_of_ceph_osd_disks' not in self.config[self.ROOT][profile]:
156             ceph_osd_disks = self._get_attribute(profile, 'ceph_osd_disks')
157             return len(ceph_osd_disks)
158         return self.config[self.ROOT][profile]['nr_of_ceph_osd_disks']
159
160     def get_profile_lvm_cinder_storage_partitions(self, profile):
161         """ get the lvm_cinder_storage_partitions
162
163             Argument:
164
165             profile name
166
167             Return:
168
169             The lvm_cinder_storage_partitions
170
171             Raise:
172
173             ConfigError in-case of an error
174         """
175         return self._get_attribute(profile, 'lvm_cinder_storage_partitions')
176
177     def get_profile_backend(self, profile):
178         """ get the storage profile backend
179
180             Argument:
181
182             profile name
183
184             Return:
185
186             The profile backend
187
188             Raise:
189
190             ConfigError in-case of an error
191         """
192         return self._get_attribute(profile, 'backend')
193
194     def get_profile_lvm_instance_storage_partitions(self, profile):
195         """ get the lvm_instance_storage_partitions
196
197             Argument:
198
199             profile name
200
201             Return:
202
203             The lvm_instance_storage_partitions
204
205             Raise:
206
207             ConfigError in-case of an error
208         """
209         return self._get_attribute(profile, 'lvm_instance_storage_partitions')
210
211     def get_profile_lvm_instance_cow_lv_storage_percentage(self, profile):
212         """ get the lvm_instance_cow_lv_storage_percentage
213
214             Argument:
215
216             profile name
217
218             Return:
219
220             The lvm_instance_cow_lv_storage_percentage
221
222             Raise:
223
224             ConfigError in-case of an error
225         """
226         return self._get_attribute(profile, 'lvm_instance_cow_lv_storage_percentage')
227
228     def get_profile_instance_storage_percentage(self, profile):
229         """ get the instance_storage_percentage
230
231             Argument:
232
233             profile name
234
235             Return:
236
237             The instance_storage_percentage
238
239             Raise:
240
241             ConfigError in-case of an error
242         """
243         return self._get_attribute(profile, 'instance_storage_percentage')
244
245     def get_profile_bare_lvm_mount_options(self, profile):
246         """ get the mount_options
247
248             Argument:
249
250             profile name
251
252             Return:
253
254             The mount_options
255
256             Raise:
257
258             ConfigError in-case of an error
259         """
260         return self._get_optional_attribute(profile, 'mount_options')
261
262     def get_profile_bare_lvm_mount_dir(self, profile):
263         """ get the mount_dir
264
265             Argument:
266
267             profile name
268
269             Return:
270
271             The mount_dir
272
273             Raise:
274
275             ConfigError in-case of an error
276         """
277         return self._get_attribute(profile, 'mount_dir')
278
279     def get_profile_bare_lvm_lv_name(self, profile):
280         """ get the lv_name
281
282             Argument:
283
284             profile name
285
286             Return:
287
288             The lv_name
289
290             Raise:
291
292             ConfigError in-case of an error
293         """
294         return self._get_attribute(profile, 'lv_name')
295
296     def _get_attribute(self, profile, attribute):
297         """ get arbirary storage profile attribute
298
299             Arguments:
300
301             - profile name
302             - attribute name
303
304             Return:
305
306             The attribute
307
308             Raise:
309
310             ConfigError in-case of an error
311         """
312         self.validate_root()
313         self.is_valid_profile(profile)
314         if attribute not in self.config[self.ROOT][profile]:
315             raise configerror.ConfigError(
316                 'Profile %s does not have %s configured' % (attribute, profile))
317         return self.config[self.ROOT][profile][attribute]
318
319     def _get_optional_attribute(self, profile, attribute, default_value=""):
320         """ get arbirary optional storage profile attribute
321
322             Arguments:
323
324             - profile name
325             - attribute name
326
327             Return:
328
329             The attribute
330
331             Raise:
332
333             ConfigError in-case of an error
334         """
335         self.validate_root()
336         self.is_valid_profile(profile)
337         if attribute not in self.config[self.ROOT][profile]:
338             return default_value
339         return self.config[self.ROOT][profile][attribute]