Skip to content

Commit

Permalink
Merge pull request #143 from aedwardstx/fix_parent_association
Browse files Browse the repository at this point in the history
Fix parent association
  • Loading branch information
aedwardstx authored Dec 19, 2024
2 parents 7ab373a + 8f4e803 commit 7feffeb
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 136 deletions.
8 changes: 4 additions & 4 deletions hier_config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ def add_child(

if check_if_present and (child := self.children.get(text)):
if self._is_duplicate_child_allowed():
new_child = self._instantiate_child(text)
new_child = self.instantiate_child(text)
self.children.append(new_child, update_mapping=False)
return new_child
if return_if_present:
return child
message = f"Found a duplicate section: {(*self.path(), text)}"
raise DuplicateChildError(message)

new_child = self._instantiate_child(text)
new_child = self.instantiate_child(text)
self.children.append(new_child)
return new_child

Expand Down Expand Up @@ -334,7 +334,7 @@ def _future( # noqa: C901
future_config.add_deep_copy_of(self_child)

@abstractmethod
def _instantiate_child(self, text: str) -> HConfigChild:
def instantiate_child(self, text: str) -> HConfigChild:
pass

@abstractmethod
Expand Down Expand Up @@ -462,7 +462,7 @@ def _config_to_get_to_right(
continue
# This creates a new HConfigChild object just in case there are some delta children.
# This is not very efficient, think of a way to not do this.
subtree = self._instantiate_child(target_child.text)
subtree = delta.instantiate_child(target_child.text)
self_child._config_to_get_to(target_child, subtree) # noqa: SLF001
if subtree.children:
delta.children.append(subtree)
Expand Down
2 changes: 1 addition & 1 deletion hier_config/child.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def _default(self) -> HConfigChild:
self.text = f"default {self.text_without_negation}"
return self

def _instantiate_child(self, text: str) -> HConfigChild:
def instantiate_child(self, text: str) -> HConfigChild:
return HConfigChild(self, text)

def _is_duplicate_child_allowed(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion hier_config/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def is_branch(self) -> bool:
"""Returns True if there are children or is an instance of HConfig."""
return True

def _instantiate_child(self, text: str) -> HConfigChild:
def instantiate_child(self, text: str) -> HConfigChild:
return HConfigChild(self, text)

@property
Expand Down
256 changes: 128 additions & 128 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hier-config"
version = "3.2.0"
version = "3.2.1"
description = "A network configuration query and comparison library, used to build remediation configurations."
packages = [
{ include="hier_config", from="."},
Expand Down
20 changes: 20 additions & 0 deletions tests/test_hier_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,3 +692,23 @@ def test_overwrite_with_no_negate() -> None:
generated_config.children["route-policy test"], delta_config, negate=False
)
assert delta_config == expected_config


def test_config_to_get_to_parent_identity() -> None:
interface_vlan2 = "interface Vlan2"
platform = Platform.CISCO_IOS
running_config_hier = get_hconfig(platform)
running_config_hier.add_children_deep(
(interface_vlan2, "ip address 192.168.1.1/24")
)
generated_config_hier = get_hconfig(platform)
generated_config_hier.add_child(interface_vlan2)
remediation_config_hier = running_config_hier.config_to_get_to(
generated_config_hier,
)
remediation_config_interface = remediation_config_hier.get_child(
equals=interface_vlan2
)
assert remediation_config_interface
assert id(remediation_config_interface.parent) == id(remediation_config_hier)
assert id(remediation_config_interface.root) == id(remediation_config_hier)
2 changes: 1 addition & 1 deletion tests/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_remediation_config_driver_mismatch() -> None:
generated_config = get_hconfig(Platform.JUNIPER_JUNOS, "dummy_config")

with pytest.raises(
ValueError, match="The running and generated configs must use the same driver."
ValueError, match=r"The running and generated configs must use the same driver."
):
WorkflowRemediation(running_config, generated_config)

Expand Down

0 comments on commit 7feffeb

Please sign in to comment.