Plugins for configuration manager
[ta/cm-plugins.git] / validators / src / OpenstackValidation.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 # pylint: disable=invalid-name, missing-docstring, too-few-public-methods,
17 # pylint: disable=logging-not-lazy, too-many-locals
18
19 import logging
20 import json
21
22 from cmframework.apis import cmvalidator
23 from cmdatahandlers.api import validation
24
25
26 class OpenstackValidationError(validation.ValidationError):
27     pass
28
29
30 class OpenstackValidation(cmvalidator.CMValidator):
31     domain = "cloud.openstack"
32
33     def get_subscription_info(self):  # pylint: disable=no-self-use
34         logging.debug('get_subscription info called')
35         return r'^cloud\.openstack$'
36
37     def validate_set(self, dict_key_value):
38         logging.debug('validate_set called with %s' % str(dict_key_value))
39
40         client = self.get_plugin_client()
41
42         for key, value in dict_key_value.iteritems():
43             value_str = value
44             value_dict = json.loads(value_str)
45
46             if key == self.domain:
47                 openstack_config = value_dict
48                 if not isinstance(value_dict, dict):
49                     raise validation.ValidationError('%s value is not a dict' % self.domain)
50             else:
51                 raise validation.ValidationError('Unexpected configuration %s' % key)
52             self.validate_openstack(openstack_config)
53
54     def validate_delete(self, properties):
55         logging.debug('validate_delete called with %s' % str(properties))
56         if self.domain in properties:
57             raise validation.ValidationError('%s cannot be deleted' % self.domain)
58         else:
59             raise validation.ValidationError('References in %s, cannot be deleted' % self.domain)
60
61     def validate_openstack(self, openstack_config):
62         if not openstack_config:
63             raise validation.ValidationError('No value for %s' % self.domain)
64
65         self.validate_admin_password(openstack_config)
66
67     @staticmethod
68     def validate_admin_password(openstack_config):
69         password = 'admin_password'
70         passwd = openstack_config.get(password)
71         if not passwd:
72             raise validation.ValidationError('Missing %s' % password)