Skip to content

Commit fa929a1

Browse files
committed
addressed reviews
1 parent 5c7cf60 commit fa929a1

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

nodescraper/plugins/inband/device_enumeration/analyzer_args.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,36 @@
2323
# SOFTWARE.
2424
#
2525
###############################################################################
26-
from typing import Optional, Union
26+
from typing import Any, Optional
27+
28+
from pydantic import field_validator
2729

2830
from nodescraper.models import AnalyzerArgs
2931

3032
from .deviceenumdata import DeviceEnumerationDataModel
3133

3234

3335
class DeviceEnumerationAnalyzerArgs(AnalyzerArgs):
34-
cpu_count: Optional[Union[list[int], int]] = None
35-
gpu_count: Optional[Union[list[int], int]] = None
36-
vf_count: Optional[Union[list[int], int]] = None
36+
cpu_count: Optional[list[int]] = None
37+
gpu_count: Optional[list[int]] = None
38+
vf_count: Optional[list[int]] = None
39+
40+
@field_validator("cpu_count", "gpu_count", "vf_count", mode="before")
41+
@classmethod
42+
def normalize_to_list(cls, v: Any) -> Optional[list[int]]:
43+
"""Convert single integer values to lists for consistent handling.
44+
45+
Args:
46+
v: The input value (can be int, list[int], or None).
47+
48+
Returns:
49+
Optional[list[int]]: The normalized list value or None.
50+
"""
51+
if v is None:
52+
return None
53+
if isinstance(v, int):
54+
return [v]
55+
return v
3756

3857
@classmethod
3958
def build_from_model(
@@ -48,7 +67,7 @@ def build_from_model(
4867
DeviceEnumerationAnalyzerArgs: instance of analyzer args class
4968
"""
5069
return cls(
51-
cpu_count=datamodel.cpu_count,
52-
gpu_count=datamodel.gpu_count,
53-
vf_count=datamodel.vf_count,
70+
cpu_count=[datamodel.cpu_count] if datamodel.cpu_count is not None else None,
71+
gpu_count=[datamodel.gpu_count] if datamodel.gpu_count is not None else None,
72+
vf_count=[datamodel.vf_count] if datamodel.vf_count is not None else None,
5473
)

nodescraper/plugins/inband/device_enumeration/device_enumeration_analyzer.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,13 @@ def analyze_data(
5252
)
5353
return self.result
5454

55-
# Convert to lists if integers, otherwise use as-is
56-
cpu_count = [args.cpu_count] if isinstance(args.cpu_count, int) else args.cpu_count
57-
gpu_count = [args.gpu_count] if isinstance(args.gpu_count, int) else args.gpu_count
58-
vf_count = [args.vf_count] if isinstance(args.vf_count, int) else args.vf_count
59-
6055
checks = {}
61-
if cpu_count is not None and cpu_count != []:
62-
checks["cpu_count"] = cpu_count
63-
if gpu_count is not None and gpu_count != []:
64-
checks["gpu_count"] = gpu_count
65-
if vf_count is not None and vf_count != []:
66-
checks["vf_count"] = vf_count
56+
if args.cpu_count is not None and args.cpu_count != []:
57+
checks["cpu_count"] = args.cpu_count
58+
if args.gpu_count is not None and args.gpu_count != []:
59+
checks["gpu_count"] = args.gpu_count
60+
if args.vf_count is not None and args.vf_count != []:
61+
checks["vf_count"] = args.vf_count
6762

6863
self.result.message = ""
6964
for check, accepted_counts in checks.items():

nodescraper/plugins/inband/device_enumeration/device_enumeration_collector.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[DeviceEnumeratio
7474
On Linux, use lscpu and lspci
7575
On Windows, use WMI and hyper-v cmdlets
7676
"""
77-
device_enum = None
7877
if self.system_info.os_family == OSFamily.LINUX:
7978
# Count CPU sockets
8079
cpu_count_res = self._run_sut_cmd(self.CMD_CPU_COUNT_LINUX)
@@ -91,31 +90,29 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[DeviceEnumeratio
9190
cpu_count_res = self._run_sut_cmd(self.CMD_CPU_COUNT_WINDOWS)
9291
gpu_count_res = self._run_sut_cmd(self.CMD_GPU_COUNT_WINDOWS)
9392
vf_count_res = self._run_sut_cmd(self.CMD_VF_COUNT_WINDOWS)
94-
cpu_count, gpu_count, vf_count = [None, None, None]
93+
94+
device_enum = DeviceEnumerationDataModel()
9595

9696
if cpu_count_res.exit_code == 0:
97-
cpu_count = int(cpu_count_res.stdout)
97+
device_enum.cpu_count = int(cpu_count_res.stdout)
9898
else:
9999
self._warning(description="Cannot determine CPU count", command=cpu_count_res)
100100

101101
if gpu_count_res.exit_code == 0:
102-
gpu_count = int(gpu_count_res.stdout)
102+
device_enum.gpu_count = int(gpu_count_res.stdout)
103103
else:
104104
self._warning(description="Cannot determine GPU count", command=gpu_count_res)
105105

106106
if vf_count_res.exit_code == 0:
107-
vf_count = int(vf_count_res.stdout)
107+
device_enum.vf_count = int(vf_count_res.stdout)
108108
else:
109109
self._warning(
110110
description="Cannot determine VF count",
111111
command=vf_count_res,
112112
category=EventCategory.SW_DRIVER,
113113
)
114114

115-
if cpu_count or gpu_count or vf_count:
116-
device_enum = DeviceEnumerationDataModel(
117-
cpu_count=cpu_count, gpu_count=gpu_count, vf_count=vf_count
118-
)
115+
if device_enum.cpu_count or device_enum.gpu_count or device_enum.vf_count:
119116
self._log_event(
120117
category=EventCategory.PLATFORM,
121118
description=f"Counted {device_enum.cpu_count} CPUs, {device_enum.gpu_count} GPUs, {device_enum.vf_count} VFs",
@@ -124,6 +121,7 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[DeviceEnumeratio
124121
)
125122
self.result.message = f"Device Enumeration: {device_enum.model_dump(exclude_none=True)}"
126123
self.result.status = ExecutionStatus.OK
124+
return self.result, device_enum
127125
else:
128126
self.result.message = "Device Enumeration info not found"
129127
self.result.status = ExecutionStatus.EXECUTION_FAILURE
@@ -132,5 +130,4 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[DeviceEnumeratio
132130
description=self.result.message,
133131
priority=EventPriority.CRITICAL,
134132
)
135-
136-
return self.result, device_enum
133+
return self.result, None

0 commit comments

Comments
 (0)