通过python调用cloudstack接口,cloudstack 接口通过 admin key来判断是否有访问有权访问
1 #!/usr/bin/env python 2 import urllib2 3 import urllib 4 import hashlib 5 import hmac 6 import base64 7 8 9 baseurl='http://10.64.8.10:8080/client/api?' 10 11 #这个字典存放命令下的参数,这里是创建计算方案'command':'createServiceOffering', 12 request={ 13 'name':'json4c8g', 14 'command':'createServiceOffering', 15 'storagetype':'shared', 16 'memory':'8096', 17 'cpunumber':'4', 18 'displaytext':'asdasd', 19 'cpuspeed':'1800', 20 'domainid':'1', 21 } 22 23 #下面的这样写字典的方式主要是和上面有个区分,这里是必须写的参数 24 request['response']='json' 25 request['apikey']='LYohWAwKAKR_QKqM9Na02xyZ2pVSjvbrt2DRFaMrZuOs6zi9gyzxUkWtzIDSqjF-ZZ4UhD_8dIP6MG9opf3PTg' 26 secretkey='iO8rxr1rid29oHAO3ZiLKOoz6F6E9IWm9V2G7yUNd0zHI6XIEY0JvaBRzHo79p_dQ5NAGDCzTlk7229p4DQNKg' 27 request_str='&'.join(['='.join([k,urllib.quote_plus(request[k])]) for k in request.keys()]) 28 sig_str='&'.join(['='.join([k.lower(),urllib.quote_plus(request[k].lower().replace('+','%20'))])for k in sorted(request.iterkeys())]) 29 sig=urllib.quote_plus(base64.encodestring(hmac.new(secretkey,sig_str,hashlib.sha1).digest()).strip()) 30 31 req=baseurl+request_str+'&signature='+sig 32 33 res=urllib2.urlopen(req) 34 35 #打印返回结果 36 print res.read()
下面就是执行返回的结果,
[root@ucom pycloud]# ./cmd.py
{ "createserviceofferingresponse" : { "serviceoffering" : {"id":"385503cd-acce-4fe8-bbe5-885dd740ba4f","name":"json4c8g","displaytext":"asdasd","cpunumber":4,"cpuspeed":1800,"memory":8096,"created":"2016-08-24T17:06:01+0800","storagetype":"shared","offerha":false,"limitcpuuse":false,"isvolatile":false,"domainid":"740182cc-05d5-11e6-abef-005056830131","domain":"ROOT","issystem":false,"defaultuse":false,"iscustomized":false} } }