Skip to content

Commit 86c5d9a

Browse files
authored
Closes #81: Handle exceptions when calculating missing/extra commands in Configurator and improve error logging (#88)
1 parent 6cdf1eb commit 86c5d9a

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

netbox_config_diff/configurator/base.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,15 @@ async def _collect_one_diff(self, device: ConfiguratorDeviceDataClass) -> None:
137137

138138
device.diff = get_unified_diff(device.rendered_config, device.actual_config, device.name)
139139
self.logger.add_diff(device.name, diff=device.diff)
140-
device.missing = diff_network_config(
141-
device.rendered_config, device.actual_config, PLATFORM_MAPPING[device.platform]
142-
)
143-
device.extra = diff_network_config(
144-
device.actual_config, device.rendered_config, PLATFORM_MAPPING[device.platform]
145-
)
140+
try:
141+
device.missing = diff_network_config(
142+
device.rendered_config, device.actual_config, PLATFORM_MAPPING[device.platform]
143+
)
144+
device.extra = diff_network_config(
145+
device.actual_config, device.rendered_config, PLATFORM_MAPPING[device.platform]
146+
)
147+
except Exception as e:
148+
self.logger.log_warning(f"Unable to get missing/extra commands for {device.name}: {e}")
146149
device.patch = get_remediation_commands(
147150
device.name, device.platform, device.actual_config, device.rendered_config
148151
)

netbox_config_diff/configurator/platforms.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ def _render_substituted_config(
4646

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

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

5656
replace_group = replace_section.group()
57-
rendered_config = config_template.replace(f"{{{{ {name} }}}}", replace_group)
57+
rendered_config = rendered_config.replace(f"{{{{ {name} }}}}", replace_group)
5858

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

0 commit comments

Comments
 (0)