Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2025 Intel Corporation
# SPDX-License-Identifier: MIT
repos:
- repo: local
hooks:
- id: code-format
name: code-format
entry: mfd-format-code
language: python
types: [ python ]
pass_filenames: false
verbose: true
additional_dependencies: [ mfd-code-quality==1.2.1 ]
- id: code-standard
name: code-standard
entry: mfd-code-standard
language: python
types: [ python ]
pass_filenames: false
verbose: true
additional_dependencies: [ mfd-code-quality==1.2.1 ]
39 changes: 20 additions & 19 deletions mfd_hyperv/instances/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
"""Vm class."""

import logging
from dataclasses import asdict
from time import sleep
from typing import Dict, Union, List, TYPE_CHECKING

from mfd_common_libs import add_logging_level, log_levels
from mfd_connect import RPyCConnection, Connection
from mfd_hyperv.attributes.vm_params import VMParams
from mfd_hyperv.exceptions import HyperVException
from mfd_hyperv.hypervisor import VMProcessorAttributes
from mfd_host import Host
from mfd_network_adapter import NetworkAdapterOwner
from mfd_typing import MACAddress
from mfd_typing.network_interface import InterfaceType

from mfd_hyperv.attributes.vm_params import VMParams
from mfd_hyperv.exceptions import HyperVException
from mfd_hyperv.hypervisor import VMProcessorAttributes

if TYPE_CHECKING:
from mfd_hyperv import HyperV
from mfd_hyperv.instances.vm_network_interface import VMNetworkInterface
Expand All @@ -25,8 +26,8 @@
add_logging_level(level_name="MODULE_DEBUG", level_value=log_levels.MODULE_DEBUG)


class VM:
"""VM class."""
class VM(Host):
"""VM class that inherits from Host."""

def __init__(
self,
Expand All @@ -35,24 +36,26 @@ def __init__(
owner: NetworkAdapterOwner = None,
hyperv: "HyperV" = None,
connection_timeout: int = None,
**kwargs,
):
"""VM constructor."""
self.connection = connection
super().__init__(connection=connection, **kwargs)
self.guest = NetworkAdapterOwner(connection=connection)
self.attributes = {}
self.owner = owner
self._hyperv = None
self.hyperv = hyperv

self.connection_timeout = connection_timeout
self._propagate_params(vm_params)
self.mng_ip = vm_params.mng_ip
self.name = vm_params.name
self.vm_params = vm_params

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

@property
def hyperv(self) -> "HyperV":
"""Hyperv property representing host's hyperv object.
"""HyperV property representing host's HyperV object.

:raises: HyperVException when this property is empty
"""
Expand All @@ -64,7 +67,7 @@ def hyperv(self) -> "HyperV":

@property
def interfaces(self) -> List["VMNetworkInterface"]:
"""Hyperv property representing VM interfaces.
"""HyperV property representing VM interfaces.

:raises: HyperVException when this property is empty
"""
Expand All @@ -79,11 +82,6 @@ def hyperv(self, value: "HyperV") -> None:
"""Hyperv property setter."""
self._hyperv = value

def _propagate_params(self, params: VMParams) -> None:
"""Add VMParams as attributes of virtual machine object."""
for key, value in asdict(params).items():
setattr(self, key, value)

def get_attributes(self) -> Dict[str, str]:
"""Get Virtual machine attributes from host (hypervisor)."""
self.attributes = self.hyperv.hypervisor.get_vm_attributes(self.name)
Expand Down Expand Up @@ -131,7 +129,7 @@ def reboot(self, timeout: int = 300) -> None:
self.wait_functional(timeout)

def wait_functional(self, timeout: int = 300) -> None:
"""Wait untill this VM can be pinged.
"""Wait until this VM can be pinged.

:param timeout: time given for VM to reach functional state
"""
Expand Down Expand Up @@ -166,13 +164,16 @@ def _get_ifaces_from_vm(self) -> Dict[str, str]:
logger.log(level=log_levels.MODULE_DEBUG, msg="Getting cached interfaces")
return self.hyperv.vm_network_interface_manager.all_vnics_attributes[self.name]
else:
logger.log(level=log_levels.MODULE_DEBUG, msg="Retrieving Vm interfaces seen from Hypervisor")
logger.log(
level=log_levels.MODULE_DEBUG,
msg="Retrieving Vm interfaces seen from Hypervisor",
)
return self.get_vm_interfaces()

def _check_vnic_correct_matching(self, created_vm_interfaces: List["VMNetworkInterface"]) -> None:
"""Check if matching was successful, if not raise Exception.

:param created_vm_interfaces: list of vnics after matching VM interfaces witt VM Guest OS interfaces
:param created_vm_interfaces: list of vNICs after matching VM interfaces with VM Guest OS interfaces
"""
all_have_iface = all(hasattr(vnic, "interface") for vnic in created_vm_interfaces)
if not all_have_iface:
Expand Down
23 changes: 22 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,25 @@ Issues = "https://github.com/intel/mfd-hyperv/issues"
Changelog = "https://github.com/intel/mfd-hyperv/blob/main/CHANGELOG.md"

[tool.setuptools.packages.find]
exclude = ["examples", "tests*", "sphinx-doc"]
exclude = ["examples", "tests*", "sphinx-doc"]

[tool.black]
line-length = 119
exclude = '''
(
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
| examples
)/
| setup.py
)
'''
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ mfd-common-libs >= 1.11.0, < 2
mfd-typing >= 1.23.0, < 2
mfd_connect >= 7.12.0, < 8
mfd-ping >= 1.15.0, < 2
mfd_network_adapter >= 14.0.0, < 15
mfd_network_adapter >= 14.0.0, < 15
mfd-host >= 2.0.0, < 3
11 changes: 3 additions & 8 deletions tests/unit/test_mfd_hyperv/test_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,11 @@ def test_object(self, vm):
vswitch_name="vswitch",
)

vm_dict = vm.__dict__

del vm_dict["owner"]
del vm_dict["guest"]
del vm_dict["_hyperv"]
del vm_dict["attributes"]
del vm_dict["connection"]
del vm_dict["connection_timeout"]
vm_dict = vm.vm_params.__dict__

assert vm_dict == vm_params.__dict__
assert vm.mng_ip == vm_params.mng_ip
assert vm.name == vm_params.name

def test_get_attributes(self, vm, mocker):
mocker.patch("mfd_hyperv.hypervisor.HypervHypervisor.get_vm_attributes", return_value="x")
Expand Down
Loading