File size: 2,033 Bytes
183b47d
 
 
 
926928e
 
 
fc87357
 
 
 
 
 
 
 
 
 
183b47d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from fastapi import APIRouter, Depends, HTTPException
from config_provider import get_config, ServiceConfig
from utils import save_service_config, log
import requests

router = APIRouter()

@router.get("/spark/projects")
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": [] }

@router.post("/spark/enable")
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" }

@router.post("/spark/disable")
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" }

@router.post("/spark/delete")
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" }