From 1e4737b3cd0349c4681c5000d641006d6f4687f9 Mon Sep 17 00:00:00 2001
From: Jacopo Gasparetto <jacopo.gasparetto@cnaf.infn.it>
Date: Wed, 5 Apr 2023 12:14:34 +0200
Subject: [PATCH] Add GET /buckets to list all buckets

---
 backend/src/__init__.py        |  0
 backend/src/ceph_service.py    | 29 +++++++++++++++++++++++++++++
 backend/src/main.py            | 11 ++---------
 backend/src/routes/__init__.py |  0
 backend/src/routes/buckets.py  |  9 +++++++++
 5 files changed, 40 insertions(+), 9 deletions(-)
 create mode 100644 backend/src/__init__.py
 create mode 100644 backend/src/ceph_service.py
 create mode 100644 backend/src/routes/__init__.py
 create mode 100644 backend/src/routes/buckets.py

diff --git a/backend/src/__init__.py b/backend/src/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/backend/src/ceph_service.py b/backend/src/ceph_service.py
new file mode 100644
index 0000000..a39481b
--- /dev/null
+++ b/backend/src/ceph_service.py
@@ -0,0 +1,29 @@
+import boto3
+import json
+import os
+
+MINIO_ENDPOINT = os.getenv("MINIO_ENDPOINT", "http://minio:9000")
+AWS_ACCESS_KEY_ID = os.getenv("MINIO_ACCESS_KEY_ID", "minioadmin")
+AWS_SECRET_ACCESS_KEY = os.getenv("MINIO_SECRET_ACCESS_KEY", "minioadmin")
+AWS_SESSION_TOKEN = None
+
+
+def get_s3_client():
+    resource = boto3.resource('s3',
+                              endpoint_url=MINIO_ENDPOINT,
+                              aws_access_key_id=AWS_ACCESS_KEY_ID,
+                              aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
+                              aws_session_token=AWS_SESSION_TOKEN,
+                              config=boto3.session.Config(signature_version='s3v4'),
+                              verify=False
+                              )
+    client = resource.meta.client
+    return client
+
+
+def list_buckets() -> json:
+    s3 = get_s3_client()
+    buckets = s3.list_buckets()
+    buckets = {key: buckets[key] for key in ["Buckets", "Owner"]}
+    return json.dumps(buckets, default=str)
+
diff --git a/backend/src/main.py b/backend/src/main.py
index d5c0b27..0544fc9 100644
--- a/backend/src/main.py
+++ b/backend/src/main.py
@@ -1,14 +1,7 @@
 from typing import Union
 from fastapi import FastAPI
+from routes import buckets
 
 app = FastAPI()
 
-
-@app.get("/")
-def read_root():
-    return {"Hello": "World"}
-
-
-@app.get("/items/{item_id}")
-def read_item(item_id: int, q: Union[str, None] = None):
-    return {"item_id": item_id, "q": q}
\ No newline at end of file
+app.include_router(buckets.router)
diff --git a/backend/src/routes/__init__.py b/backend/src/routes/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/backend/src/routes/buckets.py b/backend/src/routes/buckets.py
new file mode 100644
index 0000000..193a973
--- /dev/null
+++ b/backend/src/routes/buckets.py
@@ -0,0 +1,9 @@
+from fastapi import APIRouter
+import ceph_service
+
+router = APIRouter()
+
+
+@router.get("/buckets")
+def list_buckets():
+    return ceph_service.list_buckets()
-- 
GitLab