Skip to content

Commit

Permalink
Update default.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkala authored Sep 29, 2023
1 parent 85684c2 commit f6a8d05
Showing 1 changed file with 79 additions and 26 deletions.
105 changes: 79 additions & 26 deletions nornir_nautobot/plugins/tasks/dispatcher/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,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 @@ -94,7 +92,14 @@ def check_connectivity(cls, task: Task, logger, obj) -> Result:

@classmethod
def compliance_config(
cls, task: Task, logger, obj, features: str, backup_file: str, intended_file: str, platform: str
cls,
task: Task,
logger,
obj,
features: str,
backup_file: str,
intended_file: str,
platform: str,
) -> Result:
"""Compare two configurations against each other.
Expand Down Expand Up @@ -165,15 +170,17 @@ def generate_config(
jinja_env=jinja_env,
)[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)}``"
)
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)}``"
logger.error(error_msg, extra={"object": obj})
raise NornirNautobotException(error_msg)

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)

Expand Down Expand Up @@ -210,11 +217,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 @@ -227,7 +238,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 All @@ -253,7 +266,13 @@ class NapalmDefault(DispatcherMixin):

@classmethod
def get_config(
cls, task: Task, logger, obj, backup_file: str, remove_lines: list, substitute_lines: list
cls,
task: Task,
logger,
obj,
backup_file: str,
remove_lines: list,
substitute_lines: list,
) -> Result:
"""Get the latest configuration from the device.
Expand All @@ -269,7 +288,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 @@ -289,11 +310,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 @@ -345,9 +370,15 @@ def replace_config(
logger.error(error_msg, extra={"object": obj})
raise NornirNautobotException(error_msg)

logger.info(f"result: {push_result[0].result}, changed: {push_result.changed}", extra={"object": obj})
logger.info(
f"result: {push_result[0].result}, changed: {push_result.changed}",
extra={"object": obj},
)
logger.info("Config provision ended", extra={"object": obj})
return Result(host=task.host, result={"changed": push_result.changed, "result": push_result[0].result})
return Result(
host=task.host,
result={"changed": push_result.changed, "result": push_result[0].result},
)

@classmethod
def merge_config(
Expand Down Expand Up @@ -391,9 +422,15 @@ def merge_config(
logger.error(error_msg, extra={"object": obj})
raise NornirNautobotException(error_msg)

logger.info(f"result: {push_result[0].result}, changed: {push_result.changed}", extra={"object": obj})
logger.info(
f"result: {push_result[0].result}, changed: {push_result.changed}",
extra={"object": obj},
)
logger.info("Config merge ended", extra={"object": obj})
return Result(host=task.host, result={"changed": push_result.changed, "result": push_result[0].result})
return Result(
host=task.host,
result={"changed": push_result.changed, "result": push_result[0].result},
)


class NetmikoDefault(DispatcherMixin):
Expand All @@ -403,7 +440,13 @@ class NetmikoDefault(DispatcherMixin):

@classmethod
def get_config(
cls, task: Task, logger, obj, backup_file: str, remove_lines: list, substitute_lines: list
cls,
task: Task,
logger,
obj,
backup_file: str,
remove_lines: list,
substitute_lines: list,
) -> Result:
"""Get the latest configuration from the device using Netmiko.
Expand All @@ -418,7 +461,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 @@ -430,7 +475,9 @@ 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)

Expand All @@ -445,15 +492,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 f6a8d05

Please sign in to comment.