Search code examples
javapythonstackcloudapache-cloudstack

How to use cloud stack API Using java / python?


Is there any way to use cloud stack API along with java / python?

In the clouds stack documents they only provided the information about the interface of the API. how to use those API along with programming language?


Solution

  • CloudStack API’s can be accessed using

    Using Command Line Interface (CLI) CloudMonkey - https://cwiki.apache.org/CLOUDSTACK/cloudstack-cloudmonkey-cli.html

    Using Https Requests - http://cloudstack.apache.org/docs/en-US/Apache_CloudStack/4.0.0-incubating/html-single/API_Developers_Guide/

    Using CloudStack Clients - https://github.com/jasonhancock/cloudstack-python-client

    We can make two types of Http requests

    1. Unauthenticated API Requests using port 8096 (open port 8096 using management UI)
    2. Authenticated API Requests using signature

    Sample Python code to create Signature

    import urllib2
    import urllib // to make the url request 
    import hashlib              //encode it to http
    import hmac
    import base64      //encording 
    
    request={}
    request['command']='listUsers'
    request['response']='xml'
    request['apikey']='zdfhgsdhfgseahyg'
    secretkey='ghfgfgfg'
    
    >>> request
    {'apikey': 'plgWJfZK4gyS3mOMTVmjUVg-X-jlWlnfaUJ9GAbBbf9EdM-kAYMmAiLqzzq1ElZLYq_u38zCm0bewzGUdP66mg', 'command': 'listUsers', 'response': 'json'}
    
    >>>request_url="&".join(["=".join([r,urllib.quote_plus(request[r])]) for r in request.keys()])
    
    >>>sig_url="&".join(["=".join([r.lower(),urllib.quote_plus(request[r]).lower()]) for r in sorted(request.iterkeys())])
    
    >>>sig=urllib.quote_plus(base64.encodestring(hmac.new(secretkey,sig_url,hashlib.sha1).digest()).strip())
    
    >>> req=url+request_url+'&signature='+sig
    >>> res=urllib2.urlopen(req)
    >>> res.read()