72 lines
2.3 KiB
Python
72 lines
2.3 KiB
Python
import logging
|
|
from typing import List
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.orm import Session
|
|
|
|
from .. import models, schemas, proxmox_client as pve
|
|
from ..database import get_db
|
|
from ..deps import get_current_user, require_admin
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter(prefix="/templates", tags=["templates"])
|
|
|
|
|
|
@router.get("", response_model=List[schemas.TemplateOut])
|
|
def list_templates(db: Session = Depends(get_db), _=Depends(get_current_user)):
|
|
"""Возвращает активные шаблоны, сохранённые в панели."""
|
|
return db.query(models.Template).filter(models.Template.is_active == True).all() # noqa: E712
|
|
|
|
|
|
@router.post("", response_model=schemas.TemplateOut)
|
|
def create_template(
|
|
payload: schemas.TemplateCreate,
|
|
db: Session = Depends(get_db),
|
|
_=Depends(require_admin),
|
|
):
|
|
"""Добавляет шаблон в каталог панели."""
|
|
tpl = models.Template(**payload.model_dump())
|
|
db.add(tpl)
|
|
db.commit()
|
|
db.refresh(tpl)
|
|
return tpl
|
|
|
|
|
|
@router.get("/from-proxmox")
|
|
def list_proxmox_templates(_=Depends(require_admin)):
|
|
"""Возвращает VM-шаблоны и архивные LXC-шаблоны с Proxmox.
|
|
|
|
Готовые LXC-контейнеры по VMID намеренно не показываются.
|
|
"""
|
|
try:
|
|
vm_templates = pve.list_vm_templates()
|
|
except Exception as exc:
|
|
logger.warning("Ошибка получения VM-шаблонов из Proxmox: %s", exc)
|
|
vm_templates = []
|
|
|
|
try:
|
|
lxc_templates = pve.list_lxc_templates()
|
|
except Exception as exc:
|
|
logger.warning("Ошибка получения архивов LXC-шаблонов из Proxmox: %s", exc)
|
|
lxc_templates = []
|
|
|
|
for template in lxc_templates:
|
|
template["source_kind"] = "archive"
|
|
|
|
return {
|
|
"vm_templates": vm_templates,
|
|
"lxc_templates": lxc_templates,
|
|
}
|
|
|
|
|
|
@router.delete("/{template_id}")
|
|
def deactivate_template(
|
|
template_id: int, db: Session = Depends(get_db), _=Depends(require_admin)
|
|
):
|
|
"""Деактивирует шаблон в каталоге панели."""
|
|
tpl = db.query(models.Template).filter(models.Template.id == template_id).first()
|
|
if tpl:
|
|
tpl.is_active = False
|
|
db.commit()
|
|
return {"ok": True}
|