Hi Kirk,

Pasting a rudimentary py script that fires a VM create operation, and in doing 
so computes and passes on the signature to the VM create API. You can generate 
the apikey and secretkey for the user in the CS GUI. 


import httplib
import urllib
import urllib2
import hmac
import binascii
from hashlib import sha1
import base64
import os

ip = "10.223.76.20"
port = 8080

apikey = 
"SFt5KoxRZ3hiQR03MMppTcKQH5iZH47vcvJcADtcD-vl_MUXyPj_S9910AwCdotXZ2ImRwXEzJd30Nk-mjZPQw"

secretkey = 
"IhV4-fq2Xv-HC29_CtqfQGQo30roW7cjiAzU345O-6C9qdVXUwcOPeF5MBGNTwi3TWfWoggI-Iy6pYQsfnju6A"

respType = "json"
command = "deployVirtualMachine"
zoneId = "8ad0af04-3a20-4d2c-877f-000a568cf23c"
templateId = "1ce7d8c0-a6af-11e2-a9d9-000c29db3315"
hypervisor = "VMware"
serviceOfferingId = "300c45a9-3129-445f-9acf-3f8f14fe81c0"
networkIds = "6f4e78c2-0cd0-4807-b10d-1a8afe42eaa8"
displayname = "vm14"
#autogenerate 15k of userdata.
userdata = b'%s'%(os.urandom(20*1024))

requests = {}
requests["apiKey"] = apikey
requests["response"] = respType
requests["command"] = command
requests["zoneId"] = zoneId
requests["hypervisor"] = hypervisor
requests["serviceOfferingId"] = serviceOfferingId
requests["userdata"] = base64.b64encode(userdata)
requests["displayname"] = displayname
requests["templateId"] = templateId
requests["networkIds"] = networkIds
request = zip(requests.keys(), requests.values())
request.sort(key=lambda x: str.lower(x[0]))

requestUrl = urllib.urlencode(request)
hashStr = "&".join(["=".join([str.lower(r[0]), 
str.lower(urllib.quote_plus(str(r[1]))).replace("+", "%20")]) for r in request])

sig = urllib.quote_plus(base64.encodestring(hmac.new(secretkey, hashStr, 
sha1).digest()).strip())
requestUrl += "&signature=%s"%sig

protocol = "http"
path = "/client/api"

url = "%s://%s:%d%s"%(protocol, ip, port, path)
req = urllib2.Request(url, requestUrl)
connection = urllib2.urlopen(req)
response = connection.read()
print response


Regards,
Vijay

-----Original Message-----
From: Kirk Jantzer [mailto:kirk.jant...@gmail.com] 
Sent: Wednesday, April 17, 2013 11:16 AM
To: Cloudstack users mailing list
Subject: Re: API - listTemplates, not working or I am doing something wrong??

Does anyone have a script for creating signatures?


On Wed, Apr 17, 2013 at 1:31 PM, Kirk Jantzer <kirk.jant...@gmail.com>wrote:

> gah. *facepalm* deployvirtualmachine works without a signature, so i 
> didn't think of that.. thanks!
>
>
> On Wed, Apr 17, 2013 at 1:23 PM, Edward Valencia <edwa...@redapt.com>wrote:
>
>> The api call needs to have a signature created based on the API and 
>> secrete key, I think this is whats missing in your call:
>>
>>
>> http://cloudstack.apache.org/docs/en-US/Apache_CloudStack/4.0.1-incub
>> ating/html/API_Developers_Guide/signing-api-requests.html
>>
>> Also commands are case sensitive but the way and should be used as in 
>> they are in the API docs. Hope this help.
>>
>>
>> On Wednesday, April 17, 2013 at 10:19 AM, Kirk Jantzer wrote:
>> > ok, maybe it's not just 'listtemplates' - I've tried other commands 
>> > and
>> get
>> > the same 'unable to verify...' reply....thoughts??
>> >
>> >
>> > On Wed, Apr 17, 2013 at 1:13 PM, Kirk Jantzer 
>> > <kirk.jant...@gmail.com(mailto:
>> kirk.jant...@gmail.com)>wrote:
>> >
>> > > When I do the following:
>> > >
>> > >
>> > >
>> api?command=listTemplates&templatefilter=featured&apikey=******&respo
>> nse=json
>> > >
>> > > The result is the following (I have tried the API key of a domain
>> admin
>> > > and root admin, as well as a cookie session after GUI login - all
>> yielded
>> > > same result):
>> > >
>> > > { "listtemplatesresponse" :
>> {"uuidList":[],"errorcode":401,"errortext":"unable to verify user 
>> credentials and/or request signature"} }
>> > >
>> > >
>> > > Per this forum post, there was a suggestion to change the case of 
>> > > the commands - that changed the output, but still didn't work:
>> > >
>> > >
>> > >
>> api?command=listtemplates&templatefilter=featured&apikey=******&respo
>> nse=json
>> > >
>> > > { "errorresponse" : 
>> > > {"uuidList":[],"errorcode":432,"errortext":"The
>> given command does not exist"} }
>> >
>> >
>> > --
>> > Regards,
>> >
>> > Kirk Jantzer
>> > c: (678) 561-5475
>> >
>> >
>>
>>
>>
>
>
> --
> Regards,
>
> Kirk Jantzer
> c: (678) 561-5475
>



--
Regards,

Kirk Jantzer
c: (678) 561-5475

Reply via email to