Python ,vCloud Director REST API Series for Beginners – Just get the list of Org

Hola Everybody, (I’m not Spanish or Catalan but under effect this days)

In my first article about vCloud REST and API , i just tried to describe how to get the token with very bad coding example in WordPress. Its good to find a way to share codes from WordPress, thanks for this article.

In this article I added getting org list from vCloud Director also made error handling and XML parse with ElementTree

I’m expecting in next article start to create something inside vCloud Director with REST API and python and extend the code


# coding=utf-8
def get_vcloud_token(vcd_hostname, password, api_version):
    import urllib2
    import base64

    vcloudsession_url = "https://" + vcd_hostname + "/api/sessions"
    encode_credentials = base64.b64encode(password)

    # Prepare headers we will post
    vcloudapi_version = "application/*+xml;version=" + api_version
    vcloudapi_auth = "Basic %s" % encode_credentials
    data = ""
    authheaders = {'Accept': vcloudapi_version, 'Authorization': vcloudapi_auth}
    request_format = urllib2.Request(vcloudsession_url, data, authheaders)
    make_request = urllib2.urlopen(request_format)
    # We will not need to read whole html out
    # result = make_request.read()
    # Just need to catch key provided by vCloud Director from header response
    token = make_request.info()['x-vcloud-authorization']
    # this function will turn result to get and use it where it called
    return token

def get_vcloud_org_list(exisiting_token):
    import urllib2
    import xml.etree.cElementTree as ET

    vcloudapi_url = "https://v.dorukcloud.com/api/" + "org/"
    api_version = "5.5"
    vcloudapi_version = "application/*+xml;version=" + api_version

    # Now use the requested token before for query the org list
    afterauthheader_org = {'Accept': vcloudapi_version, 'x-vcloud-authorization': exisiting_token}

    # Be careful, there is no PUT request here, we will make a GET because of that second parameter
    # described with headers = afterauthheader_org and if you do not set headers then second parameters
    # will be accepted as a data
    make_org_list_call = urllib2.Request(vcloudapi_url, headers=afterauthheader_org)

    # Also lets improve our code with exception handling
    try:
        make_org_list_request = urllib2.urlopen(make_org_list_call)
        # Now we take the response as a file like object
        org_list_response = make_org_list_request.read()
        org_list_in_xml = ET.fromstring(org_list_response)
        # Here because of result is file like its a string we are importing it and ET will parse it
        org_list_in_xml = ET.fromstring(org_list_response)

        # Pls print response to understand how is the result
        # You will see that its an XML out and OrgList is will be the root tag or root element, it will have some child(s)
        # it will be Org and Org will have some attributes like Name , Href and type
        # we will take the name attributes of each child(s)

        # Create an empty array
        org_name_array = []

        for child in org_list_in_xml:
            # Add child.attrib['name'] inside the array
            org_name_array.append(child.attrib['name'])

        return org_name_array

    except urllib2.URLError,e:
        print 'I guess wrong URL, SORRY ! '

# This is another function defined and name is main, you can see that inside main function we are calling get_vcloud_token function

def main():
    token_result = get_vcloud_token("you_vcloud_url", "Sit_Lord@System:password", "5.5")
    #This lines for tests , you can use it after remove the #
    #print token_result

    org_names = get_vcloud_org_list(token_result)

    #This lines for tests , you can use it after remove the #
    #print type(org_names)
    #for i in org_names:
    #    print i

main()

Posted on 10/12/2014, in Python, REST API and tagged , , , , , , . Bookmark the permalink. 3 Comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: