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.
23 from cmdatahandlers.api import configerror
25 class ConfigManager(object):
26 #This needs to be updated when new domain are introduced, a getter function
27 #should be added to the domain config handler
29 def __init__(self, schema, configjson):
30 #validate according to schema
32 self.configjson = configjson
33 builder = pjo.ObjectBuilder(schema)
34 ns = builder.build_classes()
35 self.config = ns.Config(**configjson)
36 self._load_config_handlers()
37 except Exception as exp:
38 raise configerror.ConfigError(str(exp))
41 def __init__(self, configjson):
42 #validate according to schema
45 self.configjson = configjson
46 self._load_config_handlers()
47 except Exception as exp:
48 raise configerror.ConfigError(str(exp))
51 return self.configjson
53 def get_cloud_name(self):
54 if 'cloud.name' not in self.configjson:
55 raise configerror.ConfigError('Cloud name not defined')
57 return self.configjson['cloud.name']
59 def get_cloud_description(self):
60 if 'cloud.description' not in self.configjson:
61 raise configerror.ConfigError('Cloud description not defined')
62 return self.configjson['cloud.description']
64 def get_cloud_installation_date(self):
65 if 'cloud.installation_date' not in self.configjson:
66 raise configerror.ConfigError('Cloud installation date is not defined')
67 return self.configjson['cloud.installation_date']
69 def get_cloud_installation_phase(self):
70 if 'cloud.installation_phase' not in self.configjson:
71 raise configerror.ConfigError('Cloud installation phase is not defined')
72 return self.configjson['cloud.installation_phase']
74 def _load_config_handlers(self):
75 myfolder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe()))[0]))
76 dirn = os.path.dirname(myfolder)
77 basen = os.path.basename(myfolder)
79 for d in os.listdir(dirn):
82 if not os.path.isdir(dirn + '/' + d):
85 configmodule = dirn + '/' + d + '/config.py'
88 if not os.path.isfile(configmodule):
91 mod = imp.load_source(d, configmodule)
93 config = mod.Config(self)
95 domain = config.get_domain()
97 self.configmap[domain] = config
99 domhandlerfunc = 'get_' + domain + '_config_handler'
101 setattr(self, domhandlerfunc, types.MethodType(self._get_domain_config_handler, domain))
104 #finalize initialization after objects are created
105 #this is needed to handle inter-handler dependencies
106 for domain, handler in self.configmap.iteritems():
111 def _get_domain_config_handler(self, domain):
112 if domain not in self.configmap:
113 raise configerror.ConfigError('Invalid domain')
115 return self.configmap[domain]
118 def mask_sensitive_data(self):
119 for handler in self.configmap.values():
121 handler.validate_root()
122 except configerror.ConfigError:
125 handler.mask_sensitive_data()
127 if __name__ == '__main__':
128 parser = argparse.ArgumentParser(description='Config Manager', prog=sys.argv[0])
130 parser.add_argument('--domain',
134 help='The configuration domain',
138 parser.add_argument('--json',
142 help='The json file containing the configuration',
146 parser.add_argument('--api',
150 help='The API to call in the domain',
154 parser.add_argument('--dump',
157 help='Dump the configuration',
160 args, unknownargs = parser.parse_known_args(sys.argv[1:])
161 print("args = %r" % args)
162 print("unknownargs = %r" % unknownargs)
163 f = open(args.jsonfile)
166 manager = ConfigManager(data)
168 funcname = 'get_'+args.domain+'_config_handler'
169 objfunc = getattr(manager, funcname)
171 print('Got handler for %s' % obj.get_domain())
173 domainfunc = getattr(obj, args.api)
176 result = domainfunc(*unknownargs)
178 result = domainfunc()
179 print("result is %r" % result)
183 pp = pprint.PrettyPrinter(indent=4)