This content is part of the Python Zone, which is presented to you by DZone and New Relic. Visit the Python Zone for news, tips, and tutorials on the Python programming language. provides the resources and best practices to help you monitor these applications.
So, my request to enter on the free and private beta season of the new HP Cloud Services was gently accepted by the HP Cloud team, and today I finally got some time to play with the OpenStack API at HP Cloud. I’ll start with the first impressions I had with the service:
The user interface of the management is very user-friendly, the design is much like that of the Twitter Bootstrap, see the screenshot below of the “Compute” page from the “Manage” section:
Since they are using OpenStack, I really think that they should have imported the vocabulary of the OpenStack into the user interface, and instead of calling it “Size”, it would be more sensible to use “Flavour“.
The user interface still doesn’t have many features, something that I would really like to have is a “Stop” or something like that for the instances, only the “Terminate” function is present on the Manage interface, but those are details that they should be still working on since they’re only in beta.
Another important info to cite is that the access to the instances are done through SSH using a generated RSA key that they provide to you.
Let’s dig into the OpenStack API now.
To access the OpenStack API you’ll need the credentials for the authentication, HP Cloud services provide these keys on the Manage interface for each zone/service you have, see the screenshot below (with keys anonymized of course):
Now, OpenStack authentication could be done in different schemes, the scheme that I know that HP supports is the token authentication. I know that there is a lot of clients already supporting the OpenStack API (some have no documentation, some have weird API design, etc.), but the aim of this post is to show how easy would be to create a simple interface to access the OpenStack API using Python and Requests(HTTP for Humans !).
Let’s start defining our authentication scheme by sub-classing Requests AuthBase:
02.def __init__(self, auth_user, auth_key):
03.self.auth_key = auth_key
04.self.auth_user = auth_user
06.def __call__(self, r):
07.r.headers['X-Auth-User'] = self.auth_user
08.r.headers['X-Auth-Key'] = self.auth_key
As you can see, we’re defining the X-Auth-User and the X-Auth-Key in the header of the request with the parameters. These parameters are respectively your Account ID and Access Key we cited earlier. Now, all you have to do is to make the request itself using the authentication scheme, which is pretty easy using Requests:
1.ENDPOINT_URL = 'https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/'
2.ACCESS_KEY = 'Your Access Key'
3.ACCOUNT_ID = 'Your Account ID'
4.response = requests.get(ENDPOINT_URL, auth=OpenStackAuth(ACCOUNT_ID, ACCESS_KEY))
And that is it, you’re done with the authentication mechanism using just a few lines of code, and this is how the request is going to be sent to the HP Cloud service server:
This request is sent to the HP Cloud Endpoint URL (https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/). Let’s see now how the server answered this authentication request:
You can show this authentication response using Requests by printing the header attribute of the request Response object. You can see that the server answered our request with two important header items: X-Server-Management-URL and the X-Auth-Token. The management URL is now our new endpoint, is the URL we should use to do further requests to the HP Cloud services and the X-Auth-Token is the authentication Token that the server generated based on our credentials, these tokens are usually valid for 24 hours, although I haven’t tested it.
What we need to do now is to sub-class the Requests AuthBase class again but this time defining only the authentication token that we need to use on each new request we’re going to make to the management URL:
2.def __init__(self, request):
3.self.auth_token = request.headers['x-auth-token']
5.def __call__(self, r):
6.r.headers['X-Auth-Token'] = self.auth_token
Note that the OpenStackAuthToken is receiving now a response request as parameter, copying the X-Auth-Token and setting it on the request.
Let’s consume a service from the OpenStack API v.1.1, I’m going to call the List Servers API function, parse the results using JSON and then show the results on the screen:
01.# Get the management URL from the response header
02.mgmt_url = response.headers['x-server-management-url']
04.# Create a new request to the management URL using the /servers path
05.# and the OpenStackAuthToken scheme we created
06.r_server = requests.get(mgmt_url + '/servers', auth=OpenStackAuthToken(response))
08.# Parse the response and show it to the screen
09.json_parse = json.loads(r_server.text)
10.print json.dumps(json_parse, indent=4)
And this is what we get in response to this request:
16."name": "Server 22378"
31."name": "Server 11921"
And that is it, now you know how to use Requests and Python to consume OpenStack API. If you wish to read more information about the API and how does it works, you can read the documentation here.
来自 “ ITPUB博客 ” ，链接：http://blog.itpub.net/301743/viewspace-733634/，如需转载，请注明出处，否则将追究法律责任。