-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
67 lines (52 loc) · 2.35 KB
/
api.py
File metadata and controls
67 lines (52 loc) · 2.35 KB
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
import requests
import sys
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
class rest_api_lib:
def __init__(self, vmanage_ip, username, password):
self.vmanage_ip = vmanage_ip
self.session = {}
self.login(self.vmanage_ip, username, password)
def login(self, vmanage_ip, username, password):
"""Login to vmanage"""
base_url_str = 'https://%s/'%vmanage_ip
login_action = '/j_security_check'
#Format data for loginForm
login_data = {'j_username' : username, 'j_password' : password }
#Url for posting login data
login_url = base_url_str + login_action
url = base_url_str + login_url
sess = requests.session()
#If the vmanage has a certificate signed by a trusted authority change verify to True
login_response = sess.post(url=login_url, data=login_data, verify=False)
if '<html>' in login_response.content:
print "Login Failed"
sys.exit(0)
self.session[vmanage_ip] = sess
def get_request(self, mount_point):
"""GET request"""
url = "https://%s:8443/dataservice/%s"%(self.vmanage_ip, mount_point)
print url
response = self.session[self.vmanage_ip].get(url, verify=False)
data = response.content
return data
def post_request(self, mount_point, payload, headers={'Content-Type': 'application/json'}):
"""POST request"""
url = "https://%s:8443/dataservice/%s"%(self.vmanage_ip, mount_point)
payload = json.dumps(payload)
response = self.session[self.vmanage_ip].post(url=url, data=payload, headers=headers, verify=False)
data = response.content
def main(args):
if not len(args) == 3:
print __doc__
return
vmanage_ip, username, password = args[0], args[1], args[2]
obj = rest_api_lib(vmanage_ip, username, password)
#Example request to get devices from the vmanage "url=https://vmanage.viptela.com/dataservice/device"
response = obj.get_request('device')
print response
#Example request to make a Post call to the vmanage "url=https://vmanage.viptela.com/dataservice/device/action/rediscover"
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
main('34.203.116.253','admin','admin')