-
Notifications
You must be signed in to change notification settings - Fork 3
/
nitro.py
executable file
·88 lines (64 loc) · 2.63 KB
/
nitro.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
#!/usr/bin/env python
"""nitro.py: Client for the Citrix NetScaler NITRO API."""
__author__ = "Simon Lauger"
__copyright__ = "Copyright 2018, IT Consulting Simon Lauger"
__license__ = "Apache"
__version__ = "0.0.1"
__maintainer__ = "Simon Lauger"
__email__ = "[email protected]"
import requests
class NitroClient():
def __init__(self, url, username, password):
self._url = url
self._headers = {
'X-NITRO-USER': username,
'X-NITRO-PASS': password,
'Accept': 'application/json',
'Content-Type': 'application/json',
}
self._verify = True
def set_url(self, url):
self._url = url
def set_username(self, username):
self._headers['X-NITRO-USER'] = username
def set_password(self, password):
self._headers['X-NITRO-PASS'] = password
def set_verify(self, verify):
self._verify = verify
def on_error(self, action):
self._headers['X-NITRO-ONERROR'] = action
def request(self, method, endpoint, objecttype, objectname = None, params = None, data = None):
url = self._url + '/nitro/v1/' + endpoint + '/' + objecttype
if objectname != None:
url += '/' + objectname
if params != None:
url += '?'
if isinstance(params, dict):
for key, value in params.iteritems():
url += key + "=" + value
else:
url += params
method_callback = getattr(requests, method)
self._result = method_callback(
url,
data=data,
headers=self._headers,
verify=self._verify,
)
return self._result
def get_stat(self, objecttype, objectname = None, params = None):
return self.request(get, 'stat', objecttype, objectname, params)
def get_config(self, objecttype, objectname = None, params = None):
return self.request(get, 'config', objecttype, objectname, params)
def post_stat(self, objecttype, objectname = None, params = None):
return self.request(post, 'stat', objecttype, objectname, params)
def post_config(self, objecttype, objectname = None, params = None):
return self.request(post, 'config', objecttype, objectname, params)
def put_stat(self, objecttype, objectname = None, params = None):
return self.request(put, 'stat', objecttype, objectname, params)
def put_config(self, objecttype, objectname = None, params = None):
return self.request(put, 'config', objecttype, objectname, params)
def delete_stat(self, objecttype, objectname = None, params = None):
return self.request(delete, 'stat', objecttype, objectname, params)
def delete_config(self, objecttype, objectname = None, params = None):
return self.request(delete, 'config', objecttype, objectname, params)