Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add MinIO configuration #20

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions cloud_storage_handler/api/elixircloud/csh/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Model for MinIO Configuration."""

from typing import Annotated

from pydantic import BaseModel, Field, constr


class MinioConfig(BaseModel):
"""Configuration for MinIO."""
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved

hostname: str = Field(
...,
description="The hostname where the MinIO server is running.",
example="localhost",
)
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
port: int = Field(
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
..., description="The port on which the MinIO server is running.", example=9000
)
access_key: str = Field(
...,
description="The access key used for authentication with MinIO.",
example="minioadmin",
)
secret_key: str = Field(
...,
description="The secret key used for authentication with MinIO.",
example="minioadmin",
)
is_secure: bool = Field(
...,
description=(
"Specifies whether the connection to MinIO should be secure (HTTPS)."
),
example=False,
)
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
bucket_name: Annotated[str, constr(min_length=1)] = Field(
...,
description="The name of the bucket where files are stored.",
example="files",
)
27 changes: 27 additions & 0 deletions cloud_storage_handler/consts.py
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""This module handles the loading of environment variables for MinIO configuration."""
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved

from cloud_storage_handler.minio_client import MinioClient


def GetMinioConfiguration(): # type: ignore
"""Get minio configuration."""
minio_config = MinioClient()
return minio_config.response()


minio_config_data = GetMinioConfiguration()

MINIO_HOSTNAME = minio_config_data["hostname"]
MINIO_PORT = minio_config_data["port"]
MINIO_ENDPOINT = f"{MINIO_HOSTNAME}:{MINIO_PORT}"
MINIO_SECURE = minio_config_data["is_secure"]

minio_client_instance = MinioClient()
minio_client = minio_client_instance.initialise_minio(
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
endpoint=MINIO_ENDPOINT,
access_key=minio_config_data["access_key"],
secret_key=minio_config_data["secret_key"],
secure=MINIO_SECURE,
)

minio_client_instance.create_bucket(minio_config_data["bucket_name"])
12 changes: 12 additions & 0 deletions cloud_storage_handler/custom_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Custom configuration model for the FOCA app."""

from pydantic import BaseModel

from cloud_storage_handler.api.elixircloud.csh.models import MinioConfig


class CustomConfig(BaseModel):
"""Custom configuration model for the FOCA app."""

# Define custom configuration fields here
minio: MinioConfig
5 changes: 5 additions & 0 deletions cloud_storage_handler/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

from werkzeug.exceptions import BadRequest, InternalServerError, NotFound


class ConfigNotFoundError(FileNotFoundError):
"""Configuration file not found error."""


exceptions = {
Exception: {
"message": "An unexpected error occurred. Please try again.",
Expand Down
1 change: 1 addition & 0 deletions cloud_storage_handler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def init_app() -> FlaskApp:

foca = Foca(
config_file=config_path,
custom_config_model="cloud_storage_handler.custom_config.CustomConfig",
)
return foca.create_app()

Expand Down
74 changes: 74 additions & 0 deletions cloud_storage_handler/minio_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""This module provides functionality for managing MinIO client."""

import logging

from foca import Foca
from minio import Minio

from cloud_storage_handler.exceptions import ConfigNotFoundError
from cloud_storage_handler.utils import get_config_path

logger = logging.getLogger(__name__)


class MinioClient:
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
"""A class to manage MinIO client configuration and bucket creation."""

def __init__(self) -> None:
"""Initializes the MinIO client class."""
self.minio_config_data = None
self.config = Foca(config_file=get_config_path()).conf
self.client = None # MinIO client will be initialized later

def initialise_minio(self, endpoint, access_key, secret_key, secure):
"""Initialize the MinIO client with provided configurations."""
self.client = Minio(
endpoint=endpoint,
access_key=access_key,
secret_key=secret_key,
secure=secure, # Correctly use the secure flag
)
return self.client

