Skip to content

Commit bca1583

Browse files
committed
feat: Refactor VM object to inherit from Host
Refactor VM object to inherit Host funcionalities, attributes and sub-objects. Signed-off-by: Kacper Tokarzewski <[email protected]>
1 parent 7085bea commit bca1583

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

mfd_hyperv/instances/vm.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
from mfd_common_libs import add_logging_level, log_levels
1010
from mfd_connect import RPyCConnection, Connection
11+
from mfd_host import Host
12+
1113
from mfd_hyperv.attributes.vm_params import VMParams
1214
from mfd_hyperv.exceptions import HyperVException
1315
from mfd_hyperv.hypervisor import VMProcessorAttributes
@@ -24,8 +26,12 @@
2426
add_logging_level(level_name="MODULE_DEBUG", level_value=log_levels.MODULE_DEBUG)
2527

2628

27-
class VM:
28-
"""VM class."""
29+
class VM(Host):
30+
"""VM class that inherits from Host."""
31+
32+
def __new__(cls, connection: "Connection", vm_params: VMParams, *args, **kwargs):
33+
"""Override __new__ to bypass Host's factory pattern for VM instances."""
34+
return object.__new__(cls)
2935

3036
def __init__(
3137
self,
@@ -34,15 +40,15 @@ def __init__(
3440
owner: NetworkAdapterOwner = None,
3541
hyperv: "HyperV" = None,
3642
connection_timeout: int = None,
43+
**kwargs
3744
):
3845
"""VM constructor."""
39-
self.connection = connection
46+
super().__init__(connection=connection, **kwargs)
4047
self.guest = NetworkAdapterOwner(connection=connection)
4148
self.attributes = {}
4249
self.owner = owner
4350
self._hyperv = None
4451
self.hyperv = hyperv
45-
4652
self.connection_timeout = connection_timeout
4753
self._propagate_params(vm_params)
4854

@@ -51,7 +57,7 @@ def __str__(self) -> str:
5157

5258
@property
5359
def hyperv(self) -> "HyperV":
54-
"""Hyperv property representing host's hyperv object.
60+
"""HyperV property representing host's HyperV object.
5561
5662
:raises: HyperVException when this property is empty
5763
"""
@@ -63,7 +69,7 @@ def hyperv(self) -> "HyperV":
6369

6470
@property
6571
def interfaces(self) -> List["VMNetworkInterface"]:
66-
"""Hyperv property representing VM interfaces.
72+
"""HyperV property representing VM interfaces.
6773
6874
:raises: HyperVException when this property is empty
6975
"""
@@ -130,7 +136,7 @@ def reboot(self, timeout: int = 300) -> None:
130136
self.wait_functional(timeout)
131137

132138
def wait_functional(self, timeout: int = 300) -> None:
133-
"""Wait untill this VM can be pinged.
139+
"""Wait until this VM can be pinged.
134140
135141
:param timeout: time given for VM to reach functional state
136142
"""
@@ -171,7 +177,7 @@ def _get_ifaces_from_vm(self) -> Dict[str, str]:
171177
def _check_vnic_correct_matching(self, created_vm_interfaces: List["VMNetworkInterface"]) -> None:
172178
"""Check if matching was successful, if not raise Exception.
173179
174-
:param created_vm_interfaces: list of vnics after matching VM interfaces witt VM Guest OS interfaces
180+
:param created_vm_interfaces: list of vNICs after matching VM interfaces with VM Guest OS interfaces
175181
"""
176182
all_have_iface = all(hasattr(vnic, "interface") for vnic in created_vm_interfaces)
177183
if not all_have_iface:

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ mfd-common-libs >= 1.11.0, < 2
22
mfd-typing >= 1.23.0, < 2
33
mfd_connect >= 7.12.0, < 8
44
mfd-ping >= 1.15.0, < 2
5-
mfd_network_adapter >= 14.0.0, < 15
5+
mfd_network_adapter >= 14.0.0, < 15
6+
mfd-host >= 2.0.0, < 3

0 commit comments

Comments
 (0)