From 11827ee2d48c15976b23c4bb1670b1e5b6da5b13 Mon Sep 17 00:00:00 2001 From: Pratiksha Sankhe Date: Wed, 11 Sep 2024 23:13:25 +0530 Subject: [PATCH 1/3] endpoint to get all files Signed-off-by: Pratiksha Sankhe --- .../api/elixircloud/csh/controllers.py | 15 +++++++++++++++ cloud_storage_handler/api/specs/specs.yaml | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/cloud_storage_handler/api/elixircloud/csh/controllers.py b/cloud_storage_handler/api/elixircloud/csh/controllers.py index fb23a34..6f65fc5 100644 --- a/cloud_storage_handler/api/elixircloud/csh/controllers.py +++ b/cloud_storage_handler/api/elixircloud/csh/controllers.py @@ -4,6 +4,9 @@ from http import HTTPStatus from flask import jsonify +from minio.error import S3Error + +from cloud_storage_handler.clients.minio import get_minio_client logger = logging.getLogger(__name__) @@ -13,3 +16,15 @@ def home(): return jsonify( {"message": "Welcome to the Cloud Storage Handler server!"} ), HTTPStatus.OK + + +def list_files(): + """Endpoint to list all files in the MinIO bucket.""" + try: + minio_client = get_minio_client() + objects = minio_client.list_objects("files") + files = [obj.object_name for obj in objects] + return jsonify({"files": files}), 200 + + except S3Error as err: + return jsonify({"error": str(err)}), 500 diff --git a/cloud_storage_handler/api/specs/specs.yaml b/cloud_storage_handler/api/specs/specs.yaml index 2c61d7f..eebacc3 100644 --- a/cloud_storage_handler/api/specs/specs.yaml +++ b/cloud_storage_handler/api/specs/specs.yaml @@ -35,4 +35,22 @@ paths: description: The request is malformed. '500': description: An unexpected error occurred. + /list_files: + get: + description: | + Returns a list of all files in the minio storage + operationId: list_files + responses: + '200': + description: The list of files has been retrieved successfully. + content: + application/json: + schema: + type: array + items: + type: string + '400': + description: The request is malformed. + '500': + description: An unexpected error occurred. ... From 42caf726eaef24fe4a9432a5051f113c2737b4e2 Mon Sep 17 00:00:00 2001 From: Pratiksha Sankhe Date: Mon, 14 Oct 2024 23:20:11 +0530 Subject: [PATCH 2/3] added functionality to list all files Signed-off-by: Pratiksha Sankhe --- .../api/elixircloud/csh/controllers.py | 10 ++++---- cloud_storage_handler/api/specs/specs.yaml | 2 +- tests/test_integration/test_operations.py | 23 +++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cloud_storage_handler/api/elixircloud/csh/controllers.py b/cloud_storage_handler/api/elixircloud/csh/controllers.py index 6f65fc5..e10ca32 100644 --- a/cloud_storage_handler/api/elixircloud/csh/controllers.py +++ b/cloud_storage_handler/api/elixircloud/csh/controllers.py @@ -3,11 +3,9 @@ import logging from http import HTTPStatus -from flask import jsonify +from flask import current_app, jsonify from minio.error import S3Error -from cloud_storage_handler.clients.minio import get_minio_client - logger = logging.getLogger(__name__) @@ -21,8 +19,10 @@ def home(): def list_files(): """Endpoint to list all files in the MinIO bucket.""" try: - minio_client = get_minio_client() - objects = minio_client.list_objects("files") + minio_config = current_app.config.foca.custom.minio + bucket_name = minio_config.bucket_name + minio_client = current_app.config.foca.custom.minio.client.client + objects = minio_client.list_objects(bucket_name) files = [obj.object_name for obj in objects] return jsonify({"files": files}), 200 diff --git a/cloud_storage_handler/api/specs/specs.yaml b/cloud_storage_handler/api/specs/specs.yaml index eebacc3..7c67e5e 100644 --- a/cloud_storage_handler/api/specs/specs.yaml +++ b/cloud_storage_handler/api/specs/specs.yaml @@ -38,7 +38,7 @@ paths: /list_files: get: description: | - Returns a list of all files in the minio storage + Returns a list of all files in the minio bucket operationId: list_files responses: '200': diff --git a/tests/test_integration/test_operations.py b/tests/test_integration/test_operations.py index 949cb18..1d0c00f 100644 --- a/tests/test_integration/test_operations.py +++ b/tests/test_integration/test_operations.py @@ -27,3 +27,26 @@ def test_get_root(): mock_get.assert_called_once_with(server_url) print("Finished test_get_root") + + +def test_get_files(): + """Test the list_file endpoint of the service with a mocked response.""" + print("Starting test_get_files...") + + server_url = "http://localhost:8080/elixircoud/csh/v1/list_files" + + with mock.patch("requests.get") as mock_get: + mock_response = mock.Mock() + mock_response.status_code = HTTPStatus.OK + mock_get.return_value = mock_response + + response = requests.get(server_url) + print(f"Response status code: {response.status_code}") + + assert ( + response.status_code == HTTPStatus.OK + ), f"Expected status code 200, got {response.status_code}" + + mock_get.assert_called_once_with(server_url) + + print("Finished test_get_files") From 097a550656728352fbd3f395f8ba33f597254607 Mon Sep 17 00:00:00 2001 From: Pratiksha Sankhe Date: Mon, 14 Oct 2024 23:28:46 +0530 Subject: [PATCH 3/3] added check for file key Signed-off-by: Pratiksha Sankhe --- tests/test_integration/test_operations.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_integration/test_operations.py b/tests/test_integration/test_operations.py index 1d0c00f..2c69c81 100644 --- a/tests/test_integration/test_operations.py +++ b/tests/test_integration/test_operations.py @@ -38,14 +38,15 @@ def test_get_files(): with mock.patch("requests.get") as mock_get: mock_response = mock.Mock() mock_response.status_code = HTTPStatus.OK + mock_response.json.return_value = {"files": ["file1.txt", "file2.txt"]} mock_get.return_value = mock_response response = requests.get(server_url) print(f"Response status code: {response.status_code}") - assert ( - response.status_code == HTTPStatus.OK - ), f"Expected status code 200, got {response.status_code}" + assert response.status_code == HTTPStatus.OK + assert "files" in response.json() + assert isinstance(response.json()["files"], list) mock_get.assert_called_once_with(server_url)