diff options
-rw-r--r-- | glance_store/_drivers/s3.py | 9 | ||||
-rw-r--r-- | glance_store/common/auth.py | 38 | ||||
-rw-r--r-- | glance_store/exceptions.py | 15 | ||||
-rw-r--r-- | tox.ini | 3 |
4 files changed, 40 insertions, 25 deletions
diff --git a/glance_store/_drivers/s3.py b/glance_store/_drivers/s3.py index 8c3a4df..7c114c3 100644 --- a/glance_store/_drivers/s3.py +++ b/glance_store/_drivers/s3.py @@ -362,7 +362,7 @@ class Store(glance_store.driver.Store): is_secure=(loc.scheme == 's3+https'), calling_format=calling_format) - create_bucket_if_missing(self.bucket, s3_conn) + create_bucket_if_missing(self.conf, self.bucket, s3_conn) bucket_obj = get_bucket(s3_conn, self.bucket) obj_name = str(image_id) @@ -498,11 +498,12 @@ def get_s3_location(s3_host): return locations.get(key, Location.DEFAULT) -def create_bucket_if_missing(bucket, s3_conn): +def create_bucket_if_missing(conf, bucket, s3_conn): """ Creates a missing bucket in S3 if the ``s3_store_create_bucket_on_put`` option is set. + :param conf: Configuration :param bucket: Name of bucket to create :param s3_conn: Connection to S3 """ @@ -511,8 +512,8 @@ def create_bucket_if_missing(bucket, s3_conn): s3_conn.get_bucket(bucket) except S3ResponseError as e: if e.status == httplib.NOT_FOUND: - if self.conf.glance_store.s3_store_create_bucket_on_put: - host = self.conf.glance_store.s3_store_host + if conf.glance_store.s3_store_create_bucket_on_put: + host = conf.glance_store.s3_store_host location = get_s3_location(host) try: s3_conn.create_bucket(bucket, location=location) diff --git a/glance_store/common/auth.py b/glance_store/common/auth.py index 2dffd15..49281cd 100644 --- a/glance_store/common/auth.py +++ b/glance_store/common/auth.py @@ -33,6 +33,7 @@ import logging import six.moves.urllib.parse as urlparse +from glance_store import exceptions from glance_store.openstack.common import jsonutils @@ -84,14 +85,14 @@ class KeystoneStrategy(BaseStrategy): for required in ('username', 'password', 'auth_url', 'strategy'): if self.creds.get(required) is None: - raise exception.MissingCredentialError(required=required) + raise exceptions.MissingCredentialError(required=required) if self.creds['strategy'] != 'keystone': - raise exception.BadAuthStrategy(expected='keystone', - received=self.creds['strategy']) + raise exceptions.BadAuthStrategy(expected='keystone', + received=self.creds['strategy']) # For v2.0 also check tenant is present if self.creds['auth_url'].rstrip('/').endswith('v2.0'): if self.creds.get("tenant") is None: - raise exception.MissingCredentialError(required='tenant') + raise exceptions.MissingCredentialError(required='tenant') def authenticate(self): """Authenticate with the Keystone service. @@ -125,10 +126,10 @@ class KeystoneStrategy(BaseStrategy): for _ in range(self.MAX_REDIRECTS): try: _authenticate(auth_url) - except exception.AuthorizationRedirect as e: + except exceptions.AuthorizationRedirect as e: # 2. Keystone may redirect us auth_url = e.url - except exception.AuthorizationFailure: + except exceptions.AuthorizationFailure: # 3. In some configurations nova makes redirection to # v2.0 keystone endpoint. Also, new location does not # contain real endpoint, only hostname and port. @@ -141,7 +142,7 @@ class KeystoneStrategy(BaseStrategy): break else: # Guard against a redirection loop - raise exception.MaxRedirectsExceeded(redirects=self.MAX_REDIRECTS) + raise exceptions.MaxRedirectsExceeded(redirects=self.MAX_REDIRECTS) def _v1_auth(self, token_url): creds = self.creds @@ -172,15 +173,15 @@ class KeystoneStrategy(BaseStrategy): self.management_url = _management_url(self, resp) self.auth_token = resp['x-auth-token'] except KeyError: - raise exception.AuthorizationFailure() + raise exceptions.AuthorizationFailure() elif resp.status == 305: - raise exception.AuthorizationRedirect(uri=resp['location']) + raise exceptions.AuthorizationRedirect(uri=resp['location']) elif resp.status == 400: - raise exception.AuthBadRequest(url=token_url) + raise exceptions.AuthBadRequest(url=token_url) elif resp.status == 401: - raise exception.NotAuthenticated() + raise exceptions.NotAuthenticated() elif resp.status == 404: - raise exception.AuthUrlNotFound(url=token_url) + raise exceptions.AuthUrlNotFound(url=token_url) else: raise Exception(_('Unexpected response: %s') % resp.status) @@ -214,13 +215,13 @@ class KeystoneStrategy(BaseStrategy): self.management_url = endpoint self.auth_token = resp_auth['token']['id'] elif resp.status == 305: - raise exception.RedirectException(resp['location']) + raise exceptions.RedirectException(resp['location']) elif resp.status == 400: - raise exception.AuthBadRequest(url=token_url) + raise exceptions.AuthBadRequest(url=token_url) elif resp.status == 401: - raise exception.NotAuthenticated() + raise exceptions.NotAuthenticated() elif resp.status == 404: - raise exception.AuthUrlNotFound(url=token_url) + raise exceptions.AuthUrlNotFound(url=token_url) else: raise Exception(_('Unexpected response: %s') % resp.status) @@ -280,9 +281,10 @@ def get_endpoint(service_catalog, service_type='image', endpoint_region=None, if endpoint_region is None or endpoint_region == ep['region']: if endpoint is not None: # This is a second match, abort - raise exception.RegionAmbiguity(region=endpoint_region) + exc = exceptions.RegionAmbiguity + raise exc(region=endpoint_region) endpoint = ep if endpoint and endpoint.get(endpoint_type): return endpoint[endpoint_type] else: - raise exception.NoServiceEndpoint() + raise exceptions.NoServiceEndpoint() diff --git a/glance_store/exceptions.py b/glance_store/exceptions.py index 02d68df..29526d5 100644 --- a/glance_store/exceptions.py +++ b/glance_store/exceptions.py @@ -15,7 +15,11 @@ """Glance Store exception subclasses""" -from glance_store.i18n import _ +import six.moves.urllib.parse as urlparse + +from glance_store import i18n + +_ = i18n._ class BackendException(Exception): @@ -26,6 +30,11 @@ class UnsupportedBackend(BackendException): pass +class RedirectException(Exception): + def __init__(self, url): + self.url = urlparse.urlparse(url) + + class GlanceStoreException(Exception): """ Base Glance Store Exception @@ -51,6 +60,10 @@ class BadAuthStrategy(GlanceStoreException): "received \"%(received)s\"") +class AuthorizationRedirect(GlanceStoreException): + message = _("Redirecting to %(uri)s for authorization.") + + class NotFound(GlanceStoreException): message = _("Image %(image)s not found") @@ -29,11 +29,10 @@ commands = {posargs} # TODO(dmllr): Analyze or fix the warnings blacklisted below # E711 comparison to None should be 'if cond is not None:' # E712 comparison to True should be 'if cond is True:' or 'if cond:' -# F821 undefined name 'name' # F841 local variable 'name' assigned but never used # H301 one import per line # H402 one line docstring needs punctuation. # H404 multi line docstring should start with a summary -ignore = E711,E712,F821,F841,H301,H402,H404 +ignore = E711,E712,F841,H301,H402,H404 builtins = _ exclude = .venv,.git,.tox,dist,doc,etc,*glance_store/locale*,*openstack/common*,*lib/python*,*egg,build |