Port of 97882f796c0e8969c606ae723d14b6b443e2e2f9

This patch ports 'Unifies how BadStoreUri gets raised and logged' to
glance.store

Change-Id: Ifa638ebdc9c53ea57bd12d33c9df9a78202a681f
This commit is contained in:
Flavio Percoco 2014-08-26 15:23:56 +02:00
parent a2f204670f
commit c2335ed752
7 changed files with 47 additions and 45 deletions

View File

@ -113,17 +113,17 @@ class StoreLocation(glance.store.location.StoreLocation):
def parse_uri(self, uri):
if not uri.startswith('cinder://'):
reason = _("URI must start with cinder://")
LOG.error(reason)
raise exceptions.BadStoreUri(uri, reason)
reason = _("URI must start with 'cinder://'")
LOG.info(reason)
raise exceptions.BadStoreUri(message=reason)
self.scheme = 'cinder'
self.volume_id = uri[9:]
if not utils.is_uuid_like(self.volume_id):
reason = _("URI contains invalid volume ID: %s") % self.volume_id
LOG.error(reason)
raise exceptions.BadStoreUri(uri, reason)
reason = _("URI contains invalid volume ID")
LOG.info(reason)
raise exceptions.BadStoreUri(message=reason)
class Store(glance.store.driver.Store):

View File

@ -75,9 +75,9 @@ class StoreLocation(glance.store.location.StoreLocation):
self.scheme = pieces.scheme
path = (pieces.netloc + pieces.path).strip()
if path == '':
reason = _("No path specified in URI: %s") % uri
LOG.debug(reason)
raise exceptions.BadStoreUri('No path specified')
reason = _("No path specified in URI")
LOG.info(reason)
raise exceptions.BadStoreUri(message=reason)
self.path = path

View File

@ -79,14 +79,13 @@ class StoreLocation(glance.store.location.StoreLocation):
try:
self.user, self.password = creds.split(':')
except ValueError:
reason = (_("Credentials '%s' not well-formatted.")
% "".join(creds))
LOG.debug(reason)
raise exceptions.BadStoreUri()
reason = _("Credentials are not well-formatted.")
LOG.info(reason)
raise exceptions.BadStoreUri(message=reason)
else:
self.user = None
if netloc == '':
LOG.debug(_("No address specified in HTTP URL"))
LOG.info(_("No address specified in HTTP URL"))
raise exceptions.BadStoreUri(uri=uri)
self.netloc = netloc
self.path = path

View File

