|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | + |
| 14 | +"""Provide a component view. |
| 15 | +
|
| 16 | +Used to show the mapping of container instances to deployment nodes. |
| 17 | +""" |
| 18 | + |
| 19 | +from typing import Iterable, Optional, Union |
| 20 | + |
| 21 | +from ..mixin.model_ref_mixin import ModelRefMixin |
| 22 | +from ..model.container_instance import ContainerInstance |
| 23 | +from ..model.deployment_node import DeploymentNode |
| 24 | +from ..model.infrastructure_node import InfrastructureNode |
| 25 | +from ..model.relationship import Relationship |
| 26 | +from ..model.software_system_instance import SoftwareSystemInstance |
| 27 | +from .animation import Animation |
| 28 | +from .view import View |
| 29 | + |
| 30 | + |
| 31 | +__all__ = ("DeploymentView", "DeploymentViewIO") |
| 32 | + |
| 33 | + |
| 34 | +class DeploymentViewIO: |
| 35 | + pass |
| 36 | + |
| 37 | + |
| 38 | +class DeploymentView(ModelRefMixin, View): |
| 39 | + def __init__( |
| 40 | + self, |
| 41 | + *, |
| 42 | + environment: Optional[str] = None, |
| 43 | + animations: Optional[Iterable[Animation]] = None, |
| 44 | + **kwargs, |
| 45 | + ) -> None: |
| 46 | + """Initialize a deployment view.""" |
| 47 | + super().__init__(**kwargs) |
| 48 | + self._environment = environment |
| 49 | + self._animations = [] if animations is None else list(animations) |
| 50 | + |
| 51 | + @property |
| 52 | + def environment(self): |
| 53 | + """Get the name of the environment that this view is for. |
| 54 | +
|
| 55 | + E.g. "Development", "Live", etc. |
| 56 | + """ |
| 57 | + return self._environment |
| 58 | + |
| 59 | + def add_default_elements(self): |
| 60 | + """Add the default set of elements to this view.""" |
| 61 | + self.add_all_deployment_nodes() |
| 62 | + |
| 63 | + def add_all_deployment_nodes(self): |
| 64 | + """Add all of the top-level deployment nodes to this view. |
| 65 | +
|
| 66 | + If the environment is set in this view, then only nodes from the same |
| 67 | + environment will be added. |
| 68 | + """ |
| 69 | + for deployment_node in self.model.deployment_nodes: |
| 70 | + if deployment_node.parent is None: |
| 71 | + if ( |
| 72 | + self.environment is None |
| 73 | + or self.environment == deployment_node.environment |
| 74 | + ): |
| 75 | + self.add(deployment_node) |
| 76 | + |
| 77 | + def add( |
| 78 | + self, item: Union[DeploymentNode, Relationship], add_relationships: bool = True |
| 79 | + ): |
| 80 | + """Add a deployment node or relationship to this view.""" |
| 81 | + if isinstance(item, DeploymentNode): |
| 82 | + if self._add_node_children(item, add_relationships): |
| 83 | + parent = item.parent |
| 84 | + while parent is not None: |
| 85 | + self._add_element(parent, add_relationships) |
| 86 | + parent = parent.parent |
| 87 | + else: |
| 88 | + pass # TODO |
| 89 | + |
| 90 | + def remove( |
| 91 | + self, |
| 92 | + item: Union[ |
| 93 | + DeploymentNode, |
| 94 | + InfrastructureNode, |
| 95 | + ContainerInstance, |
| 96 | + SoftwareSystemInstance, |
| 97 | + ], |
| 98 | + ): |
| 99 | + """Remove the given item from this view.""" |
| 100 | + pass # TODO |
| 101 | + |
| 102 | + def _add_node_children( |
| 103 | + self, deployment_node: DeploymentNode, add_relationships: bool |
| 104 | + ): |
| 105 | + has_elements_or_relationships = False |
| 106 | + for instance in deployment_node.software_system_instances: |
| 107 | + self._add_element(instance, add_relationships) |
| 108 | + has_elements_or_relationships = True |
| 109 | + |
| 110 | + for instance in deployment_node.container_instances: |
| 111 | + container = instance.container |
| 112 | + if self.software_system is None or container.parent is self.software_system: |
| 113 | + self._add_element(instance, add_relationships) |
| 114 | + has_elements_or_relationships = True |
| 115 | + |
| 116 | + for node in deployment_node.infrastructure_nodes: |
| 117 | + self._add_element(node, add_relationships) |
| 118 | + has_elements_or_relationships = True |
| 119 | + |
| 120 | + for child in deployment_node.children: |
| 121 | + has_elements_or_relationships |= self._add_node_children( |
| 122 | + child, add_relationships |
| 123 | + ) |
| 124 | + |
| 125 | + if has_elements_or_relationships: |
| 126 | + self._add_element(deployment_node, add_relationships) |
| 127 | + |
| 128 | + return has_elements_or_relationships |
| 129 | + |
| 130 | + @property |
| 131 | + def name(self): |
| 132 | + """Get the (computed) name of this view.""" |
| 133 | + name = ( |
| 134 | + "Deployment" |
| 135 | + if self.software_system is None |
| 136 | + else f"{self.software_system.name} - Deployment" |
| 137 | + ) |
| 138 | + if self.environment: |
| 139 | + name = f"{name} - {self.environment}" |
| 140 | + return name |
| 141 | + |
| 142 | + # def can_be_removed(element: Element) |
| 143 | + |
| 144 | + # def add_animation(element_instances) |
| 145 | + |
| 146 | + # def add_animation_step(elements) |
| 147 | + |
| 148 | + # def _find_deployment_node(element: Element) |
| 149 | + |
| 150 | + @property |
| 151 | + def animations(self) -> Iterable[Animation]: |
| 152 | + pass # TODO |
0 commit comments