Skip to content

Commit

Permalink
Add Tapis tracking IDs to datafiles operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jarosenb committed Nov 26, 2024
1 parent e175834 commit a801145
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
12 changes: 6 additions & 6 deletions designsafe/apps/api/datafiles/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@
}


def datafiles_get_handler(api, client, scheme, system, path, operation, username=None, **kwargs):
def datafiles_get_handler(api, client, scheme, system, path, operation, username=None, tapis_tracking_id=None, **kwargs):
if operation not in allowed_actions[scheme]:
raise PermissionDenied
op = getattr(operations_mapping[api], operation)

try:
return op(client, system, path, username=username, **kwargs)
return op(client, system, path, username=username, tapis_tracking_id=tapis_tracking_id, **kwargs)
except BaseTapyException as exc:
raise ApiException(message=exc.message, status=500) from exc


def datafiles_post_handler(api, username, client, scheme, system,
path, operation, body=None):
path, operation, body=None, tapis_tracking_id=None):

if operation not in allowed_actions[scheme]:
raise PermissionDenied
Expand All @@ -51,7 +51,7 @@ def datafiles_post_handler(api, username, client, scheme, system,
operation in notify_actions and notify(username, operation, '{} operation has started.'.format(operation.capitalize()), 'INFO', {})

try:
result = op(client, system, path, **body)
result = op(client, system, path, tapis_tracking_id=tapis_tracking_id, **body)
operation in notify_actions and notify(username, operation, '{} operation was successful.'.format(operation.capitalize()), 'SUCCESS', result)
return result
except Exception as exc:
Expand All @@ -60,15 +60,15 @@ def datafiles_post_handler(api, username, client, scheme, system,


def datafiles_put_handler(api, username, client, scheme, system,
path, operation, body=None):
path, operation, body=None, tapis_tracking_id=None):
if operation not in allowed_actions[scheme]:
raise PermissionDenied

op = getattr(operations_mapping[api], operation)
operation in notify_actions and notify(username, operation, '{} operation has started.'.format(operation.capitalize()), 'INFO', {})

try:
result = op(client, system, path, **body)
result = op(client, system, path, tapis_tracking_id=tapis_tracking_id, **body)
if operation == 'copy' and system != body.get('dest_system', None):
notify(username, operation, 'Your file transfer request has been received and will be processed shortly.'.format(operation.capitalize()), 'SUCCESS', result)
else:
Expand Down
38 changes: 23 additions & 15 deletions designsafe/apps/api/datafiles/operations/tapis_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def listing(client, system, path, offset=0, limit=100, q=None, *args, **kwargs):
raw_listing = client.files.listFiles(systemId=system,
path=(path or '/'),
offset=int(offset),
limit=int(limit))
limit=int(limit),
headers={"X-Tapis-Tracking-ID": kwargs.get("tapis_tracking_id")})

try:
# Convert file objects to dicts for serialization.
Expand Down Expand Up @@ -83,7 +84,7 @@ def detail(client, system, path, *args, **kwargs):
"""
Retrieve the uuid for a file by parsing the query string in _links.metadata.href
"""
_listing = client.files.listFiles(systemId=system, path=urllib.parse.quote(path), offset=0, limit=1)
_listing = client.files.listFiles(systemId=system, path=urllib.parse.quote(path), offset=0, limit=1, headers={"X-Tapis-Tracking-ID": kwargs.get("tapis_tracking_id")})
f = _listing[0]
listing_res = {
'system': system,
Expand Down Expand Up @@ -229,7 +230,7 @@ def mkdir(client, system, path, dir_name):
return {"result": "OK"}


def move(client, src_system, src_path, dest_system, dest_path):
def move(client, src_system, src_path, dest_system, dest_path, *args, **kwargs):
"""
Move files and related file metadata
Expand Down Expand Up @@ -259,7 +260,8 @@ def move(client, src_system, src_path, dest_system, dest_path):
client.files.moveCopy(systemId=src_system,
path=src_path,
operation="MOVE",
newPath=dest_path_full)
newPath=dest_path_full,
headers={"X-Tapis-Tracking-ID": kwargs.get("tapis_tracking_id")})

move_file_meta_async.delay(src_system, src_path, dest_system, dest_path_full)

Expand All @@ -286,7 +288,7 @@ def move(client, src_system, src_path, dest_system, dest_path):
return {"result": "OK"}


def copy(client, src_system, src_path, dest_system, dest_path):
def copy(client, src_system, src_path, dest_system, dest_path, *args, **kwargs):
"""
Copy files and related file metadata
Expand Down Expand Up @@ -321,15 +323,16 @@ def copy(client, src_system, src_path, dest_system, dest_path):
copy_result = client.files.moveCopy(systemId=src_system,
path=src_path,
operation="COPY",
newPath=full_dest_path)
newPath=full_dest_path,
headers={"X-Tapis-Tracking-ID": kwargs.get("tapis_tracking_id")})
else:
src_url = f'tapis://{src_system}/{src_path}'
dest_url = f'tapis://{dest_system}/{full_dest_path}'

