Skip to content

Commit

Permalink
set the parent when adding nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
haidaraM committed Jan 24, 2025
1 parent 0853c51 commit 09488bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
4 changes: 3 additions & 1 deletion ansibleplaybookgrapher/graph_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def add_node(self, target_composition: str, node: Node) -> None:
:return:
"""
self._check_target_composition(target_composition)
node.parent = self
self._compositions[target_composition].append(node)

def remove_node(self, target_composition: str, node: Node) -> None:
Expand All @@ -246,6 +247,7 @@ def remove_node(self, target_composition: str, node: Node) -> None:
:return:
"""
self._check_target_composition(target_composition)
node.parent = None
self._compositions[target_composition].remove(node)

def calculate_indices(self) -> None:
Expand Down Expand Up @@ -949,7 +951,7 @@ def _get_all_links(self, links: dict[Node, list[Node]]) -> None:
"""
play = self.get_first_parent_matching_type(PlayNode)
if not play:
raise ValueError("The role must be a child of a play node.")
raise ValueError(f"The role '{self}' must be a child of a play node.")

for nodes in list(self._compositions.values()):
for node in nodes:
Expand Down
5 changes: 0 additions & 5 deletions ansibleplaybookgrapher/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def _add_task(
"node_id": generate_id(f"{node_type}_"),
"when": convert_when_to_str(task.when),
"raw_object": task,
"parent": parent_node,
"notify": _get_notified_handlers(task),
}

Expand Down Expand Up @@ -258,7 +257,6 @@ def parse(self, *args, **kwargs) -> PlaybookNode:
play_name,
hosts=play_hosts,
raw_object=play,
parent=playbook_root_node,
)
playbook_root_node.add_node("plays", play_node)

Expand Down Expand Up @@ -296,7 +294,6 @@ def parse(self, *args, **kwargs) -> PlaybookNode:
clean_name(role.get_name()),
node_id=role_node_id,
raw_object=role,
parent=play_node,
)
# edge from play to role
play_node.add_node("roles", role_node)
Expand Down Expand Up @@ -396,7 +393,6 @@ def _include_tasks_in_blocks(
str(block.name),
when=convert_when_to_str(block.when),
raw_object=block,
parent=parent_nodes[-1],
)
parent_nodes[-1].add_node(f"{node_type}s", block_node)
parent_nodes.append(block_node)
Expand Down Expand Up @@ -461,7 +457,6 @@ def _include_tasks_in_blocks(
node_id=role_node_id,
when=convert_when_to_str(task_or_block.when),
raw_object=task_or_block,
parent=parent_nodes[-1],
include_role=True,
)
parent_nodes[-1].add_node(
Expand Down
19 changes: 10 additions & 9 deletions tests/test_graph_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def test_links_structure() -> None:
play = PlayNode("composite_node")

# play -> role -> task 1 and 2
role = RoleNode("my_role_1", parent=play)
role = RoleNode("my_role_1")
play.add_node("roles", role)
task_1 = TaskNode("task 1", parent=role)
task_1 = TaskNode("task 1")
role.add_node("tasks", task_1)
task_2 = TaskNode("task 2", parent=role)
task_2 = TaskNode("task 2")
role.add_node("tasks", task_2)

# play -> task 3
task_3 = TaskNode("task 3", parent=play)
task_3 = TaskNode("task 3")
play.add_node("tasks", task_3)

all_links = play.links_structure()
Expand All @@ -45,15 +45,15 @@ def test_links_structure_with_handlers() -> None:
:return:
"""
play = PlayNode("composite_node")
play.add_node("handlers", HandlerNode("handler 1", parent=play))
play.add_node("handlers", HandlerNode("handler 2", parent=play))
play.add_node("handlers", HandlerNode("handler 1"))
play.add_node("handlers", HandlerNode("handler 2"))
play.add_node(
"handlers",
HandlerNode("handler 3", listen=["topic"], notify=["handler 1"], parent=play),
HandlerNode("handler 3", listen=["topic"], notify=["handler 1"]),
)

# play -> role -> task 1 and 2
role = RoleNode("my_role_1", parent=play)
role = RoleNode("my_role_1")
play.add_node("roles", role)
# task 1 -> handler 1
task_1 = TaskNode("task 1", notify=["handler 1"])
Expand All @@ -63,7 +63,7 @@ def test_links_structure_with_handlers() -> None:
role.add_node("tasks", task_2)

# play -> task 3 -> handler 3 via the listen 'topic'
task_3 = TaskNode("task 3", notify=["topic"], parent=play)
task_3 = TaskNode("task 3", notify=["topic"])
play.add_node("tasks", task_3)

all_links = play.links_structure()
Expand Down Expand Up @@ -110,6 +110,7 @@ def test_empty_play_method() -> None:
play.add_node("tasks", task)
assert not play.is_empty(), "The play should not be empty here"
play.remove_node("tasks", task)
assert task.parent is None, "The task should not have a parent anymore"
assert play.is_empty(), "The play should be empty again"

role.add_node("tasks", TaskNode("task 1"))
Expand Down

0 comments on commit 09488bc

Please sign in to comment.