62 lines
2.4 KiB
Python
62 lines
2.4 KiB
Python
import requests, ipaddress
|
|
|
|
class APIClient:
|
|
def __init__(self, url:str, key:str, serverid:int):
|
|
self.url = url
|
|
self.serverid = serverid
|
|
self.headers = {
|
|
"Authorization": f"Bearer {key}",
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json"
|
|
}
|
|
|
|
def get_all_info(self):
|
|
info:dict = {}
|
|
switch:dict = {}
|
|
|
|
connection_response = requests.get(f"https://{self.url}/api/servers/{self.serverid}/connections").json()
|
|
for value in connection_response["result"]:
|
|
if "related_data" in value and value["related_data"]["type"] == "snmp_switch":
|
|
network_device = requests.get(f"https://{self.url}/api/networkDevices/{value["related_data"]["meta"]["switchId"]}").json()
|
|
for port in network_device["result"]["ports"]:
|
|
if port["id"] == value["related_data"]["meta"]["portId"]:
|
|
switch = {
|
|
"host": network_device["result"]["host"],
|
|
"username": network_device["result"]["managementUser"],
|
|
"password": network_device["result"]["managementPassword"],
|
|
"vendor": network_device["result"]["managementVendor"],
|
|
"port": port["name"]
|
|
}
|
|
|
|
ipassignment_response = requests.get(f"https://{self.url}/api/servers/{self.serverid}/ipassignments").json()
|
|
for value in ipassignment_response["result"]:
|
|
if value["ipAttributes"]["isIpv4"]:
|
|
ipv4:list = []
|
|
ipv4.append(value["ip"])
|
|
elif value["ipAttributes"]["isIpv6"]:
|
|
ipv6:list = []
|
|
if value["ip"].endswith("/127"):
|
|
ipv6_ptp:str = ipaddress.ip_network(value["ip"])[1]
|
|
else:
|
|
ipv6:list = []
|
|
ipv6.append(value["ip"])
|
|
ipassignment = {
|
|
"ipv4": ipv4,
|
|
"ipv6": ipv6,
|
|
"ipv6_ptp": ipv6_ptp[0],
|
|
"ipv6_nh": ipv6_ptp[1]
|
|
}
|
|
|
|
server_response = requests.get(f"https://{self.url}/api/servers/{self.serverid}").json()
|
|
server = {
|
|
"tags": server_response["result"]["tags"]
|
|
}
|
|
|
|
info = {
|
|
"id": self.serverid,
|
|
"server": server,
|
|
"switch": switch,
|
|
"ipassignment": ipassignment
|
|
}
|
|
return info
|
|
|