cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1548
Views
0
Helpful
0
Comments
markpsmi
Cisco Employee
Cisco Employee

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

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Quick Links