Fixes LP #992096 - Ensure version in URL

* Documents the configure_via_auth parameter
* Ensures that if configure_via_auth is used (the default), that
  if the management_url that is returned does not contain a version
  identifier, the client class' DEFAULT_DOC_ROOT is appended
  appropriately.
* Adds some log debugging statements into the base client class so
  that the URL being configured/constructed is more easily identifiable

Change-Id: I307a24231b59f7a183ed669f679976cc3f8ec21c
This commit is contained in:
Jay Pipes 2012-04-30 14:38:51 -04:00
parent 621c392240
commit 60ab521b01
2 changed files with 26 additions and 5 deletions

View File

@ -23,7 +23,9 @@ import collections
import errno
import functools
import httplib
import logging
import os
import re
import select
import urllib
import urlparse
@ -43,10 +45,13 @@ except ImportError:
from glance.common import auth
from glance.common import exception, utils
LOG = logging.getLogger(__name__)
# common chunk size for get and put
CHUNKSIZE = 65536
VERSION_REGEX = re.compile(r"v[0-9\.]+")
def handle_unauthenticated(func):
"""
@ -254,6 +259,10 @@ class BaseClient(object):
GLANCE_CLIENT_CA_FILE is looked for.
:param insecure: Optional. If set then the server's certificate
will not be verified.
:param configure_via_auth: Optional. Defaults to True. If set, the
URL returned from the service catalog for the image
endpoint will **override** the URL supplied to in
the host parameter.
"""
self.host = host
self.port = port or self.DEFAULT_PORT
@ -355,11 +364,20 @@ class BaseClient(object):
<http|https>://<host>:port/doc_root
"""
LOG.debug(_("Configuring from URL: %s"), url)
parsed = urlparse.urlparse(url)
self.use_ssl = parsed.scheme == 'https'
self.host = parsed.hostname
self.port = parsed.port or 80
self.doc_root = parsed.path
self.doc_root = parsed.path.rstrip('/')
# We need to ensure a version identifier is appended to the doc_root
if not VERSION_REGEX.match(self.doc_root):
if self.DEFAULT_DOC_ROOT:
doc_root = self.DEFAULT_DOC_ROOT.lstrip('/')
self.doc_root += '/' + doc_root
msg = _("Appending doc_root %(doc_root)s to URL %(url)s")
LOG.debug(msg % locals())
# ensure connection kwargs are re-evaluated after the service catalog
# publicURL is parsed for potential SSL usage
@ -435,7 +453,10 @@ class BaseClient(object):
else:
query = None
return urlparse.ParseResult(scheme, netloc, path, '', query, '')
url = urlparse.ParseResult(scheme, netloc, path, '', query, '')
log_msg = _("Constructed URL: %s")
LOG.debug(log_msg, url.geturl())
return url
@handle_redirects
def _do_request(self, method, url, body, headers):

View File

@ -2063,7 +2063,7 @@ class TestClient(base.IsolatedUnitTest):
class TestConfigureClientFromURL(unittest.TestCase):
def setUp(self):
self.client = client.Client("0.0.0.0", doc_root="")
self.client = client.Client("0.0.0.0")
def assertConfiguration(self, url, host, port, use_ssl, doc_root):
self.client.configure_from_url(url)
@ -2078,7 +2078,7 @@ class TestConfigureClientFromURL(unittest.TestCase):
host='www.example.com',
port=80,
use_ssl=False,
doc_root=''
doc_root='/v1'
)
def test_port_ssl_doc_root(self):
@ -2087,5 +2087,5 @@ class TestConfigureClientFromURL(unittest.TestCase):
host='www.example.com',
port=8000,
use_ssl=True,
doc_root='/prefix/'
doc_root='/prefix/v1'
)