forked from netpicker/infrahub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetpicker_api.py
76 lines (61 loc) · 2.65 KB
/
netpicker_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import requests
from infrahub_sdk.checks import InfrahubCheck
class Check(InfrahubCheck):
query = "tags_check"
def validate(self, data):
login_url = 'http://opsmill-netpicker.tailc018d.ts.net/api/v1/auth/jwt/login'
info_url = "http://opsmill-netpicker.tailc018d.ts.net/api/v1/auth/info"
data = {
'grant_type': 'password',
'scope': 'openid access:api',
'username': '[email protected]',
'password': '12345678'
}
try:
# Login request
login_response = requests.post(login_url, data=data, verify=False)
login_response.raise_for_status()
# Extract access token from login response
access_token = login_response.json().get('access_token')
if not access_token:
self.log_error(message="Failed to obtain access token")
return
# Add access token to headers for info request
headers = {
'Authorization': f'Bearer {access_token}'
}
# Info request
info_response = requests.get(info_url, headers=headers, verify=False)
info_response.raise_for_status()
self.log_info(message="Response from auth/info:")
self.log_info(message=info_response.json())
# Debug request
debug_url = "http://opsmill-netpicker.tailc018d.ts.net/api/v1/policy/default/Infrahub_SDK/debug"
debug_headers = {
'Accept': 'application/json, text/plain, */*',
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json',
}
python_code = """@medium(
name='rule_show_interfaces',
)
def rule_show_interfaces(configuration, commands, device):
assert configuration == False, 'Error message'
"""
debug_data = {
"name": "rule_show_interfaces",
"severity": "MEDIUM",
"configuration": "example config here",
#"ipaddress": "cisco_ios",
"command": None,
"ruleset": "default",
"definition": {
"code": python_code
}
}
debug_response = requests.post(debug_url, headers=debug_headers, json=debug_data, verify=False)
debug_response.raise_for_status()
self.log_info(message="Response from debug endpoint:")
self.log_info(message=debug_response.json())
except requests.RequestException as e:
self.log_error(message=f"An error occurred: {e}")