2011-02-25 02:00:13 -08:00
|
|
|
# Copyright 2010 Jacob Kaplan-Moss
|
2011-02-24 13:54:10 -04:00
|
|
|
"""
|
|
|
|
OpenStack Client interface. Handles the REST calls and responses.
|
|
|
|
"""
|
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
import time
|
|
|
|
import urlparse
|
|
|
|
import urllib
|
|
|
|
import httplib2
|
2011-05-25 14:10:20 +04:00
|
|
|
import logging
|
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
|
|
|
|
# Python 2.5 compat fix
|
|
|
|
if not hasattr(urlparse, 'parse_qsl'):
|
|
|
|
import cgi
|
|
|
|
urlparse.parse_qsl = cgi.parse_qsl
|
|
|
|
|
2011-02-26 05:04:40 -04:00
|
|
|
import novaclient
|
2011-08-03 16:36:03 -04:00
|
|
|
from novaclient.v1_1 import exceptions
|
2011-01-25 14:01:22 -06:00
|
|
|
|
2011-05-25 14:10:20 +04:00
|
|
|
_logger = logging.getLogger(__name__)
|
2011-01-25 14:01:22 -06:00
|
|
|
|
2011-08-03 16:36:03 -04:00
|
|
|
class HTTPClient(httplib2.Http):
|
2011-01-25 14:01:22 -06:00
|
|
|
|
2011-02-26 05:04:40 -04:00
|
|
|
USER_AGENT = 'python-novaclient/%s' % novaclient.__version__
|
2011-01-25 14:01:22 -06:00
|
|
|
|
2011-07-07 21:07:49 +00:00
|
|
|
def __init__(self, user, apikey, projectid, auth_url, timeout=None):
|
2011-08-03 16:36:03 -04:00
|
|
|
super(HTTPClient, self).__init__(timeout=timeout)
|
2011-01-25 14:01:22 -06:00
|
|
|
self.user = user
|
|
|
|
self.apikey = apikey
|
2011-06-09 10:39:13 +04:00
|
|
|
self.projectid = projectid
|
2011-01-25 14:01:22 -06:00
|
|
|
self.auth_url = auth_url
|
2011-07-12 14:41:56 -07:00
|
|
|
self.version = 'v1.0'
|
2011-01-25 14:01:22 -06:00
|
|
|
|
|
|
|
self.management_url = None
|
|
|
|
self.auth_token = None
|
|
|
|
|
|
|
|
# httplib2 overrides
|
|
|
|
self.force_exception_to_status_code = True
|
|
|
|
|
2011-05-25 14:10:20 +04:00
|
|
|
def http_log(self, args, kwargs, resp, body):
|
2011-05-25 17:10:53 +04:00
|
|
|
if not _logger.isEnabledFor(logging.DEBUG):
|
|
|
|
return
|
2011-08-03 16:36:03 -04:00
|
|
|
|
2011-05-25 16:48:40 +04:00
|
|
|
string_parts = ['curl -i']
|
2011-05-25 14:10:20 +04:00
|
|
|
for element in args:
|
|
|
|
if element in ('GET','POST'):
|
2011-05-25 16:48:40 +04:00
|
|
|
string_parts.append(' -X %s' % element)
|
2011-05-25 14:10:20 +04:00
|
|
|
else:
|
2011-05-25 16:48:40 +04:00
|
|
|
string_parts.append(' %s' % element)
|
2011-05-25 14:10:20 +04:00
|
|
|
|
|
|
|
for element in kwargs['headers']:
|
2011-05-25 16:48:40 +04:00
|
|
|
string_parts.append(' -H "%s: %s"' % (element,kwargs['headers'][element]))
|
|
|
|
|
|
|
|
_logger.debug("REQ: %s\n" % "".join(string_parts))
|
2011-05-25 14:10:20 +04:00
|
|
|
_logger.debug("RESP:%s %s\n", resp,body)
|
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
def request(self, *args, **kwargs):
|
|
|
|
kwargs.setdefault('headers', {})
|
|
|
|
kwargs['headers']['User-Agent'] = self.USER_AGENT
|
|
|
|
if 'body' in kwargs:
|
|
|
|
kwargs['headers']['Content-Type'] = 'application/json'
|
|
|
|
kwargs['body'] = json.dumps(kwargs['body'])
|
|
|
|
|
2011-08-03 16:36:03 -04:00
|
|
|
resp, body = super(HTTPClient, self).request(*args, **kwargs)
|
2011-05-25 14:10:20 +04:00
|
|
|
|
|
|
|
self.http_log(args, kwargs, resp, body)
|
2011-08-03 16:36:03 -04:00
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
if body:
|
|
|
|
try:
|
|
|
|
body = json.loads(body)
|
|
|
|
except ValueError, e:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
body = None
|
|
|
|
|
2011-07-07 21:07:49 +00:00
|
|
|
if resp.status in (400, 401, 403, 404, 408, 413, 500, 501):
|
2011-01-25 14:01:22 -06:00
|
|
|
raise exceptions.from_response(resp, body)
|
|
|
|
|
|
|
|
return resp, body
|
|
|
|
|
|
|
|
def _cs_request(self, url, method, **kwargs):
|
|
|
|
if not self.management_url:
|
|
|
|
self.authenticate()
|
|
|
|
|
|
|
|
# Perform the request once. If we get a 401 back then it
|
|
|
|
# might be because the auth token expired, so try to
|
|
|
|
# re-authenticate and try again. If it still fails, bail.
|
|
|
|
try:
|
|
|
|
kwargs.setdefault('headers', {})['X-Auth-Token'] = self.auth_token
|
2011-06-15 09:48:24 -07:00
|
|
|
if self.projectid:
|
|
|
|
kwargs['headers']['X-Auth-Project-Id'] = self.projectid
|
2011-06-09 10:39:13 +04:00
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
resp, body = self.request(self.management_url + url, method,
|
|
|
|
**kwargs)
|
|
|
|
return resp, body
|
|
|
|
except exceptions.Unauthorized, ex:
|
|
|
|
try:
|
|
|
|
self.authenticate()
|
|
|
|
resp, body = self.request(self.management_url + url, method,
|
|
|
|
**kwargs)
|
|
|
|
return resp, body
|
|
|
|
except exceptions.Unauthorized:
|
|
|
|
raise ex
|
|
|
|
|
|
|
|
def get(self, url, **kwargs):
|
|
|
|
url = self._munge_get_url(url)
|
|
|
|
return self._cs_request(url, 'GET', **kwargs)
|
|
|
|
|
|
|
|
def post(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'POST', **kwargs)
|
|
|
|
|
|
|
|
def put(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'PUT', **kwargs)
|
|
|
|
|
|
|
|
def delete(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'DELETE', **kwargs)
|
|
|
|
|
|
|
|
def authenticate(self):
|
2011-07-12 14:41:56 -07:00
|
|
|
scheme, netloc, path, query, frag = urlparse.urlsplit(
|
|
|
|
self.auth_url)
|
|
|
|
path_parts = path.split('/')
|
|
|
|
for part in path_parts:
|
|
|
|
if len(part) > 0 and part[0] == 'v':
|
|
|
|
self.version = part
|
|
|
|
break
|
|
|
|
|
2011-06-15 09:48:24 -07:00
|
|
|
headers = {'X-Auth-User': self.user,
|
|
|
|
'X-Auth-Key': self.apikey}
|
|
|
|
if self.projectid:
|
|
|
|
headers['X-Auth-Project-Id'] = self.projectid
|
2011-01-25 14:01:22 -06:00
|
|
|
resp, body = self.request(self.auth_url, 'GET', headers=headers)
|
|
|
|
self.management_url = resp['x-server-management-url']
|
2011-07-12 14:41:56 -07:00
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
self.auth_token = resp['x-auth-token']
|
|
|
|
|
|
|
|
def _munge_get_url(self, url):
|
|
|
|
"""
|
|
|
|
Munge GET URLs to always return uncached content.
|
|
|
|
|
2011-02-08 09:27:22 -04:00
|
|
|
The OpenStack Nova API caches data *very* agressively and doesn't
|
2011-01-25 14:01:22 -06:00
|
|
|
respect cache headers. To avoid stale data, then, we append a little
|
|
|
|
bit of nonsense onto GET parameters; this appears to force the data not
|
|
|
|
to be cached.
|
|
|
|
"""
|
|
|
|
scheme, netloc, path, query, frag = urlparse.urlsplit(url)
|
|
|
|
query = urlparse.parse_qsl(query)
|
|
|
|
query.append(('fresh', str(time.time())))
|
|
|
|
query = urllib.urlencode(query)
|
|
|
|
return urlparse.urlunsplit((scheme, netloc, path, query, frag))
|