Unsolved
7 Posts
0
8516
connect to vplex restapi via python?
Hi there,
Is it possible to connect to vplex rest api via python? if so, is there any sample code that I can refer to? thanks!
Thank You
Jeffrey
Unsolved
7 Posts
0
8516
Hi there,
Is it possible to connect to vplex rest api via python? if so, is there any sample code that I can refer to? thanks!
Thank You
Jeffrey
Top
ThomDaleyPPI
108 Posts
1
August 6th, 2018 05:00
Yes it is possible I have attached a python script here which will display the details of a VPLEX context. I've tested it quite a few times and it does work. Below is the output from a run that displays the information for a cluster-witness:
Output:
Enter hostname or IP address> 10.250.X.X
Enter username> service
Enter password>
Type in the context to display data> cluster-witness
admin-state enabled
name cluster-witness
private-ip-address 128.221.254.3
public-ip-address 10.250.X.X
components components-in-cluster-witness
Code:
#################################################################################################
# #
# This python script uses REST on VPLEX to grab config data. The user is prompted #
# for the context and the data is displayed on separate lines. #
# need to add a check for dict object type #
#################################################################################################
import json
import requests
import csv
import re
import getpass
import sys
requests.packages.urllib3.disable_warnings()
#Get user input
hostname = input("Enter hostname or IP address> ")
username = input("Enter username> ")
passwd = getpass.getpass("Enter password> ")
context = input("Type in the context to display data> ")
headers = {'content-type': 'application/json'}
url = 'https://' + hostname + '/vplex/' + context
response = requests.get(url, auth=(username,passwd), headers=headers, verify=False)
if response.status_code != 200:
sys.exit("JSON Request failed error code: " + str(response.status_code))
parsed_json = json.loads(response.text)
for ini in parsed_json["response"]["context"]:
clusterid = ini["parent"]
if re.search("cluster", clusterid):
cid = clusterid[10:19]
print('Cluster ID: ' + cid)
#print(response.text)
for x in range(len(ini["attributes"])):
#Test for value type. If it's a null, then skip it. If it's a list then iterate through it. If it's a string then print it.
if ini["attributes"] ["value"] == None:
continue
elif isinstance(ini["attributes"] ["value"], list):
#Iterate through the list
strList = ''
strName = ini["attributes"] ["name"]
for l in range(len(ini["attributes"] ["value"])):
strList = ini["attributes"] ["value"] + ',' + strList
prtName = strName + ' ' + strList
print(prtName)
else:
print(ini["attributes"] ["name"] + ' ' + ini["attributes"] ["value"])
for y in range(len(ini["children"])):
print(ini["children"] ["name"] + ' ' + ini["children"] ["type"])
UsoppJazz
7 Posts
0
August 7th, 2018 20:00
Thank you for the code sample.
weird I am getting authentication error. Cross checked, my credential is correct.
error
=====================================
{
"response": {
"context": null,
"message": "User authentication failed.",
"exception": null,
"custom-data": null
}
}
==============================
I added the following to your code to bypass SSL error
====Code======
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL'
try:
requests.packages.urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST += 'HIGH:!DH:!aNULL'
except AttributeError:
pass
=====End ===========
ThomDaleyPPI
108 Posts
0
August 8th, 2018 05:00
By the way, this works for python v3.7.
ThomDaleyPPI
108 Posts
0
August 8th, 2018 09:00
Have you tried printing the username and password to the console before you attempt to login? Maybe something is getting added or removed in the process of assigning the strings to username and password. You could also temporarily disable the inputs for username and password and put them in manually here:
response = requests.get(url, auth=(service,P@ssword), headers=headers, verify=False)
I would try the above to just to verify it works.
UsoppJazz
7 Posts
0
August 9th, 2018 18:00
I am using python 3.7.
I tried entering directly with the same result.
response =response.get(url,auth=(username,password),header=headers,verify.false)
the syntax is definitely correctly as I was able to connect to my github account with this method, not sure why I cannot get it past VPLEX authentication though.
UsoppJazz
7 Posts
1
August 10th, 2018 01:00
I managed to connect in by adding credential in header instead.
thanks for pointing me in the right direction.
header = {'content-type': 'application/json, 'Username': ' ', 'Password': ' '}
request.get (url, verify=false, headers=headers)
ThomDaleyPPI
108 Posts
1
August 10th, 2018 06:00
Good news. I have other scripts as well but this is the first I wrote and it's been the foundation for the others. The biggest piece is iterating through the attributes, finding the appropriate names, determining the type of object the value is and then printing it out.
This loop looks for type virtual-volume and then finds the values for a few things I need for my documentation:
for ini in parsed_json["response"]["context"]:
if ini["type"] == 'virtual-volume':
for x in range(len(ini["attributes"])):
if ini["attributes"] ["name"] == 'name':
volName = ini["attributes"] ["value"]
if ini["attributes"] ["name"] == 'vpd-id':
vpdId = ini["attributes"] ["value"]
vpdId = re.sub('VPD83T3:', '', vpdId)
if ini["attributes"] ["name"] == 'capacity':
capBytes = ini["attributes"] ["value"]
if ini["attributes"] ["name"] == 'locality':
local = ini["attributes"] ["value"]
clusterid = ini["parent"]
if re.search("cluster", clusterid):
cid = clusterid[10:19]
if cid == 'cluster-1':
dc = 'PDC'
else:
dc = 'SDC'
strLine = volName + ',' + vpdId + ',' + capBytes + ',' + local + ',' + dc
print(strLine)
strLine = strLine + '\n'
f.write(strLine)
Happy scripting!
UsoppJazz
7 Posts
0
August 12th, 2018 19:00
Thank You for sharing the code, definitely helpful as a foundation.
agree that iterating through the attributes is a challenge.
Took me a while to understand how to grab just the virtual volume name from storage-view output.
pprint.pprint(jsondata["response"]["context"][0]["attributes"][7])
garyo
89 Posts
0
February 1st, 2019 15:00
NehaBorage
2 Posts
0
May 15th, 2020 05:00
headers = {'Username': "service", 'Password': "password", 'content-type': 'application/json', 'args' : "-c cluster-1"}
#args = {'args': "--cluster cluster-1"}
#headers = {'content-type': 'application/json'}
#headers = {'args' : "-c cluster-1"}
url = 'https://' + hostaddress + '/vplex/export+storage-view+find-unmapped-volumes'
response = requests.post(url, headers=headers, verify=False)
getting below error during passing of the arguments will you be able to help
xyz-vp01.ikea.com
{
"response": {
"context": null,
"message": "export storage-view find-unmapped-volumes",
"exception": "export storage-view Evaluation of < > failed.\ncause: Command execution failed.\ncause: missing argument --cluster: Unable to find a\n reasonable default value for this option when\n this command is executed in the current context.\n",
"custom-data": null
}
}
ankur.patel
286 Posts
0
May 19th, 2020 07:00
@NehaBorage Are you trying to get a list of volumes that arent in a storage view?
NehaBorage
2 Posts
0
June 9th, 2020 00:00
@ankur.patel - yes I am trying to get list of volumes not in Storage view
ankur.patel
286 Posts
0
June 9th, 2020 06:00
Im not well versed with python yet. I used postman to create a query. Im able to convert it to python code. Let me know if this helps. Im guessing the part you care about is the URL
import http.client
import mimetypes
conn = http.client.HTTPSConnection("10.x.x.x")
payload = ''
headers = {
'Authorization': 'Basic c2VydmljZTpNaUBEaW03VDE=',
'Cookie': 'JSESSIONID=A77047C1FADB9086668454E8848F575F'
}
conn.request("GET", "/vplex/v2/clusters/cluster-2/virtual_volumes?service_status=unexported", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))