Skip to content

Instantly share code, notes, and snippets.

@chmouel
Created March 29, 2011 15:11
Show Gist options
  • Save chmouel/892523 to your computer and use it in GitHub Desktop.
Save chmouel/892523 to your computer and use it in GitHub Desktop.
Use RackSpace Cloud Servers/File directly in python without binding
''' Implement a client for Rackspace Cloud Servers or Cloud
Files using pure python.
'''
import httplib
import urlparse
def get_auth(username, key, auth_url):
u = urlparse.urlparse(auth_url)
cls = u.scheme == 'https' and \
httplib.HTTPSConnection or httplib.HTTPConnection
h = cls(u.netloc)
headers = {"X-Auth-User": username,
"X-Auth-Key": key,
}
h.request("GET", u.path, headers=headers)
ret = {}
for k, v in h.getresponse().getheaders():
if k.startswith("x-"):
ret[k.replace("x-", "")] = v
return ret
def cs_get(auth, rest):
u = urlparse.urlparse(auth["server-management-url"])
cls = u.scheme == 'https' and \
httplib.HTTPSConnection or httplib.HTTPConnection
h = cls(u.netloc)
headers = {"X-Auth-Token": auth['auth-token']}
h.request("GET", u.path + rest, headers=headers)
resp = h.getresponse()
return resp
def cf_get(auth, rest):
u = urlparse.urlparse(auth["storage-url"])
cls = u.scheme == 'https' and \
httplib.HTTPSConnection or httplib.HTTPConnection
h = cls(u.netloc)
headers = {"X-Auth-Token": auth['auth-token']}
h.request("GET", u.path + rest, headers=headers)
resp = h.getresponse()
return resp
if __name__ == '__main__':
import json
auth = get_auth("USERNAME",
"API_KEY",
"https://auth.api.rackspacecloud.com/v1.0")
# List containers
container_listing = cf_get(auth, "").read()
# Get server details
server_details = \
json.loads(cs_get(auth, "/servers/detail").read())['servers']
# Get Images details
image_details = json.loads(cs_get(auth, "/images/detail").read())['images']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment