Skip to content

Commit 3add72e

Browse files
netpickerimi
authored andcommitted
Add NetBox test (#111)
* Add NetBox test * Remove whitespace
1 parent 199832e commit 3add72e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

Integrations/.metadata

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
author: support@netpicker.io
2+
description: ""
3+
enabled: false
4+
name: Integrations
5+
type: null

Integrations/NetBox/netbox.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from comfy.compliance import medium
2+
3+
4+
@medium(
5+
name='rule_netbox',
6+
)
7+
def rule_netbox(configuration, commands, device, netbox):
8+
devices = netbox.dcim.devices.all()
9+
device_names = [device.name for device in devices]
10+
11+
for name in device_names:
12+
print(name)
13+
14+
# Get the device by name
15+
netbox_device = netbox.dcim.devices.get(name=device.name)
16+
17+
assert netbox_device is not None, f"Device '{device.name}' not found in NetBox."
18+
19+
# Fetch interfaces for the device
20+
interfaces = netbox.dcim.interfaces.filter(device_id=netbox_device.id)
21+
22+
if not interfaces:
23+
print(f"No interfaces found for device '{device.name}'.")
24+
return
25+
26+
# Execute the 'show interfaces' command
27+
show_interfaces_output = device.cli('show interfaces')
28+
29+
# Parse the output of 'show interfaces'
30+
cli_interfaces = {}
31+
for line in show_interfaces_output.splitlines():
32+
if ' is ' in line: # Identify lines that contain interface status
33+
parts = line.split()
34+
interface_name = parts[0] # The interface name is the first part
35+
if 'up' in line:
36+
interface_status = 'enabled'
37+
else:
38+
interface_status = 'disabled'
39+
cli_interfaces[interface_name] = interface_status
40+
41+
# List to accumulate mismatch messages
42+
mismatches = []
43+
44+
# Loop through each interface and compare with NetBox
45+
for interface in interfaces:
46+
# NetBox interface name
47+
netbox_interface_name = interface.name
48+
# NetBox interface status
49+
netbox_interface_status = 'enabled' if interface.enabled else 'disabled'
50+
51+
# Get the corresponding interface status from the CLI output
52+
cli_interface_status = cli_interfaces.get(netbox_interface_name, 'unknown')
53+
54+
# Check for mismatches
55+
if netbox_interface_status != cli_interface_status:
56+
mismatches.append(
57+
f"Status mismatch for {netbox_interface_name}: "
58+
f"NetBox = {netbox_interface_status}, CLI = {cli_interface_status}\n"
59+
)
60+
61+
# Perform a single assertion at the end
62+
assert not mismatches, " ".join(mismatches)

0 commit comments

Comments
 (0)