Skip to content

Commit fc476e6

Browse files
yt-msMidnighter
authored andcommitted
feat(DeploymentNode): support adding infra nodes with +=
1 parent a59cb70 commit fc476e6

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/structurizr/model/deployment_node.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,23 +226,30 @@ def add_software_system(
226226
def add_infrastructure_node(self, name: str, **kwargs) -> InfrastructureNode:
227227
"""Create a new infrastructure node under this node."""
228228
infra_node = InfrastructureNode(name=name, parent=self, **kwargs)
229-
self._infrastructure_nodes.add(infra_node)
230-
model = self.model
231-
model += infra_node
229+
self._add_infrastructure_node(infra_node)
232230
return infra_node
233231

234232
def __iadd__(
235-
self, child: Union["DeploymentNode", Container, SoftwareSystem]
233+
self,
234+
child: Union[Container, "DeploymentNode", InfrastructureNode, SoftwareSystem],
236235
) -> "DeploymentNode":
237-
"""Add a sub-node, container or system to this node."""
236+
"""Add a sub-node, container, system or infra node to this node."""
238237
if isinstance(child, SoftwareSystem):
239238
self.add_software_system(child)
240239
elif isinstance(child, Container):
241240
self.add_container(child)
241+
elif isinstance(child, InfrastructureNode):
242+
self._add_infrastructure_node(child)
242243
else:
243244
self._add_child_deployment_node(child)
244245
return self
245246

247+
def _add_infrastructure_node(self, infra_node: InfrastructureNode):
248+
"""Add a new infrastructure node."""
249+
self._infrastructure_nodes.add(infra_node)
250+
model = self.model
251+
model += infra_node
252+
246253
def _add_child_deployment_node(self, node: "DeploymentNode"):
247254
"""Add a newly constructed child deployment node to this node."""
248255
if node in self._children:

tests/unit/model/test_deployment_node.py

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

1919
from structurizr.model import Container, SoftwareSystem
2020
from structurizr.model.deployment_node import DeploymentNode, DeploymentNodeIO
21+
from structurizr.model.infrastructure_node import InfrastructureNode
2122

2223

2324
class MockModel:
@@ -153,6 +154,7 @@ def test_deployment_node_add_with_iadd(model_with_node: MockModel):
153154
container = Container(name="container")
154155
system += container
155156
child_node = DeploymentNode(name="child")
157+
infra_node = InfrastructureNode(name="infra")
156158

157159
node += child_node
158160
assert child_node in node.children
@@ -163,6 +165,9 @@ def test_deployment_node_add_with_iadd(model_with_node: MockModel):
163165
child_node += container
164166
assert child_node.container_instances[0].container is container
165167

168+
child_node += infra_node
169+
assert infra_node in child_node.infrastructure_nodes
170+
166171

167172
def test_deployment_node_serialising_container(model_with_node):
168173
"""Test serialisation and deserialisation includes container instances."""

0 commit comments

Comments
 (0)