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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from cmdatahandlers.api import configerror
16 from cmdatahandlers.api import config
17 from cmdatahandlers.api import utils
19 class Config(config.Config):
20 def __init__(self, confman):
21 super(Config, self).__init__(confman)
22 self.ROOT='cloud.time'
31 if 'zone' not in self.config[self.ROOT]:
32 raise configerror.ConfigError('No zone configuration found')
34 if 'ntp_servers' not in self.config[self.ROOT]:
35 raise configerror.ConfigError('No ntp servers found')
37 if 'auth_type' not in self.config[self.ROOT]:
38 self.config[self.ROOT]['auth_type'] = 'crypto'
40 if 'serverkeys_path' not in self.config[self.ROOT]:
41 self.config[self.ROOT]['serverkeys_path'] = ''
43 self._validate_time_zone()
44 self._validate_ntp_servers()
46 def _validate_time_zone(self):
49 zone = self.get_zone()
52 raise configerror.ConfigError('The timezone %s is not valid' % zone)
54 def _validate_ntp_servers(self):
55 servers = self.get_ntp_servers()
56 utils.validate_list_items_unique(servers)
58 for server in servers:
59 utils.validate_ipv4_address(server)
66 A string representing time zone
70 ConfigError in-case of an error
74 return self.config[self.ROOT]['zone']
76 """ get the ntp servers
80 A list of ntp server addresses.
84 ConfigError in-case of an error
86 def get_ntp_servers(self):
88 return self.config[self.ROOT]['ntp_servers']
90 def get_auth_type(self):
92 return self.config[self.ROOT]['auth_type']
94 def get_time_config(self):
96 return self.config[self.ROOT]
98 def set_config(self, **args):
99 if args['auth_type'] is not None:
100 self.config[self.ROOT]['auth_type'] = args['auth_type']
101 if args['servers'] is not None:
102 self.config[self.ROOT]['ntp_servers'] = args['servers']
103 if args['keyfile_url'] is not None:
104 self.config[self.ROOT]['serverkeys_path'] = args['keyfile_url']
105 if args['zone'] is not None:
106 self.config[self.ROOT]['zone'] = args['zone']
108 return self.config[self.ROOT]