Plugins for configuration manager
[ta/cm-plugins.git] / validators / src / SectionValidation.py
1 #!/usr/bin/python
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import logging
17
18 from cmframework.apis import cmvalidator
19 from cmdatahandlers.api import validation
20
21
22 class SectionValidation(cmvalidator.CMValidator):
23
24     Required = ['cloud.name', 'cloud.version', 'cloud.time', 'cloud.users', 'cloud.networking',
25                 'cloud.storage', 'cloud.hosts', 'cloud.network_profiles',
26                 'cloud.storage_profiles', 'cloud.host_os']
27
28     filterstr = r'^cloud\.'
29
30     def get_subscription_info(self):
31         logging.debug('get_subscription info called')
32         return self.filterstr
33
34     def validate_set(self, dict_key_value):
35         logging.debug('validate_set called with %s', str(dict_key_value))
36
37         key_list = dict_key_value.keys()
38         self.validate_sections(key_list)
39
40     def validate_delete(self, prop):
41         # Domain specific validators should take care of validating deletion
42         pass
43
44     def validate_sections(self, sections):
45         names = []
46         missing = ''
47         client = self.get_plugin_client()
48
49         for name in self.Required:
50             if name not in sections:
51                 names.append(name)
52         properties = client.get_properties(self.filterstr)
53         keys = properties.keys()
54         for name in names:
55             if name not in keys:
56                 missing += ', ' + name if missing else name
57         if missing:
58             raise validation.ValidationError('Mandatory sections missing from configuration: %s'
59                                              % missing)