Skip to content

Commit ffea029

Browse files
committed
Added a simulate flag that when set to True will not actually send the HTTP request to Atmos.
1 parent a4d3750 commit ffea029

File tree

1 file changed

+118
-99
lines changed

1 file changed

+118
-99
lines changed

EsuRestApi.py

+118-99
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#from eventlet.green import urllib2
66
#import eventlet
77

8-
DEBUG = False
8+
DEBUG = True
9+
SIMULATE = True
910

1011
class EsuRestApi(object):
1112

@@ -84,9 +85,10 @@ def create_object(self, data, listable_meta = None, non_listable_meta = None, mi
8485
error_message = e.read()
8586
return error_message
8687

87-
else: # If there was no HTTPError, parse the location header in the response body to get the object_id
88-
object_id = self.__parse_location(response)
89-
return object_id
88+
else: # If there was no HTTPError, parse the location header in the response body to get the object_id
89+
if not SIMULATE:
90+
object_id = self.__parse_location(response)
91+
return object_id
9092

9193
def create_object_on_path(self, path, listable_meta = None, non_listable_meta = None, mime_type = None, data = None):
9294
""" Creates an object in the namespace interface and returns an object_id.
@@ -151,8 +153,10 @@ def create_object_on_path(self, path, listable_meta = None, non_listable_meta =
151153
return error_message
152154

153155
else: # If there was no HTTPError, parse the location header in the response body to get the object_id
154-
object_id = self.__parse_location(response)
155-
return object_id
156+
157+
if not SIMULATE:
158+
object_id = self.__parse_location(response)
159+
return object_id
156160

157161
def list_objects(self, metadata_key, include_meta = False, filter_user_tags = None):
158162
""" Takes a listable metadata key and returns a list of objects that match.
@@ -207,8 +211,9 @@ def list_objects(self, metadata_key, include_meta = False, filter_user_tags = No
207211
return error_message
208212

209213
else:
210-
object_list = response.read()
211-
return object_list
214+
if not SIMULATE:
215+
object_list = response.read()
216+
return object_list
212217

213218
def list_directory(self, path, limit = None, include_meta = False, token = None, filter_user_tags = None):
214219
""" Lists objects in the namespace based on path
@@ -266,16 +271,17 @@ def list_directory(self, path, limit = None, include_meta = False, token = None,
266271

267272
else:
268273

269-
dir_list = response.read()
270-
271-
272-
if response.info().getheader('x-emc-token'):
273-
token = response.info().getheader('x-emc-token')
274+
if not SIMULATE:
275+
dir_list = response.read()
276+
274277

275-
return dir_list, token
276-
277-
else:
278-
return dir_list
278+
if response.info().getheader('x-emc-token'):
279+
token = response.info().getheader('x-emc-token')
280+
281+
return dir_list, token
282+
283+
else:
284+
return dir_list
279285

280286
def delete_object(self, object_id):
281287
""" Deletes objects based on object_id. """
@@ -304,8 +310,9 @@ def delete_object(self, object_id):
304310
error_message = e.read()
305311
return error_message
306312

307-
else: # If there was no HTTPError, parse the location header in the response body to get the object_id
308-
return response
313+
else:
314+
if not SIMULATE:
315+
return response
309316

310317

311318
def delete_directory(self, path):
@@ -339,7 +346,8 @@ def delete_directory(self, path):
339346
return error_message
340347

341348
else: # If there was no HTTPError, parse the location header in the response body to get the object_id
342-
return response
349+
if not SIMULATE:
350+
return response
343351

344352

345353
def read_object(self, object_id, extent = None, head = False):
@@ -390,33 +398,33 @@ def read_object(self, object_id, extent = None, head = False):
390398
return e
391399

392400
else:
393-
394-
if head:
395-
group_acl = {}
396-
user_acl = {}
397-
system_meta = {}
398-
policy = {}
399-
400-
if response.info().getheader('x-emc-groupacl'):
401-
group_acl = response.info().getheader('x-emc-groupacl')
402-
group_acl = dict(u.split("=") for u in group_acl.split(",")) # Create a Python dictionary of the data in the header and return it.
403-
404-
if response.info().getheader('x-emc-user-acl'):
405-
user_acl = response.info().getheader('x-emc-user-acl')
406-
user_acl = dict(u.split("=") for u in user_acl.split(","))
407-
408-
if response.info().getheader('x-emc-meta'):
409-
system_meta = response.info().getheader('x-emc-meta')
410-
system_meta = dict(u.split("=") for u in system_meta.split(","))
411-
412-
if response.info().getheader('x-emc-policy'):
413-
policy = response.info().getheader('x-emc-policy')
414-
415-
return {"group_acl" : group_acl , "user_acl" : user_acl, "system_meta" : system_meta, "policy" : policy}
416-
417-
else:
418-
body = response.read()
419-
return body
401+
if not SIMULATE:
402+
if head:
403+
group_acl = {}
404+
user_acl = {}
405+
system_meta = {}
406+
policy = {}
407+
408+
if response.info().getheader('x-emc-groupacl'):
409+
group_acl = response.info().getheader('x-emc-groupacl')
410+
group_acl = dict(u.split("=") for u in group_acl.split(",")) # Create a Python dictionary of the data in the header and return it.
411+
412+
if response.info().getheader('x-emc-user-acl'):
413+
user_acl = response.info().getheader('x-emc-user-acl')
414+
user_acl = dict(u.split("=") for u in user_acl.split(","))
415+
416+
if response.info().getheader('x-emc-meta'):
417+
system_meta = response.info().getheader('x-emc-meta')
418+
system_meta = dict(u.split("=") for u in system_meta.split(","))
419+
420+
if response.info().getheader('x-emc-policy'):
421+
policy = response.info().getheader('x-emc-policy')
422+
423+
return {"group_acl" : group_acl , "user_acl" : user_acl, "system_meta" : system_meta, "policy" : policy}
424+
425+
else:
426+
body = response.read()
427+
return body
420428

421429
def read_object_from_path(self, path, extent = None, head = False):
422430
""" Returns an entire object or a partial object based on a byte range from the namespace interface.
@@ -469,32 +477,33 @@ def read_object_from_path(self, path, extent = None, head = False):
469477

470478
else:
471479

472-
if head:
473-
group_acl = {}
474-
user_acl = {}
475-
system_meta = {}
476-
policy = {}
477-
478-
if response.info().getheader('x-emc-groupacl'):
479-
group_acl = response.info().getheader('x-emc-groupacl')
480-
group_acl = dict(u.split("=") for u in group_acl.split(",")) # Create a Python dictionary of the data in the header and return it.
481-
482-
if response.info().getheader('x-emc-user-acl'):
483-
user_acl = response.info().getheader('x-emc-user-acl')
484-
user_acl = dict(u.split("=") for u in user_acl.split(","))
485-
486-
if response.info().getheader('x-emc-meta'):
487-
system_meta = response.info().getheader('x-emc-meta')
488-
system_meta = dict(u.split("=") for u in system_meta.split(","))
489-
490-
if response.info().getheader('x-emc-policy'):
491-
policy = response.info().getheader('x-emc-policy')
492-
493-
return {"group_acl" : group_acl , "user_acl" : user_acl, "system_meta" : system_meta, "policy" : policy}
494-
495-
else:
496-
body = response.read()
497-
return body
480+
if not SIMULATE:
481+
if head:
482+
group_acl = {}
483+
user_acl = {}
484+
system_meta = {}
485+
policy = {}
486+
487+
if response.info().getheader('x-emc-groupacl'):
488+
group_acl = response.info().getheader('x-emc-groupacl')
489+
group_acl = dict(u.split("=") for u in group_acl.split(",")) # Create a Python dictionary of the data in the header and return it.
490+
491+
if response.info().getheader('x-emc-user-acl'):
492+
user_acl = response.info().getheader('x-emc-user-acl')
493+
user_acl = dict(u.split("=") for u in user_acl.split(","))
494+
495+
if response.info().getheader('x-emc-meta'):
496+
system_meta = response.info().getheader('x-emc-meta')
497+
system_meta = dict(u.split("=") for u in system_meta.split(","))
498+
499+
if response.info().getheader('x-emc-policy'):
500+
policy = response.info().getheader('x-emc-policy')
501+
502+
return {"group_acl" : group_acl , "user_acl" : user_acl, "system_meta" : system_meta, "policy" : policy}
503+
504+
else:
505+
body = response.read()
506+
return body
498507

499508
def update_object(self, object_id, data, extent = None, listable_meta = None, non_listable_meta = None, mime_type = None):
500509
""" Updates an existing object with listable metadata, non-listable metadata, and/or bytes of actual object data based on range.
@@ -645,8 +654,10 @@ def create_directory(self, path):
645654
return error_message
646655

647656
else: # If there was no HTTPError, parse the location header in the response body to get the object_id
648-
object_id = self.__parse_location(response)
649-
return object_id
657+
658+
if not SIMULATE:
659+
object_id = self.__parse_location(response)
660+
return object_id
650661

651662
# Renames won't work before Atmos 1.3.x
652663
def rename_object(self, source, destination, force):
@@ -685,7 +696,8 @@ def rename_object(self, source, destination, force):
685696
return error_message
686697

687698
else:
688-
return response
699+
if not SIMULATE:
700+
return response
689701

690702
def set_user_metadata(self, object_id, listable_meta = None, non_listable_meta = None):
691703
""" Updates an existing object with listable and/or non-listable user metadata
@@ -770,7 +782,8 @@ def delete_user_metadata(self, object_id, metadata_key):
770782
return error_message
771783

772784
else:
773-
return response
785+
if not SIMULATE:
786+
return response
774787

775788

776789
def get_user_metadata(self, object_id):
@@ -807,18 +820,19 @@ def get_user_metadata(self, object_id):
807820
return error_message
808821

809822
else:
810-
nl_user_meta = {}
811-
listable_user_meta = {}
812-
813-
if response.info().getheader('x-emc-meta'):
814-
nl_user_meta = response.info().getheader('x-emc-meta')
815-
nl_user_meta = dict(u.split("=") for u in nl_user_meta.split(",")) # Create a Python dictionary of the data in the header and return it.
816-
817-
if response.info().getheader('x-emc-listable-meta'):
818-
listable_user_meta = response.info().getheader('x-emc-listable-meta')
819-
listable_user_meta = dict(u.split("=") for u in listable_user_meta.split(","))
820-
821-
return {"listable_user_meta" : listable_user_meta , "nl_user_meta" : nl_user_meta}
823+
if not SIMULATE:
824+
nl_user_meta = {}
825+
listable_user_meta = {}
826+
827+
if response.info().getheader('x-emc-meta'):
828+
nl_user_meta = response.info().getheader('x-emc-meta')
829+
nl_user_meta = dict(u.split("=") for u in nl_user_meta.split(",")) # Create a Python dictionary of the data in the header and return it.
830+
831+
if response.info().getheader('x-emc-listable-meta'):
832+
listable_user_meta = response.info().getheader('x-emc-listable-meta')
833+
listable_user_meta = dict(u.split("=") for u in listable_user_meta.split(","))
834+
835+
return {"listable_user_meta" : listable_user_meta , "nl_user_meta" : nl_user_meta}
822836

823837

824838
def get_system_metadata(self, object_id, sys_tags = None):
@@ -863,10 +877,11 @@ def get_system_metadata(self, object_id, sys_tags = None):
863877
return error_message
864878

865879
else:
866-
system_meta = []
867-
system_meta = response.info().getheader('x-emc-meta')
868-
system_meta = dict(u.split("=") for u in system_meta.split(",")) # Create a Python dictionary of the data in the header and return it.
869-
return system_meta
880+
if not SIMULATE:
881+
system_meta = []
882+
system_meta = response.info().getheader('x-emc-meta')
883+
system_meta = dict(u.split("=") for u in system_meta.split(",")) # Create a Python dictionary of the data in the header and return it.
884+
return system_meta
870885

871886
def get_listable_tags(self, metadata_key = None):
872887
""" Returns all the top level listable keys for which the given UID has access to in their namespace.
@@ -904,16 +919,19 @@ def get_listable_tags(self, metadata_key = None):
904919
hashout = self.__sign(headers)
905920

906921
try:
922+
907923
response = self.__send_request(request, hashout, headers)
908924

909925
except urllib2.HTTPError, e:
910926
error_message = e.read()
911927
return error_message
912928

913929
else:
914-
listable_tags = []
915-
listable_tags = response.info().getheader('x-emc-listable-tags')
916-
return listable_tags
930+
931+
if not SIMULATE:
932+
listable_tags = []
933+
listable_tags = response.info().getheader('x-emc-listable-tags')
934+
return listable_tags
917935

918936
def get_service_information(self):
919937
""" Returns Atmos version information. """
@@ -957,9 +975,10 @@ def __send_request(self, request, hashout, headers):
957975
if DEBUG:
958976
print request.headers
959977

960-
response = urllib2.urlopen(request)
961-
962-
return response
978+
if not SIMULATE:
979+
980+
response = urllib2.urlopen(request)
981+
return response
963982

964983

965984
def __sign(self, headers):

0 commit comments

Comments
 (0)