-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_api_lib.py
51 lines (39 loc) · 1.68 KB
/
rest_api_lib.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
import requests
import json
import sys
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:8443/'%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 b'<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)
print (payload)
response = self.session[self.vmanage_ip].post(url=url, data=payload, headers=headers, verify=False)
data = response.json()
return data