Skip to content

Commit

Permalink
Handle exceptions when calculating missing/extra commands in Configur…
Browse files Browse the repository at this point in the history
…ator and improve error logging
  • Loading branch information
miaow2 committed Nov 9, 2024
1 parent 9012a14 commit 2d974b1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 9 additions & 6 deletions netbox_config_diff/configurator/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,15 @@ async def _collect_one_diff(self, device: ConfiguratorDeviceDataClass) -> None:

device.diff = get_unified_diff(device.rendered_config, device.actual_config, device.name)
self.logger.add_diff(device.name, diff=device.diff)
device.missing = diff_network_config(
device.rendered_config, device.actual_config, PLATFORM_MAPPING[device.platform]
)
device.extra = diff_network_config(
device.actual_config, device.rendered_config, PLATFORM_MAPPING[device.platform]
)
try:
device.missing = diff_network_config(
device.rendered_config, device.actual_config, PLATFORM_MAPPING[device.platform]
)
device.extra = diff_network_config(
device.actual_config, device.rendered_config, PLATFORM_MAPPING[device.platform]
)
except Exception as e:
self.logger.log_warning(f"Unable to get missing/extra commands for {device.name}: {e}")
device.patch = get_remediation_commands(
device.name, device.platform, device.actual_config, device.rendered_config
)
Expand Down
6 changes: 3 additions & 3 deletions netbox_config_diff/configurator/platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def _render_substituted_config(

replace_sections = [(name, re.search(pattern=pattern, string=source_config)) for name, pattern in substitutes]

rendered_config = ""
rendered_config = config_template
for name, replace_section in replace_sections:
if not replace_section:
msg = f"substitution pattern {name} was unable to find a match in the target config" " source"
msg = f"substitution pattern {name} was unable to find a match in the target configsource"
self.logger.critical(msg)
raise TemplateError(msg)

replace_group = replace_section.group()
rendered_config = config_template.replace(f"{{{{ {name} }}}}", replace_group)
rendered_config = rendered_config.replace(f"{{{{ {name} }}}}", replace_group)

# remove any totally empty lines (from bad regex, or just device spitting out lines w/
# nothing on it
Expand Down

0 comments on commit 2d974b1

Please sign in to comment.