Skip to content

Commit

Permalink
refactor: Re-raise Exception instead of raising Nornir exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrew82 committed Mar 19, 2024
1 parent bab83ce commit fa7f114
Showing 1 changed file with 58 additions and 26 deletions.
84 changes: 58 additions & 26 deletions nornir_nautobot/plugins/tasks/dispatcher/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -284,7 +294,9 @@ def get_config(
Result: Nornir Result object with a dict as a result containing the running configuration
{ "config: <running configuration> }
"""
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:
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -455,7 +475,9 @@ def get_config(
Result: Nornir Result object with a dict as a result containing the running configuration
{ "config: <running configuration> }
"""
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:
Expand All @@ -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)

Expand All @@ -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:
Expand Down

0 comments on commit fa7f114

Please sign in to comment.