@ -28,6 +28,7 @@ from oslo.config import cfg
from glance.store.common import utils
from glance.store import driver
from glance.store import exceptions
from glance.store import i18n
from glance.store.i18n import _
from glance.store import location
@ -45,6 +46,7 @@ DEFAULT_CHUNKSIZE = 8 # in MiB
DEFAULT_SNAPNAME = 'snap'
LOG = logging.getLogger(__name__)
_LI = i18n._LI
_RBD_OPTS = [
cfg.IntOpt('rbd_store_chunk_size', default=DEFAULT_CHUNKSIZE,
@ -102,18 +104,18 @@ class StoreLocation(location.StoreLocation):
prefix = 'rbd://'
if not uri.startswith(prefix):
reason = _('URI must start with rbd://')
msg = (_("Invalid URI: %(uri)s: %(reason)s") % {'uri': uri,
'reason': reason})
LOG.debug(msg)
msg = _LI("Invalid URI: %s") % reason
LOG.info(msg)
raise exceptions.BadStoreUri(message=reason)
# convert to ascii since librbd doesn't handle unicode
try:
ascii_uri = str(uri)
except UnicodeError:
reason = _('URI contains non-ascii characters')
msg = (_("Invalid URI: %(uri)s: %(reason)s") % {'uri': uri,
'reason': reason})
LOG.debug(msg)
msg = _LI("Invalid URI: %s") % reason
LOG.info(msg)
raise exceptions.BadStoreUri(message=reason)
pieces = ascii_uri[len(prefix):].split('/')
if len(pieces) == 1:
@ -124,15 +126,15 @@ class StoreLocation(location.StoreLocation):
map(urllib.unquote, pieces)
else:
reason = _('URI must have exactly 1 or 4 components')
msg = (_("Invalid URI: %(uri)s: %(reason)s") % {'uri': uri,
'reason': reason})
LOG.debug(msg)
msg = _LI("Invalid URI: %s") % reason
LOG.info(msg)
raise exceptions.BadStoreUri(message=reason)
if any(map(lambda p: p == '', pieces)):
reason = _('URI cannot contain empty components')
msg = (_("Invalid URI: %(uri)s: %(reason)s") % {'uri': uri,
'reason': reason})
LOG.debug(msg)
msg = _LI("Invalid URI: %s") % reason
LOG.info(msg)
raise exceptions.BadStoreUri(message=reason)

View File

@ -120,7 +120,7 @@ class StoreLocation(glance.store.location.StoreLocation):
"s3+https:// scheme, like so: "
"s3+https://accesskey:secretkey@"
"s3.amazonaws.com/bucket/key-id")
LOG.debug(_("Invalid store uri: %s") % reason)
LOG.info(_("Invalid store uri: %s") % reason)
raise exceptions.BadStoreUri(message=reason)
pieces = urlparse.urlparse(uri)
@ -147,8 +147,8 @@ class StoreLocation(glance.store.location.StoreLocation):
self.secretkey = secret_key
except IndexError:
reason = _("Badly formed S3 credentials %s") % creds
LOG.debug(reason)
raise exceptions.BadStoreUri()
LOG.info(reason)
raise exceptions.BadStoreUri(message=reason)
else:
self.accesskey = None
path = entire_path
@ -160,11 +160,11 @@ class StoreLocation(glance.store.location.StoreLocation):
self.s3serviceurl = '/'.join(path_parts).strip('/')
else:
reason = _("Badly formed S3 URI. Missing s3 service URL.")
raise exceptions.BadStoreUri()
raise exceptions.BadStoreUri(message=reason)
except IndexError:
reason = _("Badly formed S3 URI: %s") % uri
LOG.debug(reason)
raise exceptions.BadStoreUri()
LOG.info(reason)
raise exceptions.BadStoreUri(message=reason)
class ChunkedFile(object):

View File

@ -149,9 +149,10 @@ class StoreLocation(glance.store.location.StoreLocation):
return "sheepdog://%s" % self.image
def parse_uri(self, uri):
if not uri.startswith('sheepdog://'):
raise exceptions.BadStoreUri(uri, "URI must start with %s://" %
'sheepdog')
valid_schema = 'sheepdog://'
if not uri.startswith(valid_schema):
reason = _("URI must start with '%s://'") % valid_schema
raise exceptions.BadStoreUri(message=reason)
self.image = uri[11:]

View File

@ -187,8 +187,8 @@ class StoreLocation(location.StoreLocation):
if not uri.startswith('%s://' % STORE_SCHEME):
reason = (_("URI %(uri)s must start with %(scheme)s://") %
{'uri': uri, 'scheme': STORE_SCHEME})
LOG.error(reason)
raise exceptions.BadStoreUri(reason)
LOG.info(reason)
raise exceptions.BadStoreUri(message=reason)
(self.scheme, self.server_host,
path, params, query, fragment) = urlparse.urlparse(uri)
if not query:
@ -398,9 +398,9 @@ class Store(glance.store.Store):
{'image': location.image_id})
if resp.status >= 400:
if resp.status == httplib.NOT_FOUND:
msg = 'VMware datastore could not find image at URI.'
LOG.debug(msg)
raise exceptions.NotFound(message=msg)
reason = _('VMware datastore could not find image at URI.')
LOG.info(reason)
raise exceptions.NotFound(message=reason)
msg = ('HTTP request returned a %(status)s status code.'
% {'status': resp.status})
LOG.debug(msg)
@ -408,11 +408,11 @@ class Store(glance.store.Store):
location_header = resp.getheader('location')
if location_header:
if resp.status not in (301, 302):
msg = ("The HTTP URL %(path)s attempted to redirect "
"with an invalid %(status)s status code."
% {'path': loc.path, 'status': resp.status})
LOG.debug(msg)
raise exceptions.BadStoreUri(msg)
reason = (_("The HTTP URL %(path)s attempted to redirect "
"with an invalid %(status)s status code.")
% {'path': loc.path, 'status': resp.status})
LOG.info(reason)
raise exceptions.BadStoreUri(message=reason)
location_class = glance.store.location.Location
new_loc = location_class(location.store_name,
location.store_location.__class__,