Skip to content

Commit 37c3e73

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 b5eeac9 commit 37c3e73

File tree

5 files changed

+68
-29
lines changed

5 files changed

+68
-29
lines changed

.pre-commit-config.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (C) 2025 Intel Corporation
2+
# SPDX-License-Identifier: MIT
3+
repos:
4+
- repo: local
5+
hooks:
6+
- id: code-format
7+
name: code-format
8+
entry: mfd-format-code
9+
language: python
10+
types: [ python ]
11+
pass_filenames: false
12+
verbose: true
13+
additional_dependencies: [ mfd-code-quality==1.2.1 ]
14+
- id: code-standard
15+
name: code-standard
16+
entry: mfd-code-standard
17+
language: python
18+
types: [ python ]
19+
pass_filenames: false
20+
verbose: true
21+
additional_dependencies: [ mfd-code-quality==1.2.1 ]

mfd_hyperv/instances/vm.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
"""Vm class."""
44

55
import logging
6-
from dataclasses import asdict
76
from time import sleep
87
from typing import Dict, Union, List, TYPE_CHECKING
98

109
from mfd_common_libs import add_logging_level, log_levels
1110
from mfd_connect import RPyCConnection, Connection
12-
from mfd_hyperv.attributes.vm_params import VMParams
13-
from mfd_hyperv.exceptions import HyperVException
14-
from mfd_hyperv.hypervisor import VMProcessorAttributes
11+
from mfd_host import Host
1512
from mfd_network_adapter import NetworkAdapterOwner
1613
from mfd_typing import MACAddress
1714
from mfd_typing.network_interface import InterfaceType
1815

16+
from mfd_hyperv.attributes.vm_params import VMParams
17+
from mfd_hyperv.exceptions import HyperVException
18+
from mfd_hyperv.hypervisor import VMProcessorAttributes
19+
1920
if TYPE_CHECKING:
2021
from mfd_hyperv import HyperV
2122
from mfd_hyperv.instances.vm_network_interface import VMNetworkInterface
@@ -25,8 +26,8 @@
2526
add_logging_level(level_name="MODULE_DEBUG", level_value=log_levels.MODULE_DEBUG)
2627

2728

28-
class VM:
29-
"""VM class."""
29+
class VM(Host):
30+
"""VM class that inherits from Host."""
3031

3132
def __init__(
3233
self,
@@ -35,24 +36,26 @@ def __init__(
3536
owner: NetworkAdapterOwner = None,
3637
hyperv: "HyperV" = None,
3738
connection_timeout: int = None,
39+
**kwargs,
3840
):
3941
"""VM constructor."""
40-
self.connection = connection
42+
super().__init__(connection=connection, **kwargs)
4143
self.guest = NetworkAdapterOwner(connection=connection)
4244
self.attributes = {}
4345
self.owner = owner
4446
self._hyperv = None
4547
self.hyperv = hyperv
46-
4748
self.connection_timeout = connection_timeout
48-
self._propagate_params(vm_params)
49+
self.mng_ip = vm_params.mng_ip
50+
self.name = vm_params.name
51+
self.vm_params = vm_params
4952

5053
def __str__(self) -> str:
5154
return f"{self.name}"
5255

5356
@property
5457
def hyperv(self) -> "HyperV":
55-
"""Hyperv property representing host's hyperv object.
58+
"""HyperV property representing host's HyperV object.
5659
5760
:raises: HyperVException when this property is empty
5861
"""
@@ -64,7 +67,7 @@ def hyperv(self) -> "HyperV":
6467

6568
@property
6669
def interfaces(self) -> List["VMNetworkInterface"]:
67-
"""Hyperv property representing VM interfaces.
70+
"""HyperV property representing VM interfaces.
6871
6972
:raises: HyperVException when this property is empty
7073
"""
@@ -79,11 +82,6 @@ def hyperv(self, value: "HyperV") -> None:
7982
"""Hyperv property setter."""
8083
self._hyperv = value
8184

82-
def _propagate_params(self, params: VMParams) -> None:
83-
"""Add VMParams as attributes of virtual machine object."""
84-
for key, value in asdict(params).items():
85-
setattr(self, key, value)
86-
8785
def get_attributes(self) -> Dict[str, str]:
8886
"""Get Virtual machine attributes from host (hypervisor)."""
8987
self.attributes = self.hyperv.hypervisor.get_vm_attributes(self.name)
@@ -131,7 +129,7 @@ def reboot(self, timeout: int = 300) -> None:
131129
self.wait_functional(timeout)
132130

133131
def wait_functional(self, timeout: int = 300) -> None:
134-
"""Wait untill this VM can be pinged.
132+
"""Wait until this VM can be pinged.
135133
136134
:param timeout: time given for VM to reach functional state
137135
"""
@@ -166,13 +164,16 @@ def _get_ifaces_from_vm(self) -> Dict[str, str]:
166164
logger.log(level=log_levels.MODULE_DEBUG, msg="Getting cached interfaces")
167165
return self.hyperv.vm_network_interface_manager.all_vnics_attributes[self.name]
168166
else:
169-
logger.log(level=log_levels.MODULE_DEBUG, msg="Retrieving Vm interfaces seen from Hypervisor")
167+
logger.log(
168+
level=log_levels.MODULE_DEBUG,
169+
msg="Retrieving Vm interfaces seen from Hypervisor",
170+
)
170171
return self.get_vm_interfaces()
171172

172173
def _check_vnic_correct_matching(self, created_vm_interfaces: List["VMNetworkInterface"]) -> None:
173174
"""Check if matching was successful, if not raise Exception.
174175
175-
:param created_vm_interfaces: list of vnics after matching VM interfaces witt VM Guest OS interfaces
176+
:param created_vm_interfaces: list of vNICs after matching VM interfaces with VM Guest OS interfaces
176177
"""
177178
all_have_iface = all(hasattr(vnic, "interface") for vnic in created_vm_interfaces)
178179
if not all_have_iface:

pyproject.toml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,25 @@ Issues = "https://github.com/intel/mfd-hyperv/issues"
2424
Changelog = "https://github.com/intel/mfd-hyperv/blob/main/CHANGELOG.md"
2525

2626
[tool.setuptools.packages.find]
27-
exclude = ["examples", "tests*", "sphinx-doc"]
27+
exclude = ["examples", "tests*", "sphinx-doc"]
28+
29+
[tool.black]
30+
line-length = 119
31+
exclude = '''
32+
(
33+
/(
34+
\.eggs
35+
| \.git
36+
| \.hg
37+
| \.mypy_cache
38+
| \.tox
39+
| \.venv
40+
| _build
41+
| buck-out
42+
| build
43+
| dist
44+
| examples
45+
)/
46+
| setup.py
47+
)
48+
'''

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

tests/unit/test_mfd_hyperv/test_vm.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,11 @@ def test_object(self, vm):
5050
vswitch_name="vswitch",
5151
)
5252

53-
vm_dict = vm.__dict__
54-
55-
del vm_dict["owner"]
56-
del vm_dict["guest"]
57-
del vm_dict["_hyperv"]
58-
del vm_dict["attributes"]
59-
del vm_dict["connection"]
60-
del vm_dict["connection_timeout"]
53+
vm_dict = vm.vm_params.__dict__
6154

6255
assert vm_dict == vm_params.__dict__
56+
assert vm.mng_ip == vm_params.mng_ip
57+
assert vm.name == vm_params.name
6358

6459
def test_get_attributes(self, vm, mocker):
6560
mocker.patch("mfd_hyperv.hypervisor.HypervHypervisor.get_vm_attributes", return_value="x")

0 commit comments

Comments
 (0)