diff --git a/redfish-client/redfish-client.py b/redfish-client/redfish-client.py old mode 100644 new mode 100755 index a6f9398..c1be6b6 --- a/redfish-client/redfish-client.py +++ b/redfish-client/redfish-client.py @@ -1,3 +1,5 @@ +#!/usr/bin/python + # coding=utf-8 """ @@ -19,7 +21,7 @@ Options: --conf_file FILE Configuration file [default: ~/.redfish.conf]. -config commands manage the configuration file. +config commands manage the configuration file. """ @@ -32,17 +34,65 @@ import docopt class ConfigFile(object): def __init__(self, config_file): + self._config_file = config_file # read json file - try: - with open(config_file) as json_data: + try: + with open(self._config_file) as json_data: self.data = json.load(json_data) json_data.close() - except IOError as e: - self.data = {"Nodes":{}} + except (ValueError, IOError): + self.data = {"Managers":{}} + + def save(self): + try: + with open(self._config_file , 'w') as json_data: + json.dump(self.data, json_data) + json_data.close() + except IOError as e: + print(e.msg) + sys.exit(1) + + def add_manager(self, manager_name, url, login, password): + self.data['Managers'][manager_name] = {} + self.data['Managers'][manager_name]['url'] = url + if login != None: + self.data['Managers'][manager_name]['login'] = login + if password != None: + self.data['Managers'][manager_name]['password'] = password + + def get_managers(self): + managers = [] + for manager in self.data['Managers']: + managers += [manager] + return(managers) + + def get_manager_info(self, manager): + info = {} + url=self.data['Managers'][manager]['url'] + login=self.data['Managers'][manager]['login'] + password=self.data['Managers'][manager]['password'] + info={'url':url, 'login':login, 'password':password} + return(info) + +class RedfishClientException(Exception): + """Base class for redfish client exceptions""" + def __init__(self, message=None, **kwargs): + self.kwargs = kwargs + self.message = message - if __name__ == '__main__': + # Functions + def show_manager(all=False): + print("Managers configured :") + for manager in conf_file.get_managers(): + print(manager) + if all == True: + info = conf_file.get_manager_info(manager) + print("\tUrl : {}".format(info['url'])) + print("\tLogin : {}".format(info['login'])) + print("\tPassword : {}".format(info['password'])) + # Get $HOME environment. HOME = os.getenv('HOME') @@ -52,22 +102,25 @@ if __name__ == '__main__': arguments = docopt.docopt(__doc__, version='redfish-client 0.1') print(arguments) - + arguments['--conf_file'] = arguments['--conf_file'].replace('~', HOME) - + conf_file = ConfigFile(arguments['--conf_file']) - - + + if arguments['config'] == True: - if arguments['showall'] == True: - pprint.pprint(conf_file.data) + if arguments['show'] == True: + show_manager() + elif arguments['showall'] == True: + show_manager(True) elif arguments['add'] == True: - conf_file.data['Nodes'][arguments['']] = {} - conf_file.data['Nodes'][arguments['']]['url'] = arguments[''] - if arguments[''] != None: - conf_file.data['Nodes'][arguments['']]['login'] = arguments[''] - if arguments[''] != None: - conf_file.data['Nodes'][arguments['']]['password'] = arguments[''] + conf_file.add_manager(arguments[''], + arguments[''], + arguments[''], + arguments['password']) pprint.pprint(conf_file.data) - - \ No newline at end of file + + conf_file.save() + + + sys.exit(0)