diff options
Diffstat (limited to 'vymgmt/router.py')
-rw-r--r-- | vymgmt/router.py | 136 |
1 files changed, 98 insertions, 38 deletions
diff --git a/vymgmt/router.py b/vymgmt/router.py index 15fea9e..6b342f7 100644 --- a/vymgmt/router.py +++ b/vymgmt/router.py @@ -1,11 +1,11 @@ # Copyright (c) 2016 Hochikong -from pxssh import pxssh +from pexpect import pxssh from .mgmt_common import messenger, committer from .base_exceptions.exceptions_for_set_and_delete import ConfigPathError, ConfigValueError from .base_exceptions.exceptions_for_commit import CommitFailed, CommitConflict -from .base_exceptions.base import CommonError +from .base_exceptions.base import CommonError, MaintenanceError from .error_distinguish import distinguish_for_set, distinguish_for_delete, distinguish_for_commit @@ -21,7 +21,7 @@ class Router(object): self.__divi = self.__cred.index(":") self.__username = ''.join(self.__cred[:self.__divi]) self.__passwd = ''.join(self.__cred[self.__divi+1:]) - self.__conn = pxssh() + self.__conn = pxssh.pxssh() self.__status = {"status": None, "commit": None, "save": None, "configure": None} self.__basic_string = {0: 'set ', 1: 'delete '} @@ -37,43 +37,54 @@ class Router(object): :return: A message or an error """ + has_error = None try: if self.__conn.login(self.__address, self.__username, self.__passwd) is True: self.__status["status"] = "login" - return "Result : Login successfully." else: - return "Error : Connect Failed." + has_error = 'Type1' except Exception as e: return e + if has_error == 'Type1': + raise MaintenanceError("Error : Connect Failed.") + def logout(self): """Logout the router :return: A message or an error """ + has_error = None try: if self.__status["commit"] == "No": - return "Error : You should commit and exit configure mode first." + has_error = 'Type1' if self.__status["save"] == "No": - return "Error : You should save and exit configure mode first." + has_error = 'Type2' if self.__status["configure"] == "Yes": - return "Error : You should exit configure mode first." + has_error = 'Type3' self.__conn.close() self.__status["status"] = "logout" self.__status["configure"] = None - self.__conn = pxssh() - return "Result : Logout successfully." + self.__conn = pxssh.pxssh() except Exception as e: return e + if has_error == 'Type1': + raise MaintenanceError("Error : You should commit and exit configure mode first.") + if has_error == 'Type2': + raise MaintenanceError("Error : You should save and exit configure mode first.") + if has_error == 'Type3': + raise MaintenanceError("Error : You should exit configure mode first.") + def configure(self): """Enter the VyOS configure mode :return: A message or an error """ + has_error = None try: if self.__status["status"] == "login": if self.__status["configure"] is not "Yes": @@ -81,40 +92,55 @@ class Router(object): self.__conn.prompt(0) self.__conn.set_unique_prompt() self.__status["configure"] = "Yes" - return "Result : Active configure mode successfully." else: - return "Error : In configure mode now!" + has_error = 'Type1' else: - return "Error : Router object not connect to a router." + has_error = 'Type2' except Exception as e: return e + if has_error == 'Type1': + raise MaintenanceError("Error : In configure mode now!") + if has_error == 'Type2': + raise MaintenanceError("Error : Router object not connect to a router.") + def commit(self): """Commit the configuration changes :return: A message or an error """ + has_error = None + result = None + res = None try: if self.__status["status"] == "login": if self.__status["configure"] == "Yes": if self.__status["commit"] is None: - return "Error : You don't need to commit." + has_error = 'Type1' if self.__status["commit"] == "No": res = committer(self.__conn, "commit") if "Result" in res: self.__status["commit"] = "Yes" - return "Result : Commit successfully." else: result = distinguish_for_commit(res) else: - return "Error : You have commit!" + has_error = 'Type2' else: - return "Error : Router not in configure mode!" + has_error = 'Type3' else: - return "Error : Router object not connect to a router." + has_error = 'Type4' except Exception as e: return e + if has_error == 'Type1': + raise MaintenanceError("Error : You don't need to commit.") + if has_error == 'Type2': + raise MaintenanceError("Error : You have commit!") + if has_error == 'Type3': + raise MaintenanceError("Error : Router not in configure mode!") + if has_error == 'Type4': + raise MaintenanceError("Error : Router object not connect to a router.") + if result == "CommitFailed": raise CommitFailed(res) elif result == "CommitConflict": @@ -125,36 +151,50 @@ class Router(object): :return: A message or an error """ + has_error = None try: if self.__status["status"] == "login": if self.__status["configure"] == "Yes": if self.__status["commit"] == "Yes": if self.__status["save"] is None: - return "Error : You don't need to save." + has_error = 'Type1' if self.__status["save"] == "No": self.__conn.sendline("save") self.__conn.prompt(0) self.__status["save"] = "Yes" - return "Result : Save successfully." else: - return "Error : You have saved!" + has_error = 'Type2' elif self.__status["commit"] is None: - return "Error : You don't need to save." + has_error = 'Type3' else: - return "Error : You need to commit first!" + has_error = 'Type4' else: - return "Error : Router not in configure mode!" + has_error = 'Type5' else: - return "Error : Router object not connect to a router." + has_error = 'Type6' except Exception as e: return e + if has_error == 'Type1': + raise MaintenanceError("Error : You don't need to save.") + if has_error == 'Type2': + raise MaintenanceError("Error : You have saved!") + if has_error == 'Type3': + raise MaintenanceError("Error : You don't need to save.") + if has_error == 'Type4': + raise MaintenanceError("Error : You need to commit first!") + if has_error == 'Type5': + raise MaintenanceError("Error : Router not in configure mode!") + if has_error == 'Type6': + raise MaintenanceError("Error : Router object not connect to a router.") + def exit(self, force=False): """Exit VyOS configure mode :param force: True or False :return: A message or an error """ + has_error = None try: if self.__status["status"] == "login": if self.__status["configure"] == "Yes": @@ -164,7 +204,6 @@ class Router(object): self.__status["configure"] = "No" self.__status["save"] = None self.__status["commit"] = None - return "Result : Exit configure mode successfully." else: if self.__status["commit"] == "Yes": if self.__status["save"] == "Yes": @@ -173,23 +212,30 @@ class Router(object): self.__status["configure"] = "No" self.__status["save"] = None self.__status["commit"] = None - return "Result : Exit configure mode successfully." else: - return "Error : You should save first." + has_error = 'Type1' elif self.__status["commit"] is None: self.__conn.sendline("exit") self.__conn.prompt() self.__status['configure'] = "No" - return "Result : Exit configure mode successfully." else: - return "Error : You should commit first." + has_error = 'Type2' else: - return "Error : You are not in configure mode,need not exit." + has_error = 'Type3' else: - return "Error : Router object not connect to a router." + has_error = 'Type4' except Exception as e: return e + if has_error == 'Type1': + raise MaintenanceError("Error : You should save first.") + if has_error == 'Type2': + raise MaintenanceError("Error : You should commit first.") + if has_error == 'Type3': + raise MaintenanceError("Error : You are not in configure mode,no need to exit.") + if has_error == 'Type4': + raise MaintenanceError("Error : Router object not connect to a router.") + def set(self, config): """Basic 'set' method,execute the set command in VyOS @@ -197,6 +243,9 @@ class Router(object): e.g. 'protocols static route ... next-hop ... distance ...' :return: A message or an error """ + has_error = None + result = None + res = None full_config = self.__basic_string[0] + config try: if self.__status["status"] == "login": @@ -211,16 +260,20 @@ class Router(object): pass else: self.__status["save"] = "No" - return res else: result = distinguish_for_set(res) else: - return "Error : You are not in configure mode." + has_error = 'Type1' else: - return "Error : Router object not connect to a router." + has_error = 'Type2' except Exception as e: return e + if has_error == 'Type1': + raise MaintenanceError("Error : You are not in configure mode.") + if has_error == 'Type2': + raise MaintenanceError("Error : Router object not connect to a router.") + if result == "ConfigPathError": raise ConfigPathError(res) elif result == "ConfigValueError": @@ -235,6 +288,9 @@ class Router(object): e.g. 'protocols static route ... next-hop ... distance ...' :return: A message or an error """ + has_error = None + result = None + res = None full_config = self.__basic_string[1] + config try: if self.__status["status"] == "login": @@ -249,16 +305,20 @@ class Router(object): pass else: self.__status["save"] = "No" - return res else: result = distinguish_for_delete(res) else: - return "Error : You are not in configure mode." + has_error = 'Type1' else: - return "Error : Router object not connect to a router." + has_error = 'Type2' except Exception as e: return e + if has_error == 'Type1': + raise MaintenanceError("Error : You are not in configure mode.") + if has_error == 'Type2': + raise MaintenanceError("Error : Router object not connect to a router.") + if result == "ConfigPathError": raise ConfigPathError(res) elif result == "ConfigValueError": |