diff options
author | hochikong <1097225749@qq.com> | 2016-08-24 14:41:35 +0800 |
---|---|---|
committer | hochikong <1097225749@qq.com> | 2016-08-24 14:41:35 +0800 |
commit | 5c7c56264681faa44b2bca036973b08cecf168ae (patch) | |
tree | e8c917d971e8585924515612d068eb672c4d2b9c | |
parent | 2528aa7872958c8e4507d8f42ba0fe767989b19b (diff) | |
download | python-vyos-mgmt-5c7c56264681faa44b2bca036973b08cecf168ae.tar.gz python-vyos-mgmt-5c7c56264681faa44b2bca036973b08cecf168ae.zip |
T133 Add a generic method set() and delete()
Fix all problems from now on
-rw-r--r-- | vymgmt/base_exception/__init__.py | 0 | ||||
-rw-r--r-- | vymgmt/base_exception/base.py | 15 | ||||
-rw-r--r-- | vymgmt/base_exception/exception_for_commit.py | 21 | ||||
-rw-r--r-- | vymgmt/base_exception/exceptions_for_set_and_delete.py | 22 | ||||
-rw-r--r-- | vymgmt/error_distinguish.py | 64 | ||||
-rw-r--r-- | vymgmt/mgmt_common.py | 18 | ||||
-rw-r--r-- | vymgmt/router.py (renamed from vymgmt/Router.py) | 42 |
7 files changed, 174 insertions, 8 deletions
diff --git a/vymgmt/base_exception/__init__.py b/vymgmt/base_exception/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/vymgmt/base_exception/__init__.py diff --git a/vymgmt/base_exception/base.py b/vymgmt/base_exception/base.py new file mode 100644 index 0000000..dc80840 --- /dev/null +++ b/vymgmt/base_exception/base.py @@ -0,0 +1,15 @@ +# Copyright (c) 2016 Hochikong + +prefix = "\r\n" + + +class Error(Exception): + pass + + +class CommonError(Error): + def __init__(self, message): + self.error_message = message + + def __str__(self): + return prefix + self.error_message diff --git a/vymgmt/base_exception/exception_for_commit.py b/vymgmt/base_exception/exception_for_commit.py new file mode 100644 index 0000000..26c14c4 --- /dev/null +++ b/vymgmt/base_exception/exception_for_commit.py @@ -0,0 +1,21 @@ +# Copyright (c) 2016 Hochikong + +from .base import Error + +prefix = "\r\n" + + +class CommitFailed(Error): + def __init__(self, message): + self.error_message = message + + def __str__(self): + return prefix + self.error_message + + +class CommitConflict(Error): + def __init__(self, message): + self.error_message = message + + def __str__(self): + return prefix + self.error_message diff --git a/vymgmt/base_exception/exceptions_for_set_and_delete.py b/vymgmt/base_exception/exceptions_for_set_and_delete.py new file mode 100644 index 0000000..0f65c88 --- /dev/null +++ b/vymgmt/base_exception/exceptions_for_set_and_delete.py @@ -0,0 +1,22 @@ +# Copyright (c) 2016 Hochikong + + +from .base import Error + +prefix = "\r\n" + + +class ConfigPathError(Error): + def __init__(self, message): + self.error_message = message + + def __str__(self): + return prefix + self.error_message + + +class ConfigValueError(Error): + def __init__(self, message): + self.error_message = message + + def __str__(self): + return prefix + self.error_message diff --git a/vymgmt/error_distinguish.py b/vymgmt/error_distinguish.py new file mode 100644 index 0000000..d58db2f --- /dev/null +++ b/vymgmt/error_distinguish.py @@ -0,0 +1,64 @@ +# Copyright (c) 2016 Hochikong + + +def distinguish_for_set(message): + """Distinguish the error type,PathError or ValueError + + :param message: A error message string from VyOS + :return: The type of error + """ + path_error_string = ['Configuration path:', 'is not valid'] + value_error_string = ['Value validation failed'] + all_strings = [path_error_string, value_error_string] + condition = 0 + for i in all_strings: + for x in i: + if x in message: + condition += 1 + + if condition == 2: + return "ConfigPathError" + elif condition == 1: + return "ConfigValueError" + else: + return "NonsupportButError" + + +def distinguish_for_delete(message): + """Distinguish the error type,PathError or ValueError + + :param message: A error message string from VyOS + :return: The type of error + """ + path_error_string = ['Configuration path:', 'is not valid', 'Delete failed'] + value_error_string = ['Nothing to delete', 'the specified value does not exist'] + all_strings = [path_error_string, value_error_string] + condition = 0 + + for i in all_strings: + for x in i: + if x in message: + condition += 1 + + if condition == 3: + return "ConfigPathError" + elif condition == 2: + return "ConfigValueError" + else: + return "NonsupportButError" + + +def distinguish_for_commit(message): + """Distinguish the error type + + :param message: A error message string from VyOS + :return: The type of error + """ + all_strings = ['Commit failed', 'due to another commit in progress'] + + for i in all_strings: + if i in message: + if i == all_strings[0]: + return "CommitFailed" + if i == all_strings[1]: + return "CommitConflict" diff --git a/vymgmt/mgmt_common.py b/vymgmt/mgmt_common.py index 0ed4bf0..1ccb96a 100644 --- a/vymgmt/mgmt_common.py +++ b/vymgmt/mgmt_common.py @@ -17,3 +17,21 @@ def messenger(obj, config): return "Result : Configured successfully" except Exception as e: return e + + +def committer(obj, config): + """This method used for sending commit task to VyOS + + :param obj: A connection object + :param config: A configuration string + :return: A message or an error + """ + try: + obj.sendline(config) + obj.prompt() + if len(obj.before) > obj.before.index('\r\n') + 2: + return obj.before + else: + return "Result : Commit successfully" + except Exception as e: + return e diff --git a/vymgmt/Router.py b/vymgmt/router.py index 326e025..955a9ba 100644 --- a/vymgmt/Router.py +++ b/vymgmt/router.py @@ -1,7 +1,12 @@ # Copyright (c) 2016 Hochikong + from pxssh import pxssh -from .mgmt_common import messenger +from mgmt_common import messenger, committer +from .base_exception.exceptions_for_set_and_delete import ConfigPathError, ConfigValueError +from .base_exception.exception_for_commit import CommitFailed, CommitConflict +from .base_exception.base import CommonError +from .error_distinguish import distinguish_for_set, distinguish_for_delete, distinguish_for_commit class Router(object): @@ -86,12 +91,14 @@ class Router(object): if self.__status["commit"] is None: return "Error : You don't need to commit." if self.__status["commit"] == "No": - self.__conn.sendline("commit") - self.__conn.prompt() - self.__status["commit"] = "Yes" - return "Result : Commit successfully." + 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 committed!" + return "Error : You have commit!" else: return "Error : Router not in configure mode!" else: @@ -99,6 +106,11 @@ class Router(object): except Exception as e: return e + if result == "CommitFailed": + raise CommitFailed(res) + elif result == "CommitConflict": + raise CommitConflict(res) + def save(self): """Save the configuration after commit @@ -192,7 +204,7 @@ class Router(object): self.__status["save"] = "No" return res else: - return res + result = distinguish_for_set(res) else: return "Error : You are not in configure mode." else: @@ -200,6 +212,13 @@ class Router(object): except Exception as e: return e + if result == "ConfigPathError": + raise ConfigPathError(res) + elif result == "ConfigValueError": + raise ConfigValueError(res) + elif result == "NonsupportButError": + raise CommonError(res) + def delete(self, config): """Basic 'delete' method,execute the delete command in VyOS @@ -223,10 +242,17 @@ class Router(object): self.__status["save"] = "No" return res else: - return res + result = distinguish_for_delete(res) else: return "Error : You are not in configure mode." else: return "Error : Router object not connect to a router." except Exception as e: return e + + if result == "ConfigPathError": + raise ConfigPathError(res) + elif result == "ConfigValueError": + raise ConfigValueError(res) + elif result == "NonsupportButError": + raise CommonError(res) |