-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd42_api.py
113 lines (95 loc) · 3.99 KB
/
d42_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
## Device42 API Wrapper
## Built-in GET, POST, UPDATE, DELETE methods accessible via the d42_api object.
## Main method currently used for testing new methods.
import urllib3
import requests
import base64
from pprint import pprint
from getpass import getpass
import sys
import csv
urllib3.disable_warnings()
class d42_api:
def __init__(self,ipaddress,urluser,urlpass):
self.ipaddress = ipaddress
self.urluser = urluser
self.urlpassword = urlpass
self.base_url = f"https://{ipaddress}/api/"
self.headers = {"Authorization": f"Basic {str(base64.b64encode(bytes(urluser+':'+urlpass,encoding='utf-8')),encoding='utf-8')}"}
def get(self,url_path,params=None):
req = requests.get(f'{self.base_url}{url_path}',verify=False,headers=self.headers,params=params)
return req
def post(self,url_path,params=None,data=None):
req = requests.post(f'{self.base_url}{url_path}',verify=False,headers=self.headers,params=params,data=data)
return req
def update(self,url_path,params=None,data=None):
req = requests.put(f'{self.base_url}{url_path}',verify=False,headers=self.headers,params=params,data=data)
return req.status_code
def delete(self,url_path):
req = requests.delete(f'{self.base_url}{url_path}',verify=False,headers=self.headers)
return req.status_code
## Working with IP Addresses ##
def findIp(self,ipaddress):
results = self.get(url_path='1.0/search/',params=f"query=ip&string={ipaddress}")
return results.json()['ips']
def getAvailableIpsBySubnet(self,subnet):
results = self.get(url_path='1.0/ips/',params=f"available=yes&subnet_id={subnet}")
return results.json()['ips']
def nextIp(self,subnet):
results = self.get(url_path='1.0/suggest_ip/',params=f"subnet={subnet}")
return results.json()
def assignIp(self,ipaddress,label):
data = {
"ipaddress": ipaddress,
"available": "no",
"label": label,
"mac_address":"",
"device":"",
"type":"static"
}
results = self.post(url_path='1.0/ips/',data=data)
if results.status_code == 200:
return f"{ipaddress}/{label}: Configured successfully"
else:
return f"{ipaddress}/{label}: Something went wrong. Error {results.status_code}"
def assignIpToDevice(self,ipaddress,device):
data = {
"ipaddress": ipaddress,
"device":device,
}
results = self.post(url_path='1.0/ips/',data=data)
if results.status_code == 200:
return f"{ipaddress}/{device}: Configured successfully"
else:
return f"{ipaddress}/{device}: Something went wrong. Error {results.status_code}"
def unassignIp(self,ipaddress,type):
data = {
"ipaddress": ipaddress,
"available": "yes",
"label": "",
"mac_address":"",
"device":"",
"type":type
}
results = self.post(url_path='1.0/ips/',data=data)
if results.status_code == 200:
return f'{ipaddress} now set to available'
else:
return f'{ipaddress}: Something went wrong. Error {results.status_code}'
## Working with Devices ##
def findDevice(self,devname):
results = self.get(url_path=f'1.0/devices/name/{devname}')
return results.json()
def deleteDevice(self,devid):
result = self.delete(url_path=f'1.0/devices/{devid}/')
if result == 200:
return f"{devid}: Successfully deleted"
else:
return f"{devid}: Something went wrong. Error {result}"
## General Deployment information/health ##
def listAutoDiscovery(self,type='pingsweep'):
results = self.get(f'1.0/auto_discovery/{type}')
return results.json()['jobs']
def getHealthStats(self):
results = requests.get(f'https://{self.ipaddress}:4343/healthstats/',verify=False)
return results.json()