Skip to content

Commit 30388ad

Browse files
authored
Add Device Role column to baremetal list command (#1821)
Fetches the device role from NetBox for each baremetal node, similar to how it's done in sonic list. Falls back to "N/A" if NetBox is unavailable or device is not found. Closes osism/issues#1322 AI-assisted: Claude Code Signed-off-by: Christian Berendt <[email protected]>
1 parent 6f9c62b commit 30388ad

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

osism/commands/baremetal.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,38 @@ def take_action(self, parsed_args):
5353

5454
baremetal = conn.baremetal.nodes(**query)
5555

56-
result = [
57-
[
58-
b["name"],
59-
b["power_state"],
60-
b["provision_state"],
61-
b["maintenance"],
62-
]
63-
for b in baremetal
64-
]
56+
result = []
57+
for b in baremetal:
58+
# Get device role from NetBox
59+
device_role = "N/A"
60+
if utils.nb:
61+
try:
62+
# Try to find device by name first
63+
device = utils.nb.dcim.devices.get(name=b["name"])
64+
65+
# If not found by name, try by inventory_hostname custom field
66+
if not device:
67+
devices = utils.nb.dcim.devices.filter(
68+
cf_inventory_hostname=b["name"]
69+
)
70+
if devices:
71+
device = list(devices)[0]
72+
73+
# Get device role
74+
if device and device.role and hasattr(device.role, "name"):
75+
device_role = device.role.name
76+
except Exception as e:
77+
logger.debug(f"Could not get device role for {b['name']}: {e}")
78+
79+
result.append(
80+
[
81+
b["name"],
82+
device_role,
83+
b["power_state"],
84+
b["provision_state"],
85+
b["maintenance"],
86+
]
87+
)
6588

6689
result.sort(key=lambda x: x[0])
6790

@@ -70,6 +93,7 @@ def take_action(self, parsed_args):
7093
result,
7194
headers=[
7295
"Name",
96+
"Device Role",
7397
"Power State",
7498
"Provision State",
7599
"Maintenance",

0 commit comments

Comments
 (0)