Skip to content

Commit

Permalink
add functionality to disable direct upload to cache
Browse files Browse the repository at this point in the history
  • Loading branch information
MBueschelberger committed Sep 6, 2023
1 parent 5b0237e commit ab9c457
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions osp/app/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ def depends_upload(
return _get_upload(temp.name, uuid, minio_client)


def depends_upload_enabled(config: AppConfig = Depends(get_appconfig)) -> bool:
"""Check whether direct upoad to data cache is enabled or not"""
return config.enable_upload


def depends_download(
dataset_name: str = Query(..., title="Cache ID received after the upload."),
minio_client: Minio = Depends(depends_minio),
Expand Down
16 changes: 16 additions & 0 deletions osp/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
depends_modellist,
depends_registry,
depends_upload,
depends_upload_enabled,
get_app,
get_appconfig,
get_dependencies,
Expand All @@ -47,6 +48,7 @@
TaskStatusModel,
TransformationStatus,
UploadDataResponse,
UploadNotEnabledError,
task_to_transformation_map,
)

Expand Down Expand Up @@ -100,8 +102,11 @@ async def root():
@app.put("/data/cache", operation_id="createDataset")
async def upload_data(
object_key: UUID = Depends(depends_upload),
upload_enabled: bool = Depends(depends_upload_enabled),
) -> UploadDataResponse:
"""Upload data from internal cache"""
if not upload_enabled:
raise UploadNotEnabledError("Direct upload to cache is not enabled")
return UploadDataResponse(id=object_key, last_modified=str(datetime.now()))


Expand Down Expand Up @@ -321,6 +326,17 @@ async def download_error_handler(request, exc): # pylint: disable=unused-argume
return JSONResponse(status_code=422, content="Error while fetching the resource")


@app.exception_handler(UploadNotEnabledError)
async def upload_error_handler(request, exc): # pylint: disable=unused-argument
"""Return response based on the MinioDownloadError"""
# Raise an HTTPException with a 422 status code and the error messages
return JSONResponse(
status_code=403,
content="""Direct upload to cache is not enabled.
Please contact an administrator.""",
)


# @app.exception_handler(MinioConnectionError)
# async def connection_error_handler(request, exc): # pylint: disable=unused-argument
# """Return response based on the MinioDownloadError"""
Expand Down
4 changes: 4 additions & 0 deletions osp/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,7 @@ class InfoType(str, Enum):

SCHEMA = "Schema"
EXAMPLE = "Example"


class UploadNotEnabledError(Exception):
"""Upload not permitted since not enabled by admin."""
6 changes: 6 additions & 0 deletions osp/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class AppConfig(RedisSettings):
None, description="Resolvable hostname to the outside world."
)

enable_upload: Optional[bool] = Field(
False,
description="""Whether direct data upload into
the cache should be enabled or not""",
)

class Config:
"""Pydantic config for FastAPI-celery settings"""

Expand Down

0 comments on commit ab9c457

Please sign in to comment.