def create_bucket(self, bucket_name):
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
"""Create a new bucket if it does not already exist."""
if self.client is None:
raise RuntimeError("MinIO client is not initialized.")

if not self.client.bucket_exists(bucket_name):
self.client.make_bucket(bucket_name)

def get_default_minio_config(self):
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
"""Get the default minio configuration."""
logger.warning(
"Minio configuration not found in config. Using default configuration."
)
return {
"hostname": "localhost",
"port": 9000,
"access_key": "minioadmin",
"secret_key": "minioadmin",
"is_secure": False,
"bucket_name": "files",
}

def get_minio_from_config(self):
"""Returns minio configuration from config."""
if not self.config.custom:
raise ConfigNotFoundError("Custom configuration not found.")

minio_config_data = self.config.custom.get("minio")
if not minio_config_data:
raise ConfigNotFoundError("Service info not found in custom configuration.")
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved

return minio_config_data

def response(self):
"""Returns minio configuration response."""
if self.minio_config_data is None:
try:
self.minio_config_data = self.get_minio_from_config()
except ConfigNotFoundError:
self.minio_config_data = self.get_default_minio_config()

return self.minio_config_data
37 changes: 37 additions & 0 deletions cloud_storage_handler/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Utility functions for the Cloud Storage Handler package."""

import os
from pathlib import Path

from foca import Foca

from cloud_storage_handler.custom_config import CustomConfig
from cloud_storage_handler.exceptions import ConfigNotFoundError


def get_config_path() -> Path:
"""Get the configuration path.

Returns:
The path of the config file.
"""
# Determine the configuration path
if config_path_env := os.getenv("CSH_FOCA_CONFIG_PATH"):
return Path(config_path_env).resolve()
else:
return (Path(__file__).parents[1] / "deployment" / "config.yaml").resolve()
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved


def get_custom_config() -> CustomConfig:
"""Get the custom configuration.

Returns:
The custom configuration.
"""
conf = Foca(config_file=get_config_path()).conf
try:
return CustomConfig(**conf.custom)
except AttributeError:
raise ConfigNotFoundError(
"Custom configuration not found in config file."
) from None
10 changes: 10 additions & 0 deletions deployment/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ api:
swagger_ui: true
serve_spec: true

custom:
# MinIO configuration based on MinIO model
minio:
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
hostname: localhost
port: 9001
access_key: minioadmin
secret_key: minioadmin
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
is_secure: false
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
bucket_name: 'files'

exceptions:
required_members: [['message'], ['code']]
status_member: ['code']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ cloud\_storage\_handler.api.elixircloud.csh.controllers module
:undoc-members:
:show-inheritance:

cloud\_storage\_handler.api.elixircloud.csh.models module
---------------------------------------------------------

.. automodule:: cloud_storage_handler.api.elixircloud.csh.models
:members:
:undoc-members:
:show-inheritance:
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved

Module contents
---------------

Expand Down
32 changes: 32 additions & 0 deletions docs/source/pages/cloud_storage_handler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ Subpackages
Submodules
----------

cloud\_storage\_handler.consts module
-------------------------------------

.. automodule:: cloud_storage_handler.consts
:members:
:undoc-members:
:show-inheritance:

cloud\_storage\_handler.custom\_config module
---------------------------------------------

.. automodule:: cloud_storage_handler.custom_config
:members:
:undoc-members:
:show-inheritance:
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved
psankhe28 marked this conversation as resolved.
Show resolved Hide resolved

cloud\_storage\_handler.exceptions module
-----------------------------------------

Expand All @@ -28,6 +44,22 @@ cloud\_storage\_handler.main module
:undoc-members:
:show-inheritance:

cloud\_storage\_handler.minio\_client module
--------------------------------------------

.. automodule:: cloud_storage_handler.minio_client
:members:
:undoc-members:
:show-inheritance:

cloud\_storage\_handler.utils module
------------------------------------

.. automodule:: cloud_storage_handler.utils
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------

Expand Down
Loading
Loading