Test CloudStack API Using Simulator
4657 단어 rest파이썬CloudStackapi
환경
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.
htps : // 후 b. 도 c r. 이 m / r / c ぉ ds ck / 시무와 r /
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 htp // // cs.ぉ ds ck. 아파치. 오 rg / 엔 / ㅁ st /에서 v. 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}]}}
Reference
이 문제에 관하여(Test CloudStack API Using Simulator), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/bmj0114/items/34f9e30c8778b163efa5텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)