Plugins for configuration manager
[ta/cm-plugins.git] / validators / src / UsersValidation.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 import json
18
19 from cmframework.apis import cmvalidator
20 from cmdatahandlers.api import validation
21
22
23 class UsersValidation(cmvalidator.CMValidator):
24     domain = 'cloud.users'
25
26     def get_subscription_info(self):
27         logging.debug('get_subscription info called')
28         return r'^cloud\.users$'
29
30     def validate_set(self, dict_key_value):
31         user_attr = 'admin_user_name'
32         passwd_attr = 'admin_user_password'
33         init_user_attr = 'initial_user_name'
34         init_passwd_attr = 'initial_user_password'
35
36         logging.debug('validate_set called with %s' % str(dict_key_value))
37
38         value_str = dict_key_value.get(self.domain)
39         value_dict = {} if not value_str else json.loads(value_str)
40         if not value_dict:
41             raise validation.ValidationError('No value for %s' % self.domain)
42         if not isinstance(value_dict, dict):
43             raise validation.ValidationError('%s value is not a dict' % self.domain)
44
45         utils = validation.ValidationUtils()
46         user = value_dict.get(user_attr)
47         if user:
48             utils.validate_username(user)
49         else:
50             raise validation.ValidationError('Missing %s' % user_attr)
51         um_user = value_dict.get(init_user_attr)
52         if um_user:
53             utils.validate_username(um_user)
54         else:
55             raise validation.ValidationError('Missing %s' % init_user_attr)
56
57         if not value_dict.get(passwd_attr):
58             raise validation.ValidationError('Missing %s' % passwd_attr)
59         if not value_dict.get(init_passwd_attr):
60             raise validation.ValidationError('Missing %s' % init_passwd_attr)
61
62     def validate_delete(self, dict_key_value):
63         logging.debug('validate_delete called with %s' % str(dict_key_value))
64         raise validation.ValidationError('%s cannot be deleted' % self.domain)