UCSDirectorExample.py

Version 2

    Attached is an example of Python script that calls UCSD northbound Web Services API. This has been tested with Python 2.7 and UCSD 5.2. The example show how to execute a Workflow in UCSD.

     

     

    Here is the Script:

     

     

    import urllib2

    import json

    import time

     

    ip_addr = "10.95.4.23"

    username = "admin"

    password = "admin"

    workflowName = "DeployESXi"

     

     

     

    def getApiKey (ip_addr, username, password):

        ip_addr = ip_addr

        username = username

        password = password

       

        url = 'http://'+ip_addr+'/app/api/rest?formatType=json&opName=getRESTKey&user='+username+'&password='+password

     

        # This packages the request (it doesn't make it)

        request = urllib2.Request(url=url)

     

        # Sends the request and catches the response

        response = urllib2.urlopen(request)

       

        return response.read().replace("\"", "")

     

     

    apiKey = getApiKey(ip_addr, username, password)

     

     

    url = 'http://'+ip_addr+'/app/api/rest?formatType=json&opName=userAPIGetMyLoginProfile&opData={}'

     

     

    # This packages the request (it doesn't make it)

    request = urllib2.Request(url=url, headers={"X-Cloupia-Request-Key" : apiKey})

     

    # Sends the request and catches the response

    response = urllib2.urlopen(request)

       

    print response.read()

     

     

     

     

    #######Execute a workflow#######

     

     

    url = 'http://'+ip_addr+'/app/api/rest?formatType=json&opName=userAPISubmitWorkflowServiceRequest&opData={param0:"DeployESXi",param1:{"list":[{"name":"Hostname","value":"Nike"}]},param2:-1}'

    request = urllib2.Request(url=url, headers={"X-Cloupia-Request-Key" : apiKey})

    response = urllib2.urlopen(request)

    j = json.loads(response.read())

    serviceRequest = j['serviceResult']

    print serviceRequest

     

     

    ######Check for completion######

    flag = 0

    while flag == 0:

        time.sleep(30)

        url = 'http://'+ip_addr+'/app/api/rest?formatType=json&opName=userAPIGetWorkflowStatus&opData={param0:'+str(serviceRequest)+'}'

        request = urllib2.Request(url=url, headers={"X-Cloupia-Request-Key" : apiKey})

        response = urllib2.urlopen(request)

        j = json.loads(response.read())

        status = j['serviceResult']

        if status == 1:

            print "Workflow "+workflowName+" with SR ID "+str(serviceRequest)+" is In Progress"

        elif status == 3:

            print "Workflow "+workflowName+" with SR ID "+str(serviceRequest)+" is Complete"

            flag = 1

        elif status == 2:

            print "Workflow "+workflowName+" with SR ID "+str(serviceRequest)+" Failed"

            flag = 1