-
Notifications
You must be signed in to change notification settings - Fork 5
/
device42.py
120 lines (93 loc) · 3.54 KB
/
device42.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
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
import os
import requests
requests.packages.urllib3.disable_warnings()
class Device42BaseException(Exception):
pass
class Device42BadArgumentError(Exception):
pass
class Device42HTTPError(Device42BaseException):
pass
class Device42WrongRequest(Device42HTTPError):
pass
class Device42(object):
def __init__(self, endpoint, user, password, **kwargs):
self.base = endpoint
self.user = user
self.pwd = password
self.verify_cert = False
self.debug = kwargs.get('debug', False)
self.logger = kwargs.get('logger', None)
self.base_url = "%s" % self.base
self.headers = {}
def _send(self, method, path, data=None):
""" General method to send requests """
url = "%s/%s" % (self.base_url, path)
params = None
if method == 'GET':
params = data
data = None
resp = requests.request(method, url, data=data, params=params,
auth=(self.user, self.pwd),
verify=self.verify_cert, headers=self.headers)
if not resp.ok:
raise Device42HTTPError("HTTP %s (%s) Error %s: %s\n request was %s" %
(method, path, resp.status_code, resp.text, data))
retval = resp.json()
return retval
def _get(self, path, data=None):
return self._send("GET", path, data=data)
def _post(self, path, data):
if not path.endswith('/'):
path += '/'
return self._send("POST", path, data=data)
def _put(self, path, data):
if not path.endswith('/'):
path += '/'
return self._send("PUT", path, data=data)
def _delete(self, path):
return self._send("DELETE", path)
def _log(self, message, level="DEBUG"):
if self.logger:
self.logger.log(level.upper(), message)
def get_device_by_name(self, name):
path = "api/1.0/devices/name/%s" % name
return self._get(path)
def get_all_devices(self):
path = "api/1.0/devices/all/"
devices = []
init_data = self._get(path, {'limit': 1, 'offset': 0})
total_count = init_data['total_count']
i = 0
limit = 1000
while i < total_count:
devices_data = self._get(path, {'limit': limit, 'offset': i})
devices = devices + devices_data['Devices']
i += limit
return devices
def doql(self, url, method, query=None):
path = url
if query is None:
query = "SELECT * FROM view_device_v1 order by device_pk"
data = {"output_type": "json", "query": query}
result = self._post(path, data)
return result
def request(self, source_url, method, model):
models = []
if method == "GET":
result = self._get(source_url)
if model in result:
models = result[model]
limit = 0
total_count = 0
if "limit" in result:
limit = result["limit"]
if "total_count" in result:
total_count = result["total_count"]
offset = limit
while offset < total_count:
result = self._get(source_url, data={"offset":offset, "limit":limit})
if model in result:
models += result[model]
offset += limit
return models