Skip to content

Commit

Permalink
container: improve exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fvennetier committed Mar 29, 2018
1 parent 63ceba0 commit 9374823
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions oioswift/proxy/controllers/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

from oio.common import exceptions

from oioswift.utils import get_listing_content_type, handle_service_busy
from oioswift.utils import get_listing_content_type, \
handle_oio_no_such_container, handle_oio_timeout, \
handle_service_busy


class ContainerController(SwiftContainerController):
Expand All @@ -43,6 +45,9 @@ class ContainerController(SwiftContainerController):
'x-container-sync-key', 'x-container-sync-to',
'x-versions-location']

@handle_oio_no_such_container
@handle_oio_timeout
@handle_service_busy
def GETorHEAD(self, req):
"""Handler for HTTP GET/HEAD requests."""
if self.account_info(self.account_name, req) is None:
Expand Down Expand Up @@ -86,11 +91,7 @@ def convert_policy(self, resp):

def get_metadata_resp_headers(self, meta):
headers = {}
try:
system = meta['system']
except KeyError:
# compatibility with oio-sds < 3.2
system = meta['properties']
system = meta.get('system') or {}
# sys.m2.ctime is microseconds
ctime = float(system.get('sys.m2.ctime', 0)) / 1000000.0
headers.update({
Expand Down Expand Up @@ -133,21 +134,18 @@ def get_container_list_resp(self, req):
delimiter = '/'
opts = req.environ.get('oio.query', {})
oio_headers = {'X-oio-req-id': self.trans_id}
try:
result = self.app.storage.object_list(
self.account_name, self.container_name, prefix=prefix,
limit=limit, delimiter=delimiter, marker=marker,
end_marker=end_marker, properties=True,
versions=opts.get('versions', False),
deleted=opts.get('deleted', False),
headers=oio_headers)
result = self.app.storage.object_list(
self.account_name, self.container_name, prefix=prefix,
limit=limit, delimiter=delimiter, marker=marker,
end_marker=end_marker, properties=True,
versions=opts.get('versions', False),
deleted=opts.get('deleted', False),
headers=oio_headers)

resp_headers = self.get_metadata_resp_headers(result)
resp = self.create_listing(
req, out_content_type, resp_headers, result,
self.container_name, **opts)
except exceptions.NoSuchContainer:
return HTTPNotFound(request=req)
resp_headers = self.get_metadata_resp_headers(result)
resp = self.create_listing(
req, out_content_type, resp_headers, result,
self.container_name, **opts)
return resp

def create_listing(self, req, out_content_type, resp_headers,
Expand Down Expand Up @@ -210,15 +208,13 @@ def update_data_record(self, record, versions=False):
@public
@delay_denial
@cors_validation
@handle_service_busy
def GET(self, req):
"""Handler for HTTP GET requests."""
return self.GETorHEAD(req)

@public
@delay_denial
@cors_validation
@handle_service_busy
def HEAD(self, req):
"""Handler for HTTP HEAD requests."""
return self.GETorHEAD(req)
Expand All @@ -228,15 +224,10 @@ def get_container_head_resp(self, req):
out_content_type = get_listing_content_type(req)
headers['Content-Type'] = out_content_type
oio_headers = {'X-oio-req-id': self.trans_id}
try:
meta = self.app.storage.container_get_properties(
self.account_name, self.container_name, headers=oio_headers)
headers.update(self.get_metadata_resp_headers(meta))
resp = HTTPNoContent(request=req, headers=headers, charset='utf-8')
except exceptions.NoSuchContainer:
resp = HTTPNotFound(request=req, headers=headers)

return resp
meta = self.app.storage.container_get_properties(
self.account_name, self.container_name, headers=oio_headers)
headers.update(self.get_metadata_resp_headers(meta))
return HTTPNoContent(request=req, headers=headers, charset='utf-8')

def properties_from_headers(self, headers):
metadata = {
Expand Down Expand Up @@ -282,6 +273,7 @@ def get_container_create_resp(self, req, headers):

@public
@cors_validation
@handle_oio_timeout
@handle_service_busy
def PUT(self, req):
"""HTTP PUT request handler."""
Expand Down Expand Up @@ -327,6 +319,7 @@ def PUT(self, req):

@public
@cors_validation
@handle_oio_timeout
@handle_service_busy
def POST(self, req):
"""HTTP POST request handler."""
Expand Down Expand Up @@ -365,31 +358,30 @@ def get_container_post_resp(self, req, headers):
resp = self.PUT(req)
return resp

def get_container_delete_resp(self, req, headers):
def get_container_delete_resp(self, req):
oio_headers = {'X-oio-req-id': self.trans_id}
try:
self.app.storage.container_delete(
self.account_name, self.container_name, headers=oio_headers)
except exceptions.ContainerNotEmpty:
return HTTPConflict(request=req)
except exceptions.NoSuchContainer:
return HTTPNotFound(request=req)
resp = HTTPNoContent(request=req)
return resp

@public
@cors_validation
@handle_oio_no_such_container
@handle_oio_timeout
@handle_service_busy
def DELETE(self, req):
"""HTTP DELETE request handler."""
account_partition, accounts, container_count = \
self.account_info(self.account_name, req)
if not accounts:
return HTTPNotFound(request=req)
headers = self.generate_request_headers(req, transfer=True)
clear_info_cache(self.app, req.environ,
self.account_name, self.container_name)
resp = self.get_container_delete_resp(req, headers)
resp = self.get_container_delete_resp(req)
if resp.status_int == HTTP_ACCEPTED:
return HTTPNotFound(request=req)
return resp

0 comments on commit 9374823

Please sign in to comment.