Skip to content

Commit

Permalink
Fixes LP #992096 - Ensure version in URL
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jaypipes committed Apr 30, 2012
1 parent 621c392 commit 60ab521
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
25 changes: 23 additions & 2 deletions glance/common/client.py
Expand Up @@ -23,7 +23,9 @@
import errno
import functools
import httplib
import logging
import os
import re
import select
import urllib
import urlparse
Expand All @@ -43,10 +45,13 @@
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):
"""
Expand Down Expand Up @@ -254,6 +259,10 @@ def __init__(self, host, port=None, use_ssl=False, auth_tok=None,
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
Expand Down Expand Up @@ -355,11 +364,20 @@ def configure_from_url(self, url):
<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
Expand Down Expand Up @@ -435,7 +453,10 @@ def _construct_url(self, action, params=None):
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):
Expand Down
6 changes: 3 additions & 3 deletions glance/tests/unit/test_clients.py
Expand Up @@ -2063,7 +2063,7 @@ def test_delete_member(self):

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)
Expand All @@ -2078,7 +2078,7 @@ def test_no_port_no_ssl_no_doc_root(self):
host='www.example.com',
port=80,
use_ssl=False,
doc_root=''
doc_root='/v1'
)

def test_port_ssl_doc_root(self):
Expand All @@ -2087,5 +2087,5 @@ def test_port_ssl_doc_root(self):
host='www.example.com',
port=8000,
use_ssl=True,
doc_root='/prefix/'
doc_root='/prefix/v1'
)

0 comments on commit 60ab521

Please sign in to comment.