In this post, I am going to test how to CloudStack API works using Simulator.
Environment OS: Ubuntu 16.04 Java: openjdk version "1.8.0_131" Scala: 2.10 Build Tool: Apache Maven 3.5.0 IDE: IntelliJ CloudStack(simulator): 4.9
Run CloudStack simulator on a docker container It is very easy to run CloudStack simulator on a docker container. https://hub.docker.com/r/cloudstack/simulator/ Just execute the following command on your docker server.
docker pull cloudstack/simulator
docker run --name cloudstack -d -p 8080:8080 cloudstack/simulator
Then, you can connect to CloudStack management web GUI.
Generate API key and Secret key to access API Following this guide http://docs.cloudstack.apache.org/en/latest/dev.html, you need to generate keys to access CloudStack via API. You can generate the keys on GUI.
Click "generate keys", then API key and Secret key are created.
Generate signature To send HTTP GET request to CloudStack, you need to generate signature by encording api key and request URL. As it is a little bit complicated process for coding in Scala, and because this is just for a test purpose, I coded a program to send HTTP GET request in Python just by following the instruction in the guide.
import urllib2
import urllib
import hashlib
import hmac
import base64
baseurl='http://10.32.2.56:8080/client/api?'
request={}
request['command']='listUsers'
request['response']='json'
request['apikey']='***omitted***'
secretkey='***omitted***'
request_str='&'.join(['='.join([k,urllib.quote_plus(request[k])]) for k in request.keys()])
sig_str='&'.join(['='.join([k.lower(),urllib.quote_plus(request[k].lower().replace('+','%20'))])for k in sorted(request.iterkeys())])
#sig_str 'apikey=plgwjfzk4gys3momtvmjuvg-x-jlwlnfauj9gabbbf9edm-kaymmailqzzq1elzlyq_u38zcm0bewzgudp66mg&command=listusers&response=json'
sig=hmac.new(secretkey,sig_str,hashlib.sha1)
sig=hmac.new(secretkey,sig_str,hashlib.sha1).digest()
sig=base64.encodestring(hmac.new(secretkey,sig_str,hashlib.sha1).digest())
sig=base64.encodestring(hmac.new(secretkey,sig_str,hashlib.sha1).digest()).strip()
sig=urllib.quote_plus(base64.encodestring(hmac.new(secretkey,sig_str,hashlib.sha1).digest()).strip())
req=baseurl+request_str+'&signature='+sig
res=urllib2.urlopen(req)
print res.read()
Confirm CloudStack API returns expected values The simulator returned values as expected, therefore, this simulator can be used for further investigation.
C:\Users\>activate python27
(python27) C:\Users\>python "C:\Users\\Desktop\temp.py"
{"listusersresponse":{"count":1,"user":[{"id":"4bc88465-6b5a-11e7-9f99-0242ac110002","username":"admin","firstname":"Admin","lastname":"User","email":"[email protected]","created":"2017-07-18T01:42:02+0000","state":"enabled","account":"admin","accounttype":1,"usersource":"native","roleid":"56866a3d-6b5a-11e7-9f99-0242ac110002","roletype":"Admin","rolename":"Root Admin","domainid":"4bc8433a-6b5a-11e7-9f99-0242ac110002","domain":"ROOT","apikey":"AJ_88MyUSU8sDqcHd1JXQnW9T-_SbkV6-aCCpuSq8PvS-MH6r-uy-GBkX-1QqeZ49a-glnbL_Vvu2dz_en9XZw","accountid":"4bc8631f-6b5a-11e7-9f99-0242ac110002","iscallerchilddomain":false,"isdefault":true}]}}
Recommended Posts