Skip to content

Commit e77e89a

Browse files
yt-msMidnighter
authored andcommitted
feat: add InfrrastructureNodes to DeploymentNodes
1 parent b49abb7 commit e77e89a

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/structurizr/model/deployment_node.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ def add_software_system_instance(
201201
model += instance
202202
return instance
203203

204+
def add_infrastructure_node(self, name: str, **kwargs) -> InfrastructureNode:
205+
"""Create a new infrastructure node under this node."""
206+
infra_node = InfrastructureNode(name=name, parent=self, **kwargs)
207+
self._infrastructure_nodes.add(infra_node)
208+
model = self.model
209+
model += infra_node
210+
return infra_node
211+
204212
def __iadd__(self, node: "DeploymentNode") -> "DeploymentNode":
205213
"""Add a newly constructed chile deployment node to this node."""
206214
if node in self._children:
@@ -255,4 +263,10 @@ def hydrate(
255263
)
256264
node._software_system_instances.add(instance)
257265

266+
for infra_node_io in deployment_node_io.infrastructure_nodes:
267+
infra_node = InfrastructureNode.hydrate(
268+
infra_node_io, model=model, parent=node
269+
)
270+
node._infrastructure_nodes.add(infra_node)
271+
258272
return node

src/structurizr/model/infrastructure_node.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
if TYPE_CHECKING:
23+
from .deployment_node import DeploymentNode
2324
from .model import Model
2425

2526

@@ -55,18 +56,21 @@ def __init__(
5556
self,
5657
*,
5758
technology: str = "",
59+
parent: "DeploymentNode" = None,
5860
**kwargs,
5961
):
6062
"""Initialize an infrastructure node model."""
6163
super().__init__(**kwargs)
6264
self.technology = technology
6365
self.tags.add(Tags.INFRASTRUCTURE_NODE)
66+
self.parent = parent
6467

6568
@classmethod
6669
def hydrate(
6770
cls,
6871
node_io: InfrastructureNodeIO,
6972
model: "Model",
73+
parent: "DeploymentNode",
7074
) -> "InfrastructureNode":
7175
"""Hydrate a new InfrastructureNode instance from its IO.
7276
@@ -75,6 +79,7 @@ def hydrate(
7579
node = cls(
7680
**cls.hydrate_arguments(node_io),
7781
technology=node_io.technology,
82+
parent=parent,
7883
)
7984
model += node
8085
return node

tests/unit/model/test_deployment_node.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,35 @@ def test_deployment_node_adding_software_system_replicating_relationships(
207207
):
208208
"""Test replicating relationships when adding a software system instance."""
209209
raise AssertionError() # Not implemented yet
210+
211+
212+
def test_deployment_node_add_infrastructure_node(model_with_node):
213+
"""Test adding an infrastructure node to a deployment node."""
214+
node = model_with_node.empty_node
215+
216+
infra_node = node.add_infrastructure_node("infraNode")
217+
218+
assert infra_node.name == "infraNode"
219+
assert infra_node.model is model_with_node
220+
assert infra_node.parent is node
221+
assert infra_node in node.infrastructure_nodes
222+
223+
224+
def test_deployment_node_serialising_infrastructure_nodes(model_with_node):
225+
"""Test serialisation and deserialisation includes infrastructure nodes."""
226+
node = model_with_node.empty_node
227+
node.add_infrastructure_node("infraNode")
228+
229+
io = DeploymentNodeIO.from_orm(node)
230+
231+
assert len(io.infrastructure_nodes) == 1
232+
assert io.infrastructure_nodes[0].id == "id"
233+
assert io.infrastructure_nodes[0].name == "infraNode"
234+
235+
node2 = DeploymentNode.hydrate(io, model_with_node)
236+
237+
assert len(node2.infrastructure_nodes) == 1
238+
infra_node = node2.infrastructure_nodes[0]
239+
assert infra_node.name == "infraNode"
240+
assert infra_node.model is model_with_node
241+
assert infra_node.parent is node2

tests/unit/model/test_infrastructure_node.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ def test_infrastructure_node_tags():
6868
def test_infrastructure_node_hydration(empty_model):
6969
"""Check hydrating an infrastructure node from its IO."""
7070
io = InfrastructureNodeIO(name="node1", technology="tech")
71+
parent = object()
7172

72-
node = InfrastructureNode.hydrate(io, empty_model)
73+
node = InfrastructureNode.hydrate(io, empty_model, parent=parent)
7374

7475
assert node.name == "node1"
7576
assert node.technology == "tech"
77+
assert node.parent is parent

0 commit comments

Comments
 (0)