Skip to content

Commit 9640ca1

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

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 interface 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 SPEED_IDS, DEVICE_IDS, 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
@@ -371,6 +371,7 @@ def get_interfaces(
371371
interface_names: Optional[List[str]] = None,
372372
random_interface: Optional[bool] = None,
373373
all_interfaces: Optional[bool] = None,
374+
mac_address: MACAddress | None = None,
374375
) -> List["NetworkInterface"]:
375376
"""
376377
Get Network Interface objects.
@@ -383,6 +384,7 @@ def get_interfaces(
383384
3) (`pci_device`|`family`|`speed`|`family`+`speed`) + (`random_interface`|`all_interfaces`)
384385
4) (`random_interface`|`all_interfaces`)
385386
5) `interface_names`
387+
6) `mac_address`
386388
387389
:param pci_address: PCI address
388390
:param pci_device: PCI device
@@ -392,6 +394,7 @@ def get_interfaces(
392394
:param interface_names: Names of the interfaces
393395
:param random_interface: Flag - random interface
394396
:param all_interfaces: Flag - all interfaces
397+
:param mac_address: MAC Address of the interface
395398
:return: List of Network Interface objects depending on passed args
396399
"""
397400
all_interfaces_info: List[InterfaceInfoType] = self._get_all_interfaces_info()
@@ -405,6 +408,7 @@ def get_interfaces(
405408
interface_names=interface_names,
406409
random_interface=random_interface,
407410
all_interfaces=all_interfaces,
411+
mac_address=mac_address,
408412
)
409413

410414
if not filtered_info:
@@ -422,6 +426,7 @@ def get_interface(
422426
interface_index: Optional[int] = None,
423427
interface_name: Optional[str] = None,
424428
namespace: Optional[str] = None,
429+
mac_address: MACAddress | None = None,
425430
) -> "NetworkInterface":
426431
"""
427432
Get single interface of network adapter.
@@ -430,6 +435,7 @@ def get_interface(
430435
1) interface_name
431436
2) pci_address
432437
3) pci_device / family / speed + interface_index
438+
4) mac_address
433439
434440
:param pci_address: PCI address
435441
:param pci_device: PCI device
@@ -438,6 +444,7 @@ def get_interface(
438444
:param interface_index: Index of interface, like 0 - first interface of adapter
439445
:param interface_name: Name of the interface
440446
:param namespace: Linux namespace, in which cmd will be executed
447+
:param mac_address: MAC Address of the interface
441448
:return: Network Interface
442449
"""
443450
all_interfaces_info: List[InterfaceInfoType] = self._get_all_interfaces_info()
@@ -450,6 +457,7 @@ def get_interface(
450457
interface_indexes=[interface_index] if interface_index is not None else [],
451458
interface_names=[interface_name] if interface_name is not None else [],
452459
all_interfaces=True,
460+
mac_address=mac_address,
453461
)
454462

455463
if len(filtered_info) > 1:
@@ -474,6 +482,7 @@ def _filter_interfaces_info(
474482
interface_names: Optional[List[str]] = None,
475483
random_interface: Optional[bool] = None,
476484
all_interfaces: Optional[bool] = None,
485+
mac_address: MACAddress | None = None,
477486
) -> List[InterfaceInfoType]:
478487
"""
479488
Filter list based on passed criteria.
@@ -487,6 +496,7 @@ def _filter_interfaces_info(
487496
:param interface_names: Names of the interfaces
488497
:param random_interface: Flag - random interface
489498
:param all_interfaces: Flag - all interfaces
499+
:param mac_address: MAC Address of the interface
490500
:return: Filtered list of InterfaceInfo objects
491501
"""
492502
self._validate_filtering_args(
@@ -501,6 +511,8 @@ def _filter_interfaces_info(
501511
selected = [info for info in all_interfaces_info if info.name in interface_names]
502512
elif family is not None or speed is not None:
503513
selected = self._get_info_by_speed_and_family(all_interfaces_info, family=family, speed=speed)
514+
elif mac_address is not None:
515+
selected = [info for info in all_interfaces_info if info.mac_address == mac_address]
504516
else:
505517
selected = all_interfaces_info
506518

@@ -560,6 +572,7 @@ def _validate_filtering_args(
560572
family: Optional[str] = None,
561573
speed: Optional[str] = None,
562574
interface_names: Optional[List[str]] = None,
575+
mac_address: MACAddress | None = None,
563576
) -> None:
564577
"""Validate passed args based on expected combinations."""
565578
passed_combinations_amount = sum(
@@ -568,6 +581,7 @@ def _validate_filtering_args(
568581
pci_device is not None,
569582
interface_names is not None and interface_names != [],
570583
family is not None or speed is not None,
584+
mac_address is not None,
571585
]
572586
)
573587

@@ -581,7 +595,12 @@ def _validate_filtering_args(
581595
return
582596

583597
NetworkAdapterOwner._log_selection_criteria(
584-
pci_address=pci_address, pci_device=pci_device, interface_names=interface_names, family=family, speed=speed
598+
pci_address=pci_address,
599+
pci_device=pci_device,
600+
interface_names=interface_names,
601+
family=family,
602+
speed=speed,
603+
mac_address=mac_address,
585604
)
586605

587606
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
@@ -179,6 +179,7 @@ def _filter_interfaces_info(
179179
interface_names: Optional[List[str]] = None,
180180
random_interface: Optional[bool] = None,
181181
all_interfaces: Optional[bool] = None,
182+
mac_address: MACAddress | None = None,
182183
) -> List[InterfaceInfo]:
183184
"""
184185
Filter all interfaces based on selected criteria.
@@ -192,6 +193,7 @@ def _filter_interfaces_info(
192193
:param interface_names: Names of the interfaces
193194
:param random_interface: Flag - random interface
194195
:param all_interfaces: Flag - all interfaces
196+
:param mac_address: MAC Address of the interface
195197
:return: List of Network Interface objects depending on passed args
196198
"""
197199
selected = []
@@ -220,6 +222,8 @@ def _filter_interfaces_info(
220222
continue
221223
if interface_names and interface.name not in interface_names:
222224
continue
225+
if mac_address and interface.mac_address != mac_address:
226+
continue
223227

224228
selected.append(interface)
225229

@@ -240,6 +244,7 @@ def get_interface(
240244
interface_index: Optional[int] = None,
241245
interface_name: Optional[str] = None,
242246
namespace: Optional[str] = None,
247+
mac_address: MACAddress | None = None,
243248
) -> "ESXiNetworkInterface":
244249
"""
245250
Get single interface of network adapter.
@@ -248,6 +253,7 @@ def get_interface(
248253
1) interface_name
249254
2) pci_address
250255
3) pci_device / family / speed + interface_index
256+
4) mac_address
251257
252258
:param pci_address: PCI address
253259
:param pci_device: PCI device
@@ -256,6 +262,7 @@ def get_interface(
256262
:param interface_index: Index of interface, like 0 - first interface of adapter
257263
:param interface_name: Name of the interface
258264
:param namespace: Linux namespace, in which cmd will be executed
265+
:param mac_address: MAC Address of the interface
259266
:return: Network Interface
260267
"""
261268
interface_indexes = [interface_index] if interface_index is not None else []
@@ -269,6 +276,7 @@ def get_interface(
269276
speed=speed,
270277
interface_indexes=interface_indexes,
271278
interface_names=interface_names,
279+
mac_address=mac_address,
272280
)
273281
if len(interfaces) < 1:
274282
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)