2011-10-25 16:50:08 -07:00
|
|
|
# Copyright 2010 Jacob Kaplan-Moss
|
|
|
|
# Copyright 2011 OpenStack LLC.
|
|
|
|
# Copyright 2011 Piston Cloud Computing, Inc.
|
|
|
|
# Copyright 2011 Nebula, Inc.
|
|
|
|
|
|
|
|
# All Rights Reserved.
|
|
|
|
"""
|
|
|
|
OpenStack Client interface. Handles the REST calls and responses.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import copy
|
|
|
|
import logging
|
|
|
|
import urlparse
|
|
|
|
|
|
|
|
import httplib2
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2012-10-13 00:15:39 +00:00
|
|
|
from keystoneclient import access
|
2011-10-25 16:50:08 -07:00
|
|
|
from keystoneclient import exceptions
|
|
|
|
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPClient(httplib2.Http):
|
|
|
|
|
|
|
|
USER_AGENT = 'python-keystoneclient'
|
|
|
|
|
2011-12-17 22:36:59 -08:00
|
|
|
def __init__(self, username=None, tenant_id=None, tenant_name=None,
|
2011-12-19 10:00:39 -08:00
|
|
|
password=None, auth_url=None, region_name=None, timeout=None,
|
2012-05-23 18:16:50 +00:00
|
|
|
endpoint=None, token=None, cacert=None, key=None,
|
2012-10-13 00:15:39 +00:00
|
|
|
cert=None, insecure=False, original_ip=None, debug=False,
|
|
|
|
auth_ref=None):
|
2012-05-23 18:16:50 +00:00
|
|
|
super(HTTPClient, self).__init__(timeout=timeout, ca_certs=cacert)
|
|
|
|
if cert:
|
|
|
|
if key:
|
|
|
|
self.add_certificate(key=key, cert=cert, domain='')
|
|
|
|
else:
|
|
|
|
self.add_certificate(key=cert, cert=cert, domain='')
|
2012-10-13 00:15:39 +00:00
|
|
|
self.version = 'v2.0'
|
|
|
|
self.auth_ref = access.AccessInfo(**auth_ref) if auth_ref else None
|
|
|
|
if self.auth_ref:
|
|
|
|
self.username = self.auth_ref.username
|
|
|
|
self.tenant_id = self.auth_ref.tenant_id
|
|
|
|
self.tenant_name = self.auth_ref.tenant_name
|
|
|
|
self.auth_url = self.auth_ref.auth_url
|
|
|
|
self.management_url = self.auth_ref.management_url
|
|
|
|
self.auth_token = self.auth_ref.auth_token
|
|
|
|
#NOTE(heckj): allow override of the auth_ref defaults from explicit
|
|
|
|
# values provided to the client
|
2011-12-17 22:36:59 -08:00
|
|
|
self.username = username
|
2011-12-17 22:07:13 -08:00
|
|
|
self.tenant_id = tenant_id
|
|
|
|
self.tenant_name = tenant_name
|
2011-10-25 16:50:08 -07:00
|
|
|
self.password = password
|
2012-01-30 14:13:57 -06:00
|
|
|
self.auth_url = auth_url.rstrip('/') if auth_url else None
|
2011-12-17 22:07:13 -08:00
|
|
|
self.auth_token = token
|
2012-09-13 15:45:40 +02:00
|
|
|
self.original_ip = original_ip
|
2011-10-25 16:50:08 -07:00
|
|
|
|
2012-10-13 00:15:39 +00:00
|
|
|
self.management_url = endpoint.rstrip('/') if endpoint else None
|
|
|
|
self.region_name = region_name
|
2011-10-25 16:50:08 -07:00
|
|
|
|
|
|
|
# httplib2 overrides
|
|
|
|
self.force_exception_to_status_code = True
|
2012-07-09 17:07:41 +02:00
|
|
|
self.disable_ssl_certificate_validation = insecure
|
2011-10-25 16:50:08 -07:00
|
|
|
|
2012-08-16 18:18:22 -07:00
|
|
|
# logging setup
|
2012-10-13 00:15:39 +00:00
|
|
|
self.debug_log = debug
|
2012-08-16 18:18:22 -07:00
|
|
|
if self.debug_log:
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
_logger.setLevel(logging.DEBUG)
|
|
|
|
_logger.addHandler(ch)
|
|
|
|
|
2011-10-25 16:50:08 -07:00
|
|
|
def authenticate(self):
|
2012-10-24 07:12:30 -05:00
|
|
|
""" Authenticate against the Identity API.
|
2011-10-25 16:50:08 -07:00
|
|
|
|
|
|
|
Not implemented here because auth protocols should be API
|
|
|
|
version-specific.
|
2012-10-13 00:15:39 +00:00
|
|
|
|
|
|
|
Expected to authenticate or validate an existing authentication
|
|
|
|
reference already associated with the client. Invoking this call
|
|
|
|
*always* makes a call to the Keystone.
|
2011-10-25 16:50:08 -07:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def _extract_service_catalog(self, url, body):
|
|
|
|
""" Set the client's service catalog from the response data.
|
|
|
|
|
|
|
|
Not implemented here because data returned may be API
|
|
|
|
version-specific.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2012-08-16 18:18:22 -07:00
|
|
|
def http_log_req(self, args, kwargs):
|
|
|
|
if not self.debug_log:
|
2011-10-25 16:50:08 -07:00
|
|
|
return
|
|
|
|
|
|
|
|
string_parts = ['curl -i']
|
|
|
|
for element in args:
|
|
|
|
if element in ('GET', 'POST'):
|
|
|
|
string_parts.append(' -X %s' % element)
|
|
|
|
else:
|
|
|
|
string_parts.append(' %s' % element)
|
|
|
|
|
|
|
|
for element in kwargs['headers']:
|
|
|
|
header = ' -H "%s: %s"' % (element, kwargs['headers'][element])
|
|
|
|
string_parts.append(header)
|
|
|
|
|
|
|
|
_logger.debug("REQ: %s\n" % "".join(string_parts))
|
|
|
|
if 'body' in kwargs:
|
|
|
|
_logger.debug("REQ BODY: %s\n" % (kwargs['body']))
|
2012-08-16 18:18:22 -07:00
|
|
|
|
|
|
|
def http_log_resp(self, resp, body):
|
|
|
|
if self.debug_log:
|
|
|
|
_logger.debug("RESP: %s\nRESP BODY: %s\n", resp, body)
|
2011-10-25 16:50:08 -07:00
|
|
|
|
2012-09-11 11:10:40 -05:00
|
|
|
def serialize(self, entity):
|
|
|
|
return json.dumps(entity)
|
|
|
|
|
2011-10-25 16:50:08 -07:00
|
|
|
def request(self, url, method, **kwargs):
|
|
|
|
""" Send an http request with the specified characteristics.
|
|
|
|
|
|
|
|
Wrapper around httplib2.Http.request to handle tasks such as
|
|
|
|
setting headers, JSON encoding/decoding, and error handling.
|
|
|
|
"""
|
|
|
|
# Copy the kwargs so we can reuse the original in case of redirects
|
|
|
|
request_kwargs = copy.copy(kwargs)
|
|
|
|
request_kwargs.setdefault('headers', kwargs.get('headers', {}))
|
|
|
|
request_kwargs['headers']['User-Agent'] = self.USER_AGENT
|
2012-09-13 15:45:40 +02:00
|
|
|
if self.original_ip:
|
|
|
|
request_kwargs['headers']['Forwarded'] = "for=%s;by=%s" % (
|
|
|
|
self.original_ip, self.USER_AGENT)
|
2011-10-25 16:50:08 -07:00
|
|
|
if 'body' in kwargs:
|
|
|
|
request_kwargs['headers']['Content-Type'] = 'application/json'
|
2012-09-11 11:10:40 -05:00
|
|
|
request_kwargs['body'] = self.serialize(kwargs['body'])
|
2011-10-25 16:50:08 -07:00
|
|
|
|
2012-08-16 18:18:22 -07:00
|
|
|
self.http_log_req((url, method,), request_kwargs)
|
2011-11-10 17:23:48 -08:00
|
|
|
resp, body = super(HTTPClient, self).request(url,
|
|
|
|
method,
|
|
|
|
**request_kwargs)
|
2012-08-16 18:18:22 -07:00
|
|
|
self.http_log_resp(resp, body)
|
2011-10-25 16:50:08 -07:00
|
|
|
|
2012-10-17 14:52:48 -04:00
|
|
|
if resp.status in (400, 401, 403, 404, 408, 409, 413, 500, 501):
|
2012-10-13 00:15:39 +00:00
|
|
|
_logger.debug("Request returned failure status: %s", resp.status)
|
2012-10-17 14:52:48 -04:00
|
|
|
raise exceptions.from_response(resp, body)
|
|
|
|
elif resp.status in (301, 302, 305):
|
|
|
|
# Redirected. Reissue the request to the new location.
|
|
|
|
return self.request(resp['location'], method, **kwargs)
|
|
|
|
|
2011-10-25 16:50:08 -07:00
|
|
|
if body:
|
|
|
|
try:
|
|
|
|
body = json.loads(body)
|
2012-04-05 17:25:37 -05:00
|
|
|
except ValueError:
|
2011-10-25 16:50:08 -07:00
|
|
|
_logger.debug("Could not decode JSON from body: %s" % body)
|
|
|
|
else:
|
|
|
|
_logger.debug("No body was returned.")
|
|
|
|
body = None
|
|
|
|
|
|
|
|
return resp, body
|
|
|
|
|
|
|
|
def _cs_request(self, url, method, **kwargs):
|
2012-10-13 00:15:39 +00:00
|
|
|
""" Makes an authenticated request to keystone endpoint by
|
|
|
|
concatenating self.management_url and url and passing in method and
|
|
|
|
any associated kwargs. """
|
2011-10-25 16:50:08 -07:00
|
|
|
|
2012-10-13 00:15:39 +00:00
|
|
|
if self.management_url is None:
|
|
|
|
raise exceptions.AuthorizationFailure(
|
|
|
|
'Current authorization does not have a known management url')
|
2011-10-25 16:50:08 -07:00
|
|
|
kwargs.setdefault('headers', {})
|
2012-02-09 01:44:11 +00:00
|
|
|
if self.auth_token:
|
2011-10-25 16:50:08 -07:00
|
|
|
kwargs['headers']['X-Auth-Token'] = self.auth_token
|
|
|
|
|
2012-10-13 00:15:39 +00:00
|
|
|
resp, body = self.request(self.management_url + url, method,
|
|
|
|
**kwargs)
|
|
|
|
return resp, body
|
2011-10-25 16:50:08 -07:00
|
|
|
|
|
|
|
def get(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'GET', **kwargs)
|
|
|
|
|
2012-09-11 11:06:54 -05:00
|
|
|
def head(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'HEAD', **kwargs)
|
|
|
|
|
2011-10-25 16:50:08 -07:00
|
|
|
def post(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'POST', **kwargs)
|
|
|
|
|
|
|
|
def put(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'PUT', **kwargs)
|
|
|
|
|
2012-09-11 11:06:54 -05:00
|
|
|
def patch(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'PATCH', **kwargs)
|
|
|
|
|
2011-10-25 16:50:08 -07:00
|
|
|
def delete(self, url, **kwargs):
|
|
|
|
return self._cs_request(url, 'DELETE', **kwargs)
|