copy_response = client.files.createTransferTask(elements=[{
'sourceURI': src_url,
'destinationURI': dest_url
}])
}], headers={"X-Tapis-Tracking-ID": kwargs.get("tapis_tracking_id")})
copy_result = {
'uuid': copy_response.uuid,
'status': copy_response.status,
Expand Down Expand Up @@ -362,11 +365,12 @@ def copy(client, src_system, src_path, dest_system, dest_path):
return dict(copy_result)


def delete(client, system, path):
def delete(client, system, path, *args, **kwargs):
return client.files.delete(systemId=system,
filePath=urllib.parse.quote(path))
filePath=urllib.parse.quote(path),
headers={"X-Tapis-Tracking-ID": kwargs.get("tapis_tracking_id")})

def rename(client, system, path, new_name):
def rename(client, system, path, new_name, *args, **kwargs):
"""Renames a file. This is performed under the hood by moving the file to
the same parent folder but with a new name.
Expand Down Expand Up @@ -397,7 +401,8 @@ def rename(client, system, path, new_name):
client.files.moveCopy(systemId=system,
path=path,
operation="MOVE",
newPath=new_path)
newPath=new_path,
headers={"X-Tapis-Tracking-ID": kwargs.get("tapis_tracking_id")})

move_file_meta_async.delay(system, path, system, new_path)

Expand All @@ -415,7 +420,7 @@ def rename(client, system, path, new_name):



def trash(client, system, path, trash_path):
def trash(client, system, path, trash_path, *args, **kwargs):
"""Move a file to the .Trash folder.
Params
Expand All @@ -442,7 +447,7 @@ def trash(client, system, path, trash_path):
except tapipy.errors.NotFoundError:
mkdir(client, system, trash_root, trash_foldername)

resp = move(client, system, path, system, trash_path)
resp = move(client, system, path, system, trash_path, headers={"X-Tapis-Tracking-ID": kwargs.get("tapis_tracking_id")})

return resp

Expand Down Expand Up @@ -480,7 +485,10 @@ def upload(client, system, path, uploaded_file, webkit_relative_path=None, *args


dest_path = os.path.join(path.strip('/'), uploaded_file.name)
response_json = client.files.insert(systemId=system, path=dest_path, file=uploaded_file)
response_json = client.files.insert(systemId=system,
path=dest_path,
file=uploaded_file,
headers={"X-Tapis-Tracking-ID": kwargs.get("tapis_tracking_id")})
return {"result": "OK"}
agave_indexer.apply_async(kwargs={'systemId': system,
'filePath': path,
Expand Down Expand Up @@ -540,7 +548,7 @@ def preview(client, system, path, href="", max_uses=3, lifetime=600, *args, **kw
except FileMetaModel.DoesNotExist:
meta = {}

postit_result = client.files.createPostIt(systemId=system, path=path, allowedUses=max_uses, validSeconds=lifetime)
postit_result = client.files.createPostIt(systemId=system, path=path, allowedUses=max_uses, validSeconds=lifetime, headers={"X-Tapis-Tracking-ID": kwargs.get("tapis_tracking_id")})
url = postit_result.redeemUrl

if file_ext in settings.SUPPORTED_TEXT_PREVIEW_EXTS:
Expand Down
6 changes: 3 additions & 3 deletions designsafe/apps/api/datafiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get(self, request, api, operation=None, scheme='private', system=None, path=

try:
response = datafiles_get_handler(
api, client, scheme, system, path, operation, username=request.user.username, **request.GET.dict())
api, client, scheme, system, path, operation, tapis_tracking_id=f"portals.{request.session.session_key}", username=request.user.username, **request.GET.dict())
return JsonResponse(response)
except (BoxOAuthException, DropboxAuthError, GoogleAuthError):
raise resource_expired_handler(api)
Expand Down Expand Up @@ -104,7 +104,7 @@ def put(self, request, api, operation=None, scheme='private', system=None, path=
raise resource_unconnected_handler(api)

try:
response = datafiles_put_handler(api, request.user.username, client, scheme, system, path, operation, body=body)
response = datafiles_put_handler(api, request.user.username, client, scheme, system, path, operation, tapis_tracking_id=f"portals.{request.session.session_key}", body=body)
except HTTPError as e:
return JsonResponse({'message': str(e)}, status=e.response.status_code)

Expand Down Expand Up @@ -135,7 +135,7 @@ def post(self, request, api, operation=None, scheme='private', system=None, path
except AttributeError:
raise resource_unconnected_handler(api)

response = datafiles_post_handler(api, request.user.username, client, scheme, system, path, operation, body={**post_files, **post_body})
response = datafiles_post_handler(api, request.user.username, client, scheme, system, path, operation, tapis_tracking_id=f"portals.{request.session.session_key}", body={**post_files, **post_body})

return JsonResponse(response)

Expand Down

0 comments on commit a801145

Please sign in to comment.