hi,
i am new to vcd api, how can i start working with rest api. any sample code in python will help a lot.
Hi,
Make sure your user exists in the specified organization. This works for me:
> curl -i -k -H "Accept:application/*+xml;version=5.1;" -u 'Administrator@System:<pass>' -X POST https://<ip>/api/sessions ~
HTTP/1.1 200 OK
Date: Tue, 22 Jul 2014 11:43:37 GMT
...
x-vcloud-authorization: <token>
Set-Cookie: vcloud-token=<token>; Secure; Path=/
Content-Type: application/vnd.vmware.vcloud.session+xml;version=5.1
Date: Tue, 22 Jul 2014 11:43:37 GMT
Transfer-Encoding: chunked
.. body
I wrote a small demo in python: https://www.sendspace.com/file/qt656m
You can use the requests python library. You will first need to get an authorization token (login). You can do this with basic authentication. Do a POST to <api-url>/sessions and pass the credentials in the Authorization header - Authorization: Basic <base64 credentials> - where <base64 credentials> is the base64 encoded string of the <user>@<org>:<pass> string. The response should have status code 200 and should contain the x-vcloud-authorization header. After you get this you can remove the Authorization header from subsequent requests and instead pass the x-vcloud-authorization header.
Some code below (note that I haven't tested it):
import requests
import base64
session = requests.Session()
credentials = str.encode("%s@%s:%s" % (user, org, password), "UTF-8")
encoded_credentials = base64.b64encode(credentials)
auth_header_value = "Basic %s" % encoded_credentials
authorization_header = {"Authorization": auth_header_value}
session.headers.update(authorization_header)
url = "<api-url>/sessions"
response = session.post(url)
if response.status_code == 200:
auth_token = response.headers["x-vcloud-authorization"]
session.headers.update({"x-vcloud-authorization": auth_token})
del session.headers["Authorization"]
else:
raise HttpError("Invalid status code: %s" % response.status_code)
Now you can execute requests against the API in the same way.
You may need to set this
session.verify = False
in order to skip SSL cert verification (in a dev environment).
import requests
import base64
session = requests.Session()
session.verify = False
credentials = str.encode("%s@%s:%s" % ('administrator','Scenerio2','nopasswd'), "UTF-8")
encoded_credentials = base64.b64encode(credentials)
auth_header_value = "Basic %s" % encoded_credentials
authorization_header = {"Authorization": auth_header_value}
session.headers.update(authorization_header)
url = "http://10.10.51.8/api/v1.5/login"
response = session.post(url)
print response
if response.status_code == 200:
uth_token = response.headers["x-vcloud-authorization"]
session.headers.update({"x-vcloud-authorization": auth_token})
del session.headers["Authorization"]
-------Its giving the error code 406,please help
Could you try to add the Accept header too: Accept: application/*+xml ?
And could you paste more information about the response - body and headers?
Actually it should probably be Accept: application/*+xml;version=<version>
Thanks for help
but i am getting error even through curl
Hi,
Make sure your user exists in the specified organization. This works for me:
> curl -i -k -H "Accept:application/*+xml;version=5.1;" -u 'Administrator@System:<pass>' -X POST https://<ip>/api/sessions ~
HTTP/1.1 200 OK
Date: Tue, 22 Jul 2014 11:43:37 GMT
...
x-vcloud-authorization: <token>
Set-Cookie: vcloud-token=<token>; Secure; Path=/
Content-Type: application/vnd.vmware.vcloud.session+xml;version=5.1
Date: Tue, 22 Jul 2014 11:43:37 GMT
Transfer-Encoding: chunked
.. body
I wrote a small demo in python: https://www.sendspace.com/file/qt656m
In the blow request, am i doing anything wrong, please help!! However my vapp is getting instantiated Through Rest Client.
method = "POST"
url = self.OrganizationRefLink+'/action/instantiateVAppTemplate'
print url
print xmlTemplate
handler = urllib2.HTTPSHandler()
opener = urllib2.build_opener(handler)
request = urllib2.Request(url)
request.add_header("Accept",'application/*+xml;version=1.5')
request.add_header("Content-Type",'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml')
print "Adding "+str(self.xVcloudAuthorization)
request.add_header("x-vcloud-authorization",self.xVcloudAuthorization)
hdl = open(xmlTemplate)
data = hdl.read()
print data
print "--------------"+data+"----------"
request.add_data(data)
request.get_method = lambda: method
try:
connection = opener.open(request)
except urllib2.HTTPError,e:
connection = e
Response:
[xyz@localhost vcloud-vapp]$ python vCloudClient.py
aVZYNCKtUG4hp1DFJDdYbAhb/xW7v3FyGH5k3pLhHYE=
Returning the Ref Link : https://10.10.51.8/api/org/fb412f68-1278-40a1-bda7-48b2716ad4f7for Ref: Scenerio2 .
Returning the Ref Link : https://10.10.51.8/api/vdc/f69a3b83-45d8-4863-8e7d-49e76e619043for Ref: Scenerio2 .
https://10.10.51.8/api/vdc/f69a3b83-45d8-4863-8e7d-49e76e619043
Returning the Ref Link : https://10.10.51.8/api/catalog/d8f6f580-3d15-47e9-8ece-dde4232828a9for Ref: testCatalog .
Returning the Ref Link : https://10.10.51.8/api/catalogItem/4651a27a-3559-442c-8905-3e75a315fed4for Ref: test_vAppTemplate .
Returning the Ref Link : https://10.10.51.8/api/vAppTemplate/vappTemplate-645b13f5-b4af-45d1-829d-cb62f71286b9for Ref: test_vAppTemplate .
Creating xml :vAppSanjeev_XML.xml from xml template :vAppTemplate.xml
https://10.10.51.8/api/vdc/f69a3b83-45d8-4863-8e7d-49e76e619043/action/instantiateVAppTemplate
vAppSanjeev_XML.xml
Adding aVZYNCKtUG4hp1DFJDdYbAhb/xW7v3FyGH5k3pLhHYE=
<?xml version="1.0" ?><InstantiateVAppTemplateParams deploy="false" name="vAppSanjeev" powerOn="false" xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1">
<Description>Created with the Automation Script</Description>
<Source href="https://10.10.51.8/api/vAppTemplate/vappTemplate-645b13f5-b4af-45d1-829d-cb62f71286b9"/>
</InstantiateVAppTemplateParams>
--------------<?xml version="1.0" ?><InstantiateVAppTemplateParams deploy="false" name="vAppSanjeev" powerOn="false" xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1">
<Description>Created with the Automation Script</Description>
<Source href="https://10.10.51.8/api/vAppTemplate/vappTemplate-645b13f5-b4af-45d1-829d-cb62f71286b9"/>
</InstantiateVAppTemplateParams>----------
ERRROR 500
[xyz@localhost vcloud-vapp]$
Could you provide the full response body and vCloud logs if possible? The returned code is not enough to debug this. Also by saying that "vapp is getting instantiated Through Rest Client", which Rest Client you are referring to?
i am doing it using python, however i am able to do it successfully using the firefox rest client. Is there any incorrect thing i have done in my python program ?
I am not sure, I need full response body and logs if possible.
HTTP Error 500: Server Error
ERRROR 500
<?xml version="1.0" encoding="UTF-8"?>
<Error xmlns="http://www.vmware.com/vcloud/v1.5" minorErrorCode="INTERNAL_SERVER_ERROR" message="Unable to perform this action. Contact your cloud administrator." majorErrorCode="500" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://10.10.51.8/api/v1.5/schema/master.xsd"></Error>
Hm, this doesn't help either. I would suggest to intercept the full request and response while invoking the operation through Firefox Rest Client and then see what's the difference in the request/response executed with your python program.