UNSOLVED

UsoppJazz

updated

8 years ago

U

UsoppJazz

7 Posts

0

8768

August 5th, 2018 20:00

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

  • ThomDaleyPPI

    1 Rookie

    •

    108 Posts

    7885

    1

    Posted 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

    7885

    0

    Posted 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

    1 Rookie

    •

    108 Posts

    7885

    0

    Posted August 8th, 2018 05:00

    By the way, this works for python v3.7.

  • ThomDaleyPPI

    1 Rookie

    •

    108 Posts

    7885

    0

    Posted 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

    7885

    0

    Posted 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

    7885

    1

    Posted 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

    1 Rookie

    •

    108 Posts

    7885

    1

    Posted 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

    7885

    0

    Posted 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

    7765

    0

    Posted February 1st, 2019 15:00

    Not sure if you've seen this, but in terms of sample scripts there's a zip file on every VPLEX management server in /opt/emc/VPlex/clientscripts.zip. This is a copy: https://community.emc.com/docs/DOC-64817 Gary
  • 824

    0

    Posted 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
    }
    }