Skip to content

Commit 3c84b62

Browse files
committed
feat: Add MAC address support for interface retrieval in NetworkAdapterOwner
Signed-off-by: Lasota, Adrian <[email protected]>
1 parent e7e3f55 commit 3c84b62

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

examples/linux_owner_example.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from mfd_network_adapter.network_adapter_owner.linux import LinuxNetworkAdapterOwner
77
from mfd_connect import RPyCConnection
8-
from mfd_typing import PCIAddress
8+
from mfd_typing import PCIAddress, MACAddress
99

1010
owner = LinuxNetworkAdapterOwner(connection=RPyCConnection("10.11.12.13"))
1111

@@ -38,3 +38,5 @@
3838
# utils - get_same_pci_bus_interfaces example
3939
interface = owner.get_interface(interface_name="eth1")
4040
owner.utils.get_same_pci_bus_interfaces(interface=interface) # get interfaces on the same PCI bus as eth1 interface
41+
42+
interface = owner.get_interfaces(mac_address=MACAddress("00:1A:2B:3C:4D:5E")) # get interfaces by MAC address

mfd_network_adapter/network_adapter_owner/base.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from mfd_common_libs import log_levels, add_logging_level
1313
from mfd_const import MANAGEMENT_NETWORK, Family, Speed
14-
from mfd_typing import OSName, PCIDevice, PCIAddress, VendorID
14+
from mfd_typing import OSName, PCIDevice, PCIAddress, VendorID, MACAddress
1515
from mfd_typing.network_interface import InterfaceInfo, WindowsInterfaceInfo, LinuxInterfaceInfo
1616

1717
from .exceptions import NetworkAdapterConnectedOSNotSupported, NetworkAdapterIncorrectData
@@ -376,6 +376,7 @@ def get_interfaces(
376376
interface_names: Optional[List[str]] = None,
377377
random_interface: Optional[bool] = None,
378378
all_interfaces: Optional[bool] = None,
379+
mac_address: MACAddress | None = None,
379380
) -> List["NetworkInterface"]:
380381
"""
381382
Get Network Interface objects.
@@ -388,6 +389,7 @@ def get_interfaces(
388389
3) (`pci_device`|`family`|`speed`|`family`+`speed`) + (`random_interface`|`all_interfaces`)
389390
4) (`random_interface`|`all_interfaces`)
390391
5) `interface_names`
392+
6) `mac_address`
391393
392394
:param pci_address: PCI address
393395
:param pci_device: PCI device
@@ -397,6 +399,7 @@ def get_interfaces(
397399
:param interface_names: Names of the interfaces
398400
:param random_interface: Flag - random interface
399401
:param all_interfaces: Flag - all interfaces
402+
:param mac_address: MAC Address of the interface
400403
:return: List of Network Interface objects depending on passed args
401404
"""
402405
all_interfaces_info: List[InterfaceInfoType] = self._get_all_interfaces_info()
@@ -410,6 +413,7 @@ def get_interfaces(
410413
interface_names=interface_names,
411414
random_interface=random_interface,
412415
all_interfaces=all_interfaces,
416+
mac_address=mac_address,
413417
)
414418

415419
if not filtered_info:
@@ -427,6 +431,7 @@ def get_interface(
427431
interface_index: Optional[int] = None,
428432
interface_name: Optional[str] = None,
429433
namespace: Optional[str] = None,
434+
mac_address: MACAddress | None = None,
430435
) -> "NetworkInterface":
431436
"""
432437
Get single interface of network adapter.
@@ -435,6 +440,7 @@ def get_interface(
435440
1) interface_name
436441
2) pci_address
437442
3) pci_device / family / speed + interface_index
443+
4) mac_address
438444
439445
:param pci_address: PCI address
440446
:param pci_device: PCI device
@@ -443,6 +449,7 @@ def get_interface(
443449
:param interface_index: Index of interface, like 0 - first interface of adapter
444450
:param interface_name: Name of the interface
445451
:param namespace: Linux namespace, in which cmd will be executed
452+
:param mac_address: MAC Address of the interface
446453
:return: Network Interface
447454
"""
448455
all_interfaces_info: List[InterfaceInfoType] = self._get_all_interfaces_info()
@@ -455,6 +462,7 @@ def get_interface(
455462
interface_indexes=[interface_index] if interface_index is not None else [],
456463
interface_names=[interface_name] if interface_name is not None else [],
457464
all_interfaces=True,
465+
mac_address=mac_address,
458466
)
459467

