Handle endpoints with versions consistently

When using the cli the Glance client wraps the endpoint in a 'strip
version' function. This means that endpoints of the following forms can
both be used:

https://region-x.images.example.com:443/v1
https://region-x.images.example.com:443

When calling the client library directly (as Ceilometer does) however
only endpoints of the second form work. The cli and library should handle
the two cases consistently.

Addresses bug 1243276.

Change-Id: Ice7b581fee32540a7057ba47433a10166a3caed2
This commit is contained in:
Stuart McLaren
2013-10-22 15:51:49 +00:00
parent f6c3d4e141
commit ee7fd2f5bd
6 changed files with 102 additions and 22 deletions
+13
View File
@@ -17,6 +17,7 @@ from __future__ import print_function
import errno
import os
import re
import sys
import uuid
@@ -315,3 +316,15 @@ def get_data_file(args):
else:
# (3) no image data provided
return None
def strip_version(endpoint):
"""Strip version from the last component of endpoint if present."""
# Get rid of trailing '/' if present
if endpoint.endswith('/'):
endpoint = endpoint[:-1]
url_bits = endpoint.split('/')
# regex to match 'v1' or 'v2.0' etc
if re.match('v\d+\.?\d*', url_bits[-1]):
endpoint = '/'.join(url_bits[:-1])
return endpoint
+1 -18
View File
@@ -24,7 +24,6 @@ import json
import logging
import os
from os.path import expanduser
import re
import sys
from keystoneclient.v2_0 import client as ksclient
@@ -306,21 +305,6 @@ class OpenStackImagesShell(object):
subparser.add_argument(*args, **kwargs)
subparser.set_defaults(func=callback)
# TODO(dtroyer): move this into the common client support?
# Compatibility check to remove API version as the trailing component
# in a service endpoint; also removes a trailing '/'
def _strip_version(self, endpoint):
"""Strip version from the last component of endpoint if present."""
# Get rid of trailing '/' if present
if endpoint.endswith('/'):
endpoint = endpoint[:-1]
url_bits = endpoint.split('/')
# regex to match 'v1' or 'v2.0' etc
if re.match('v\d+\.?\d*', url_bits[-1]):
endpoint = '/'.join(url_bits[:-1])
return endpoint
def _get_ksclient(self, **kwargs):
"""Get an endpoint and auth token from Keystone.
@@ -349,8 +333,7 @@ class OpenStackImagesShell(object):
endpoint_kwargs['attr'] = 'region'
endpoint_kwargs['filter_value'] = kwargs.get('region_name')
endpoint = client.service_catalog.url_for(**endpoint_kwargs)
return self._strip_version(endpoint)
return client.service_catalog.url_for(**endpoint_kwargs)
def _get_image_url(self, args):
"""Translate the available url-related options into a single string.
+4 -2
View File
@@ -14,6 +14,7 @@
# under the License.
from glanceclient.common import http
from glanceclient.common import utils
from glanceclient.v1 import image_members
from glanceclient.v1 import images
@@ -28,8 +29,9 @@ class Client(object):
http requests. (optional)
"""
def __init__(self, *args, **kwargs):
def __init__(self, endpoint, *args, **kwargs):
"""Initialize a new client for the Images v1 API."""
self.http_client = http.HTTPClient(*args, **kwargs)
self.http_client = http.HTTPClient(utils.strip_version(endpoint),
*args, **kwargs)
self.images = images.ImageManager(self.http_client)
self.image_members = image_members.ImageMemberManager(self.http_client)
+4 -2
View File
@@ -16,6 +16,7 @@
import warlock
from glanceclient.common import http
from glanceclient.common import utils
from glanceclient.v2 import image_members
from glanceclient.v2 import image_tags
from glanceclient.v2 import images
@@ -32,8 +33,9 @@ class Client(object):
http requests. (optional)
"""
def __init__(self, *args, **kwargs):
self.http_client = http.HTTPClient(*args, **kwargs)
def __init__(self, endpoint, *args, **kwargs):
self.http_client = http.HTTPClient(utils.strip_version(endpoint),
*args, **kwargs)
self.schemas = schemas.Controller(self.http_client)
image_model = self._get_image_model()
self.images = images.Controller(self.http_client,