From fa7f1140ea39bc0e96a7ce9e654ad624b4be6ca2 Mon Sep 17 00:00:00 2001 From: Justin Drew <2396364+jdrew82@users.noreply.github.com> Date: Tue, 19 Mar 2024 14:36:04 -0500 Subject: [PATCH] refactor: Re-raise Exception instead of raising Nornir exception --- .../plugins/tasks/dispatcher/default.py | 84 +++++++++++++------ 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/nornir_nautobot/plugins/tasks/dispatcher/default.py b/nornir_nautobot/plugins/tasks/dispatcher/default.py index 44b4747..2ef72ab 100644 --- a/nornir_nautobot/plugins/tasks/dispatcher/default.py +++ b/nornir_nautobot/plugins/tasks/dispatcher/default.py @@ -66,9 +66,7 @@ def check_connectivity(cls, task: Task, logger, obj) -> Result: ip_addr = hostname else: if not is_fqdn_resolvable(hostname): - error_msg = ( - f"`E1003:` The hostname {hostname} did not have an IP nor was resolvable, preemptively failed." - ) + error_msg = f"`E1003:` The hostname {hostname} did not have an IP nor was resolvable, preemptively failed." logger.error(error_msg, extra={"object": obj}) raise NornirNautobotException(error_msg) ip_addr = socket.gethostbyname(hostname) @@ -175,28 +173,34 @@ def generate_config( )[0].result except NornirSubTaskError as exc: if isinstance(exc.result.exception, jinja2.exceptions.UndefinedError): # pylint: disable=no-else-raise - error_msg = ( - f"`E1010:` There was a jinja2.exceptions.UndefinedError error: ``{str(exc.result.exception)}``" - ) + error_msg = f"`E1010:` There was a jinja2.exceptions.UndefinedError error: ``{str(exc.result.exception)}``" logger.error(error_msg, extra={"object": obj}) - raise NornirNautobotException(error_msg) + # raise NornirNautobotException(error_msg) + raise exc.result.exception elif isinstance(exc.result.exception, jinja2.TemplateSyntaxError): - error_msg = (f"`E1011:` There was a jinja2.TemplateSyntaxError error: ``{str(exc.result.exception)}``",) + error_msg = ( + f"`E1011:` There was a jinja2.TemplateSyntaxError error: ``{str(exc.result.exception)}``", + ) logger.error(error_msg, extra={"object": obj}) - raise NornirNautobotException(error_msg) + # raise NornirNautobotException(error_msg) + raise exc.result.exception elif isinstance(exc.result.exception, jinja2.TemplateNotFound): error_msg = f"`E1012:` There was an issue finding the template and a jinja2.TemplateNotFound error was raised: ``{str(exc.result.exception)}``" logger.error(error_msg, extra={"object": obj}) - raise NornirNautobotException(error_msg) + # raise NornirNautobotException(error_msg) + raise exc.result.exception elif isinstance(exc.result.exception, jinja2.TemplateError): error_msg = f"`E1013:` There was an issue general Jinja error: ``{str(exc.result.exception)}``" logger.error(error_msg, extra={"object": obj}) - raise NornirNautobotException(error_msg) + # raise NornirNautobotException(error_msg) + raise exc.result.exception - error_msg = f"`E1014:` Failed with an unknown issue. `{exc.result.exception}`" + error_msg = ( + f"`E1014:` Failed with an unknown issue. `{exc.result.exception}`" + ) logger.error(error_msg, extra={"object": obj}) raise NornirNautobotException(error_msg) @@ -219,11 +223,15 @@ def _remove_lines(cls, logger, _running_config: str, remove_lines: list) -> str: """ if not remove_lines: return _running_config - logger.debug("Removing lines from configuration based on `remove_lines` definition") + logger.debug( + "Removing lines from configuration based on `remove_lines` definition" + ) return clean_config(_running_config, remove_lines) @classmethod - def _substitute_lines(cls, logger, _running_config: str, substitute_lines: list) -> str: + def _substitute_lines( + cls, logger, _running_config: str, substitute_lines: list + ) -> str: """Substitutes lines in configuration as specified in substitute Lines list. Args: @@ -236,7 +244,9 @@ def _substitute_lines(cls, logger, _running_config: str, substitute_lines: list) """ if not substitute_lines: return _running_config - logger.debug("Substitute lines from configuration based on `substitute_lines` definition") + logger.debug( + "Substitute lines from configuration based on `substitute_lines` definition" + ) return sanitize_config(_running_config, substitute_lines) @classmethod @@ -284,7 +294,9 @@ def get_config( Result: Nornir Result object with a dict as a result containing the running configuration { "config: } """ - logger.debug(f"Executing get_config for {task.host.name} on {task.host.platform}") + logger.debug( + f"Executing get_config for {task.host.name} on {task.host.platform}" + ) # TODO: Find standard napalm exceptions and account for them try: @@ -304,11 +316,15 @@ def get_config( running_config = result[0].result.get("config", {}).get("running", None) if remove_lines: - logger.debug("Removing lines from configuration based on `remove_lines` definition") + logger.debug( + "Removing lines from configuration based on `remove_lines` definition" + ) running_config = clean_config(running_config, remove_lines) if substitute_lines: - logger.debug("Substitute lines from configuration based on `substitute_lines` definition") + logger.debug( + "Substitute lines from configuration based on `substitute_lines` definition" + ) running_config = sanitize_config(running_config, substitute_lines) if backup_file: @@ -356,7 +372,9 @@ def replace_config( revert_in=revert_in, ) except NornirSubTaskError as exc: - error_msg = f"`E1015:` Failed with an unknown issue. `{exc.result.exception}`" + error_msg = ( + f"`E1015:` Failed with an unknown issue. `{exc.result.exception}`" + ) logger.error(error_msg, extra={"object": obj}) raise NornirNautobotException(error_msg) @@ -408,7 +426,9 @@ def merge_config( revert_in=revert_in, ) except NornirSubTaskError as exc: - error_msg = f"`E1015:` Failed with an unknown issue. `{exc.result.exception}`" + error_msg = ( + f"`E1015:` Failed with an unknown issue. `{exc.result.exception}`" + ) logger.error(error_msg, extra={"object": obj}) raise NornirNautobotException(error_msg) @@ -455,7 +475,9 @@ def get_config( Result: Nornir Result object with a dict as a result containing the running configuration { "config: } """ - logger.debug(f"Executing get_config for {task.host.name} on {task.host.platform}") + logger.debug( + f"Executing get_config for {task.host.name} on {task.host.platform}" + ) command = cls.config_command try: @@ -467,11 +489,15 @@ def get_config( raise NornirNautobotException(error_msg) if isinstance(exc.result.exception, NetmikoTimeoutException): - error_msg = f"`E1018:` Failed with a timeout issue. `{exc.result.exception}`" + error_msg = ( + f"`E1018:` Failed with a timeout issue. `{exc.result.exception}`" + ) logger.error(error_msg, extra={"object": obj}) raise NornirNautobotException(error_msg) - error_msg = f"`E1016:` Failed with an unknown issue. `{exc.result.exception}`" + error_msg = ( + f"`E1016:` Failed with an unknown issue. `{exc.result.exception}`" + ) logger.error(error_msg, extra={"object": obj}) raise NornirNautobotException(error_msg) @@ -482,15 +508,21 @@ def get_config( # Primarily seen in Cisco devices. if "ERROR: % Invalid input detected at" in running_config: - error_msg = "`E1019:` Discovered `ERROR: % Invalid input detected at` in the output" + error_msg = ( + "`E1019:` Discovered `ERROR: % Invalid input detected at` in the output" + ) logger.error(error_msg, extra={"object": obj}) raise NornirNautobotException(error_msg) if remove_lines: - logger.debug("Removing lines from configuration based on `remove_lines` definition") + logger.debug( + "Removing lines from configuration based on `remove_lines` definition" + ) running_config = clean_config(running_config, remove_lines) if substitute_lines: - logger.debug("Substitute lines from configuration based on `substitute_lines` definition") + logger.debug( + "Substitute lines from configuration based on `substitute_lines` definition" + ) running_config = sanitize_config(running_config, substitute_lines) if backup_file: