Skip to content

Instantly share code, notes, and snippets.

@alistairncoles
Created August 8, 2014 14:20
Show Gist options
  • Save alistairncoles/4e5d3d91081d5de41ca1 to your computer and use it in GitHub Desktop.
Save alistairncoles/4e5d3d91081d5de41ca1 to your computer and use it in GitHub Desktop.
refactor x-delete-* checks in obj controller
diff --git a/swift/proxy/controllers/obj.py b/swift/proxy/controllers/obj.py
index a7f970a..926768e 100644
--- a/swift/proxy/controllers/obj.py
+++ b/swift/proxy/controllers/obj.py
@@ -271,12 +271,14 @@ class ObjectController(Controller):
return HTTPNotFound(request=req)
try:
- req, delete_at_container, delete_at_part, \
- delete_at_nodes = self._config_obj_expiration(req)
+ x_delete_at = self._check_obj_expiration(req)
except ValueError as e:
return HTTPBadRequest(request=req, content_type='text/plain',
body=str(e))
+ req, delete_at_container, delete_at_part, \
+ delete_at_nodes = self._config_obj_expiration(req, x_delete_at)
+
# pass the policy index to storage nodes via req header
policy_index = req.headers.get('X-Backend-Storage-Policy-Index',
container_info['storage_policy'])
@@ -422,11 +424,7 @@ class ObjectController(Controller):
bodies.append('')
return statuses, reasons, bodies, etags
- def _config_obj_expiration(self, req):
- delete_at_container = None
- delete_at_part = None
- delete_at_nodes = None
-
+ def _check_obj_expiration(self, req):
if 'x-delete-after' in req.headers:
try:
x_delete_after = int(req.headers['x-delete-after'])
@@ -446,6 +444,13 @@ class ObjectController(Controller):
if x_delete_at < time.time():
raise ValueError('X-Delete-At in past')
+ return x_delete_at
+
+ def _config_obj_expiration(self, req, x_delete_at):
+ delete_at_container = None
+ delete_at_part = None
+ delete_at_nodes = None
+ if x_delete_at:
req.environ.setdefault('swift.log_info', []).append(
'x-delete-at:%s' % x_delete_at)
delete_at_container = normalize_delete_at_timestamp(
@@ -498,6 +503,11 @@ class ObjectController(Controller):
body=str(e))
if ml is not None and ml > constraints.MAX_FILE_SIZE:
return HTTPRequestEntityTooLarge(request=req)
+ try:
+ x_delete_at = self._check_obj_expiration(req)
+ except ValueError as e:
+ return HTTPBadRequest(request=req, content_type='text/plain',
+ body=str(e))
partition, nodes = obj_ring.get_nodes(
self.account_name, self.container_name, self.object_name)
@@ -650,12 +660,14 @@ class ObjectController(Controller):
req = sink_req
- try:
- req, delete_at_container, delete_at_part, \
- delete_at_nodes = self._config_obj_expiration(req)
- except ValueError as e:
- return HTTPBadRequest(request=req, content_type='text/plain',
- body=str(e))
+ try:
+ x_delete_at = self._check_obj_expiration(req)
+ except ValueError as e:
+ return HTTPBadRequest(request=req, content_type='text/plain',
+ body=str(e))
+
+ req, delete_at_container, delete_at_part, \
+ delete_at_nodes = self._config_obj_expiration(req, x_delete_at)
node_iter = GreenthreadSafeIterator(
self.iter_nodes_local_first(obj_ring, partition))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment