Skip to content

Commit 06f6dc3

Browse files
authored
Add Infrahub rule (#121)
* Add Infrahub rule * Fix linting issues * More linting issues
1 parent 4694134 commit 06f6dc3

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

Integrations/Infrahub/infrahub.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from comfy.compliance import medium
2+
from infrahub_sdk import Config, InfrahubClientSync
3+
4+
5+
@medium(
6+
name='rule_infrahub',
7+
)
8+
def rule_infrahub(configuration, commands, device):
9+
# https://github.com/opsmill/infrahub
10+
# https://docs.infrahub.app/python-sdk/
11+
12+
endpoint = "http://hostname:80"
13+
api_token = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
14+
branch = "main"
15+
16+
infrahub = InfrahubClientSync(address=endpoint, config=Config(api_token=api_token))
17+
18+
# devices = infrahub.all(kind='InfraDevice', branch=branch)
19+
# device_names = [device.name.value for device in devices]
20+
21+
# for name in device_names:
22+
# print(name)
23+
24+
infrahub_device = infrahub.get(kind='InfraDevice', branch=branch, name__value=device.name, include=["interfaces"])
25+
26+
assert infrahub_device is not None, f"Device '{device.name}' not found in Infrahub."
27+
28+
infrahub_device.interfaces.fetch()
29+
30+
interfaces = infrahub_device.interfaces.peers
31+
32+
# for interface in interfaces:
33+
# print(interface.peer.enabled.value, interface.display_label, interface.typename, interface.peer.name.value)
34+
35+
if not interfaces:
36+
print(f"No interfaces found for device '{device.name}'.")
37+
return
38+
39+
show_interfaces_output = device.cli('show interfaces')
40+
41+
cli_interfaces = {}
42+
for line in show_interfaces_output.splitlines():
43+
if ' is ' in line:
44+
parts = line.split()
45+
interface_name = parts[0]
46+
if 'up' in line:
47+
interface_status = 'enabled'
48+
else:
49+
interface_status = 'disabled'
50+
cli_interfaces[interface_name] = interface_status
51+
52+
mismatches = []
53+
54+
for interface in interfaces:
55+
infrahub_interface_name = interface.peer.name.value
56+
infrahub_interface_status = 'enabled' if interface.peer.enabled.value else 'disabled'
57+
58+
cli_interface_status = cli_interfaces.get(infrahub_interface_name, 'unknown')
59+
60+
if infrahub_interface_status != cli_interface_status:
61+
mismatches.append(
62+
f"Status mismatch for {infrahub_interface_name}: "
63+
f"Infrahub = {infrahub_interface_status}, CLI = {cli_interface_status}\n"
64+
)
65+
66+
assert not mismatches, " ".join(mismatches)

0 commit comments

Comments
 (0)