Spaces:
Building
Building
from fastapi import APIRouter, Depends, HTTPException | |
from config_provider import get_config, ServiceConfig | |
from utils import save_service_config, log | |
import requests | |
router = APIRouter() | |
def get_spark_projects(config: ServiceConfig = Depends(get_config)): | |
try: | |
spark_url = config.spark_endpoint + "/projects" | |
response = requests.get(spark_url, timeout=5) | |
response.raise_for_status() | |
projects = response.json() | |
return { "projects": projects } | |
except Exception as e: | |
log(f"β οΈ Spark service unreachable: {str(e)}") | |
return { "error": True, "projects": [] } | |
def enable_project(project_name: str, config: ServiceConfig = Depends(get_config)): | |
project = next((p for p in config.data['projects'] if p['name'] == project_name), None) | |
if not project: | |
raise HTTPException(status_code=404, detail="Project not found") | |
project['enabled'] = True | |
save_service_config(config) | |
log(f"β Project '{project_name}' enabled") | |
return { "status": "enabled" } | |
def disable_project(project_name: str, config: ServiceConfig = Depends(get_config)): | |
project = next((p for p in config.data['projects'] if p['name'] == project_name), None) | |
if not project: | |
raise HTTPException(status_code=404, detail="Project not found") | |
project['enabled'] = False | |
save_service_config(config) | |
log(f"β Project '{project_name}' disabled") | |
return { "status": "disabled" } | |
def delete_project_spark(project_name: str, config: ServiceConfig = Depends(get_config)): | |
project = next((p for p in config.data['projects'] if p['name'] == project_name), None) | |
if not project: | |
raise HTTPException(status_code=404, detail="Project not found") | |
config.data['projects'].remove(project) | |
save_service_config(config) | |
log(f"ποΈ Project '{project_name}' deleted from Spark") | |
return { "status": "deleted" } | |