460468
if len(filtered_info) > 1:
@@ -479,6 +487,7 @@ def _filter_interfaces_info(
479487
interface_names: Optional[List[str]] = None,
480488
random_interface: Optional[bool] = None,
481489
all_interfaces: Optional[bool] = None,
490+
mac_address: MACAddress | None = None,
482491
) -> List[InterfaceInfoType]:
483492
"""
484493
Filter list based on passed criteria.
@@ -492,6 +501,7 @@ def _filter_interfaces_info(
492501
:param interface_names: Names of the interfaces
493502
:param random_interface: Flag - random interface
494503
:param all_interfaces: Flag - all interfaces
504+
:param mac_address: MAC Address of the interface
495505
:return: Filtered list of InterfaceInfo objects
496506
"""
497507
self._validate_filtering_args(
@@ -506,6 +516,8 @@ def _filter_interfaces_info(
506516
selected = [info for info in all_interfaces_info if info.name in interface_names]
507517
elif family is not None or speed is not None:
508518
selected = self._get_info_by_speed_and_family(all_interfaces_info, family=family, speed=speed)
519+
elif mac_address is not None:
520+
selected = [info for info in all_interfaces_info if info.mac_address == mac_address]
509521
else:
510522
selected = all_interfaces_info
511523

@@ -565,6 +577,7 @@ def _validate_filtering_args(
565577
family: Optional[str] = None,
566578
speed: Optional[str] = None,
567579
interface_names: Optional[List[str]] = None,
580+
mac_address: MACAddress | None = None,
568581
) -> None:
569582
"""Validate passed args based on expected combinations."""
570583
passed_combinations_amount = sum(
@@ -573,6 +586,7 @@ def _validate_filtering_args(
573586
pci_device is not None,
574587
interface_names is not None and interface_names != [],
575588
family is not None or speed is not None,
589+
mac_address is not None,
576590
]
577591
)
578592

@@ -586,7 +600,12 @@ def _validate_filtering_args(
586600
return
587601

588602
NetworkAdapterOwner._log_selection_criteria(
589-
pci_address=pci_address, pci_device=pci_device, interface_names=interface_names, family=family, speed=speed
603+
pci_address=pci_address,
604+
pci_device=pci_device,
605+
interface_names=interface_names,
606+
family=family,
607+
speed=speed,
608+
mac_address=mac_address,
590609
)
591610

592611
def _get_all_interfaces_info(self) -> List[InterfaceInfoType]:

mfd_network_adapter/network_adapter_owner/esxi.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def _filter_interfaces_info(
184184
interface_names: Optional[List[str]] = None,
185185
random_interface: Optional[bool] = None,
186186
all_interfaces: Optional[bool] = None,
187+
mac_address: MACAddress | None = None,
187188
) -> List[InterfaceInfo]:
188189
"""
189190
Filter all interfaces based on selected criteria.
@@ -197,6 +198,7 @@ def _filter_interfaces_info(
197198
:param interface_names: Names of the interfaces
198199
:param random_interface: Flag - random interface
199200
:param all_interfaces: Flag - all interfaces
201+
:param mac_address: MAC Address of the interface
200202
:return: List of Network Interface objects depending on passed args
201203
"""
202204
selected = []
@@ -225,6 +227,8 @@ def _filter_interfaces_info(
225227
continue
226228
if interface_names and interface.name not in interface_names:
227229
continue
230+
if mac_address and interface.mac_address != mac_address:
231+
continue
228232

229233
selected.append(interface)
230234

@@ -245,6 +249,7 @@ def get_interface(
245249
interface_index: Optional[int] = None,
246250
interface_name: Optional[str] = None,
247251
namespace: Optional[str] = None,
252+
mac_address: MACAddress | None = None,
248253
) -> "ESXiNetworkInterface":
249254
"""
250255
Get single interface of network adapter.
@@ -253,6 +258,7 @@ def get_interface(
253258
1) interface_name
254259
2) pci_address
255260
3) pci_device / family / speed + interface_index
261+
4) mac_address
256262
257263
:param pci_address: PCI address
258264
:param pci_device: PCI device
@@ -261,6 +267,7 @@ def get_interface(
261267
:param interface_index: Index of interface, like 0 - first interface of adapter
262268
:param interface_name: Name of the interface
263269
:param namespace: Linux namespace, in which cmd will be executed
270+
:param mac_address: MAC Address of the interface
264271
:return: Network Interface
265272
"""
266273
interface_indexes = [interface_index] if interface_index is not None else []
@@ -274,6 +281,7 @@ def get_interface(
274281
speed=speed,
275282
interface_indexes=interface_indexes,
276283
interface_names=interface_names,
284+
mac_address=mac_address,
277285
)
278286
if len(interfaces) < 1:
279287
raise NetworkAdapterNotFound("Could not find adapter with selected parameters")

tests/unit/test_mfd_network_adapter/test_network_adapter_owner/test_esxi_network_owner.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class TestESXiNetworkOwner:
7878
vmnic6 0000:31:00.2 igbn Down 0Mbps Half 00:00:00:00:00:00 1500 Intel(R) I350 Gigabit Network Connection
7979
vmnic7 0000:31:00.3 igbn Down 0Mbps Half 00:00:00:00:00:00 1500 Intel(R) I350 Gigabit Network Connection
8080
vmnic8 0000:b1:00.0 ixgben Up 10000Mbps Full 00:00:00:00:00:00 1500 Intel(R) 82599 10 Gigabit Dual Port Network Connection
81-
vmnic9 0000:b1:00.1 ixgben Up 10000Mbps Full 00:00:00:00:00:00 1500 Intel(R) 82599 10 Gigabit Dual Port Network Connection
81+
vmnic9 0000:b1:00.1 ixgben Up 10000Mbps Full 00:00:00:00:00:01 1500 Intel(R) 82599 10 Gigabit Dual Port Network Connection
8282
""" # noqa: E501
8383
)
8484

@@ -173,6 +173,14 @@ def test_filter_interfaces_pci_address(self, owner2):
173173
assert len(devices) == 1
174174
assert devices[0].name == "vmnic13"
175175

176+
def test_filter_interfaces_mac_address(self, owner2):
177+
all_interfaces_info = owner2._get_all_interfaces_info()
178+
devices = owner2._filter_interfaces_info(
179+
all_interfaces_info=all_interfaces_info, mac_address=MACAddress("00:00:00:00:00:01")
180+
)
181+
assert len(devices) == 1
182+
assert devices[0].name == "vmnic9"
183+
176184
def test_filter_interfaces_pci_device_all_1(self, owner2):
177185
all_interfaces_info = owner2._get_all_interfaces_info()
178186
devices = owner2._filter_interfaces_info(

0 commit comments

Comments
 (0)