|
15 | 15 |
|
16 | 16 | """Provide a deployment node model.""" |
17 | 17 |
|
| 18 | +from typing import TYPE_CHECKING, Iterable, List |
18 | 19 |
|
19 | | -from typing import Iterable |
| 20 | +from pydantic import Field |
20 | 21 |
|
21 | 22 | from .deployment_element import DeploymentElement, DeploymentElementIO |
22 | 23 |
|
23 | 24 |
|
| 25 | +if TYPE_CHECKING: |
| 26 | + from .model import Model |
| 27 | + |
24 | 28 | __all__ = ("DeploymentNode", "DeploymentNodeIO") |
25 | 29 |
|
26 | 30 |
|
27 | 31 | class DeploymentNodeIO(DeploymentElementIO): |
28 | | - """Represent a deployment node.""" |
| 32 | + """ |
| 33 | + Represent a deployment node. |
| 34 | +
|
| 35 | + Attributes: |
| 36 | + id: The ID of this deployment node in the model. |
| 37 | + name: The name of this node. |
| 38 | + description: A short description of this node. |
| 39 | + environment (str): |
| 40 | + tags: A comma separated list of tags associated with this node. |
| 41 | + children: The deployment nodes that are direct children of this node. |
| 42 | + properties: A set of arbitrary name-value properties. |
| 43 | + relationships: The set of relationships from this node to |
| 44 | + other elements. |
| 45 | + url (pydantic.HttpUrl): |
| 46 | + """ |
| 47 | + |
| 48 | + class Config: |
| 49 | + """Pydantic configuration for DeploymentNodeIO.""" |
| 50 | + |
| 51 | + # Prevent infinite recursion for `children` - see |
| 52 | + # https://github.com/samuelcolvin/pydantic/issues/524 |
| 53 | + validate_assignment = "limited" |
29 | 54 |
|
30 | | - parent: "DeploymentNodeIO" |
31 | 55 | technology: str = "" |
32 | 56 | instances: int = 1 |
| 57 | + children: List["DeploymentNodeIO"] = Field(default=()) |
| 58 | + |
| 59 | + |
| 60 | +DeploymentNodeIO.update_forward_refs() |
33 | 61 |
|
34 | 62 |
|
35 | 63 | class DeploymentNode(DeploymentElement): |
36 | | - """Represent a deployment node.""" |
| 64 | + """ |
| 65 | + Represent a deployment node. |
| 66 | +
|
| 67 | + Attributes: |
| 68 | + id: The ID of this deployment node in the model. |
| 69 | + name: The name of this node. |
| 70 | + description: A short description of this node. |
| 71 | + environment (str): |
| 72 | + tags: A comma separated list of tags associated with this node. |
| 73 | + children: The deployment nodes that are direct children of this node. |
| 74 | + properties: A set of arbitrary name-value properties. |
| 75 | + relationships: The set of relationships from this node to |
| 76 | + other elements. |
| 77 | + url (pydantic.HttpUrl): |
| 78 | + """ |
37 | 79 |
|
38 | 80 | def __init__( |
39 | 81 | self, |
40 | 82 | *, |
41 | | - parent: "DeploymentNode", |
| 83 | + parent: "DeploymentNode" = None, |
42 | 84 | technology: str = "", |
43 | 85 | instances: int = 1, |
44 | 86 | children: Iterable["DeploymentNode"] = (), |
45 | 87 | container_instances: Iterable["DeploymentNode"] = (), |
46 | | - **kwargs |
| 88 | + **kwargs, |
47 | 89 | ) -> None: |
48 | 90 | """Initialize a deployment node.""" |
49 | 91 | super().__init__(**kwargs) |
50 | 92 | self.parent = parent |
51 | 93 | self.technology = technology |
52 | 94 | self.instances = instances |
53 | | - self.children = set(children) |
54 | | - self.container_instances = set(container_instances) |
| 95 | + self._children = set(children) |
| 96 | + self._container_instances = set(container_instances) |
| 97 | + |
| 98 | + @property |
| 99 | + def children(self) -> Iterable["DeploymentNode"]: |
| 100 | + """Return read-only list of child nodes.""" |
| 101 | + return list(self._children) |
| 102 | + |
| 103 | + def add_deployment_node(self, **kwargs) -> "DeploymentNode": |
| 104 | + """Add a new child deployment node to this node.""" |
| 105 | + node = DeploymentNode(**kwargs) |
| 106 | + self += node |
| 107 | + return node |
| 108 | + |
| 109 | + def __iadd__(self, node: "DeploymentNode") -> "DeploymentNode": |
| 110 | + """Add a newly constructed chile deployment node to this node.""" |
| 111 | + if node in self._children: |
| 112 | + return self |
| 113 | + |
| 114 | + # if self.get_component_with_name(component.name): |
| 115 | + # raise ValueError( |
| 116 | + # f"Component with name {component.name} already exists in {self}." |
| 117 | + # ) |
| 118 | + |
| 119 | + if node.parent is None: |
| 120 | + node.parent = self |
| 121 | + elif node.parent is not self: |
| 122 | + raise ValueError( |
| 123 | + f"DeploymentNode with name {node.name} already has parent " |
| 124 | + f"{node.parent}. Cannot add to {self}." |
| 125 | + ) |
| 126 | + self._children.add(node) |
| 127 | + model = self.model |
| 128 | + model += node |
| 129 | + return self |
55 | 130 |
|
56 | 131 | @classmethod |
57 | | - def hydrate(cls, deployment_node_io: DeploymentNodeIO) -> "DeploymentNode": |
| 132 | + def hydrate( |
| 133 | + cls, |
| 134 | + deployment_node_io: DeploymentNodeIO, |
| 135 | + model: "Model", |
| 136 | + parent: "DeploymentNode" = None, |
| 137 | + ) -> "DeploymentNode": |
58 | 138 | """Hydrate a new DeploymentNode instance from its IO.""" |
59 | | - # TODO (midnighter): Initialization requires `parent`. |
60 | | - return cls( |
61 | | - # parent=deployment_node_io.parent, |
| 139 | + node = cls( |
| 140 | + parent=parent, |
62 | 141 | name=deployment_node_io.name, |
63 | 142 | description=deployment_node_io.description, |
64 | 143 | ) |
| 144 | + model += node |
| 145 | + |
| 146 | + for child_io in deployment_node_io.children: |
| 147 | + child_node = DeploymentNode.hydrate(child_io, model, node) |
| 148 | + node += child_node |
| 149 | + |
| 150 | + return node |
0 commit comments