diff --git a/API/v1/Console/__init__.py b/API/v1/Console/__init__.py index 2e0379d..9b7c862 100644 --- a/API/v1/Console/__init__.py +++ b/API/v1/Console/__init__.py @@ -1,6 +1,43 @@ -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException, Depends +from XenGarden.session import create_session +from app.settings import Settings +from XenAPI.XenAPI import Failure +from API.v1.Common import xenapi_failure_jsonify +from xmlrpc.client import Fault +from typing import Optional -from API.v1.Console.info import router as _console_router +from XenGarden.Console import Console -console_router = APIRouter() -console_router.include_router(_console_router, tags=["console"]) +from API.v1.Console.info import router as _console_info + + +# === Condition Checker === +async def verify_console_uuid(cluster_id: str, console_uuid: Optional[str] = None): + if console_uuid is None: + return + + session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) + + try: + console = Console.get_by_uuid(session, console_uuid) + + except Failure as xenapi_error: + if xenapi_error.details[0] == "UUID_INVALID": + raise HTTPException(status_code=404, detail=f"Console {console_uuid} does not exist") + + raise HTTPException( + status_code=500, detail=xenapi_failure_jsonify(xenapi_error) + ) + except Fault as xml_rpc_error: + raise HTTPException( + status_code=int(xml_rpc_error.faultCode), + detail=xml_rpc_error.faultString, + ) + + session.xenapi.session.logout() + + +# === Router Actions === +console_router = APIRouter(dependencies=[Depends(verify_console_uuid)]) + +console_router.include_router(_console_info, tags=["console"]) diff --git a/API/v1/GuestMetrics/__init__.py b/API/v1/GuestMetrics/__init__.py index e33cbef..c339d29 100644 --- a/API/v1/GuestMetrics/__init__.py +++ b/API/v1/GuestMetrics/__init__.py @@ -1,6 +1,43 @@ -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException, Depends +from XenGarden.session import create_session +from app.settings import Settings +from XenAPI.XenAPI import Failure +from API.v1.Common import xenapi_failure_jsonify +from xmlrpc.client import Fault +from typing import Optional -from API.v1.GuestMetrics.info import router as _info_router +from XenGarden.GuestMetrics import GuestMetrics -guest_router = APIRouter() -guest_router.include_router(_info_router, tags=["guest"]) +from API.v1.GuestMetrics.info import router as _guest_info + + +# === Condition Checker === +async def verify_guest_uuid(cluster_id: str, guest_uuid: Optional[str] = None): + if guest_uuid is None: + return + + session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) + + try: + guest_metrics = GuestMetrics.get_by_uuid(session, guest_uuid) + + except Failure as xenapi_error: + if xenapi_error.details[0] == "UUID_INVALID": + raise HTTPException(status_code=404, detail=f"GuestMetrics {guest_uuid} does not exist") + + raise HTTPException( + status_code=500, detail=xenapi_failure_jsonify(xenapi_error) + ) + except Fault as xml_rpc_error: + raise HTTPException( + status_code=int(xml_rpc_error.faultCode), + detail=xml_rpc_error.faultString, + ) + + session.xenapi.session.logout() + + +# === Router Actions === +guest_router = APIRouter(dependencies=[Depends(verify_guest_uuid)]) + +guest_router.include_router(_guest_info, tags=["guest"]) diff --git a/API/v1/Host/__init__.py b/API/v1/Host/__init__.py index 6faba02..5a3d51f 100644 --- a/API/v1/Host/__init__.py +++ b/API/v1/Host/__init__.py @@ -1,8 +1,45 @@ -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException, Depends +from XenGarden.session import create_session +from app.settings import Settings +from XenAPI.XenAPI import Failure +from API.v1.Common import xenapi_failure_jsonify +from xmlrpc.client import Fault +from typing import Optional + +from XenGarden.Host import Host from API.v1.Host.info import router as _host_info from API.v1.Host.list import router as _host_list -host_router = APIRouter() + +# === Condition Checker === +async def verify_host_uuid(cluster_id: str, host_uuid: Optional[str] = None): + if host_uuid is None: + return + + session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) + + try: + host = Host.get_by_uuid(session, host_uuid) + + except Failure as xenapi_error: + if xenapi_error.details[0] == "UUID_INVALID": + raise HTTPException(status_code=404, detail=f"Host {host_uuid} does not exist") + + raise HTTPException( + status_code=500, detail=xenapi_failure_jsonify(xenapi_error) + ) + except Fault as xml_rpc_error: + raise HTTPException( + status_code=int(xml_rpc_error.faultCode), + detail=xml_rpc_error.faultString, + ) + + session.xenapi.session.logout() + + +# === Router Actions === +host_router = APIRouter(dependencies=[Depends(verify_host_uuid)]) + host_router.include_router(_host_list, tags=["host"]) host_router.include_router(_host_info, tags=["host"]) diff --git a/API/v1/SR/__init__.py b/API/v1/SR/__init__.py index 2b1d1bd..d6ba2ac 100644 --- a/API/v1/SR/__init__.py +++ b/API/v1/SR/__init__.py @@ -1,11 +1,49 @@ -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException, Depends +from XenGarden.session import create_session +from app.settings import Settings +from XenAPI.XenAPI import Failure +from API.v1.Common import xenapi_failure_jsonify +from xmlrpc.client import Fault +from typing import Optional + +from XenGarden.SR import SR from API.v1.SR.find import router as _sr_find from API.v1.SR.info import router as _sr_info from API.v1.SR.list import router as _sr_list from API.v1.SR.scan import router as _sr_scan -sr_router = APIRouter() + +# === Condition Checker === +async def verify_sr_uuid(cluster_id: str, sr_uuid: Optional[str] = None): + if sr_uuid is None: + return + + session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) + + try: + sr = SR.get_by_uuid(session, sr_uuid) + + except Failure as xenapi_error: + if xenapi_error.details[0] == "UUID_INVALID": + raise HTTPException(status_code=404, detail=f"SR {sr_uuid} does not exist") + + raise HTTPException( + status_code=500, detail=xenapi_failure_jsonify(xenapi_error) + ) + except Fault as xml_rpc_error: + raise HTTPException( + status_code=int(xml_rpc_error.faultCode), + detail=xml_rpc_error.faultString, + ) + + session.xenapi.session.logout() + + +# === Router Actions === + +sr_router = APIRouter(dependencies=[Depends(verify_sr_uuid)]) + sr_router.include_router(_sr_list, tags=["sr"]) sr_router.include_router(_sr_find, tags=["sr"]) sr_router.include_router(_sr_info, tags=["sr"]) diff --git a/API/v1/VBD/__init__.py b/API/v1/VBD/__init__.py index 39a79ee..bdf9b9e 100644 --- a/API/v1/VBD/__init__.py +++ b/API/v1/VBD/__init__.py @@ -1,4 +1,12 @@ -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException, Depends +from XenGarden.session import create_session +from app.settings import Settings +from XenAPI.XenAPI import Failure +from API.v1.Common import xenapi_failure_jsonify +from xmlrpc.client import Fault +from typing import Optional + +from XenGarden.VBD import VBD from API.v1.VBD.delete import router as _vbd_delete from API.v1.VBD.find_vdi import router as _vbd_find_by_vdi @@ -6,7 +14,36 @@ from API.v1.VBD.list import router as _vbd_list from API.v1.VBD.media import router as _vbd_media -vbd_router = APIRouter() + +# === Condition Checker === +async def verify_vbd_uuid(cluster_id: str, vbd_uuid: Optional[str] = None): + if vbd_uuid is None: + return + + session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) + + try: + vbd = VBD.get_by_uuid(session, vbd_uuid) + + except Failure as xenapi_error: + if xenapi_error.details[0] == "UUID_INVALID": + raise HTTPException(status_code=404, detail=f"VBD {vbd_uuid} does not exist") + + raise HTTPException( + status_code=500, detail=xenapi_failure_jsonify(xenapi_error) + ) + except Fault as xml_rpc_error: + raise HTTPException( + status_code=int(xml_rpc_error.faultCode), + detail=xml_rpc_error.faultString, + ) + + session.xenapi.session.logout() + + +# === Router Actions === +vbd_router = APIRouter(dependencies=[Depends(verify_vbd_uuid)]) + vbd_router.include_router(_vbd_list, tags=["vbd"]) vbd_router.include_router(_vbd_info, tags=["vbd"]) vbd_router.include_router(_vbd_find_by_vdi, tags=["vbd"]) diff --git a/API/v1/VDI/__init__.py b/API/v1/VDI/__init__.py index f492d07..19aee7a 100644 --- a/API/v1/VDI/__init__.py +++ b/API/v1/VDI/__init__.py @@ -1,11 +1,47 @@ -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException, Depends +from XenGarden.session import create_session +from app.settings import Settings +from XenAPI.XenAPI import Failure +from API.v1.Common import xenapi_failure_jsonify +from xmlrpc.client import Fault +from typing import Optional + +from XenGarden.VDI import VDI from API.v1.VDI.delete import router as _vdi_delete from API.v1.VDI.find import router as _vdi_find from API.v1.VDI.info import router as _vdi_info from API.v1.VDI.list import router as _vdi_list -vdi_router = APIRouter() +# === Condition Checker === +async def verify_vdi_uuid(cluster_id: str, vdi_uuid: Optional[str] = None): + if vdi_uuid is None: + return + + session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) + + try: + vdi = VDI.get_by_uuid(session, vdi_uuid) + + except Failure as xenapi_error: + if xenapi_error.details[0] == "UUID_INVALID": + raise HTTPException(status_code=404, detail=f"VDI {vdi_uuid} does not exist") + + raise HTTPException( + status_code=500, detail=xenapi_failure_jsonify(xenapi_error) + ) + except Fault as xml_rpc_error: + raise HTTPException( + status_code=int(xml_rpc_error.faultCode), + detail=xml_rpc_error.faultString, + ) + + session.xenapi.session.logout() + + +# === Router Actions === +vdi_router = APIRouter(dependencies=[Depends(verify_vdi_uuid)]) + vdi_router.include_router(_vdi_list, tags=["vdi"]) vdi_router.include_router(_vdi_find, tags=["vdi"]) vdi_router.include_router(_vdi_info, tags=["vdi"]) diff --git a/API/v1/VIF/__init__.py b/API/v1/VIF/__init__.py index 6bac466..bfa31cd 100644 --- a/API/v1/VIF/__init__.py +++ b/API/v1/VIF/__init__.py @@ -1,4 +1,12 @@ -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException, Depends +from XenGarden.session import create_session +from app.settings import Settings +from XenAPI.XenAPI import Failure +from API.v1.Common import xenapi_failure_jsonify +from xmlrpc.client import Fault +from typing import Optional + +from XenGarden.VIF import VIF from API.v1.VIF.info import router as _vif_info from API.v1.VIF.ipv4 import router as _vif_ipv4 @@ -9,9 +17,38 @@ from API.v1.VIF.lock import router as _vif_lock from API.v1.VIF.qos import router as _vif_qos -vif_router = APIRouter() -vif_router.include_router(_vif_info, tags=["vif"]) + +# === Condition Checker === +async def verify_vif_uuid(cluster_id: str, vif_uuid: Optional[str] = None): + if vif_uuid is None: + return + + session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) + + try: + vif = VIF.get_by_uuid(session, vif_uuid) + + except Failure as xenapi_error: + if xenapi_error.details[0] == "UUID_INVALID": + raise HTTPException(status_code=404, detail=f"VIF {vif_uuid} does not exist") + + raise HTTPException( + status_code=500, detail=xenapi_failure_jsonify(xenapi_error) + ) + except Fault as xml_rpc_error: + raise HTTPException( + status_code=int(xml_rpc_error.faultCode), + detail=xml_rpc_error.faultString, + ) + + session.xenapi.session.logout() + + +# === Router Actions === +vif_router = APIRouter(dependencies=[Depends(verify_vif_uuid)]) + vif_router.include_router(_vif_list, tags=["vif"]) +vif_router.include_router(_vif_info, tags=["vif"]) vif_router.include_router(_vif_ipv4, tags=["vif"]) vif_router.include_router(_vif_ipv4_allowed, tags=["vif"]) vif_router.include_router(_vif_ipv6, tags=["vif"]) diff --git a/API/v1/VM/__init__.py b/API/v1/VM/__init__.py index 82fc11d..d240f0e 100644 --- a/API/v1/VM/__init__.py +++ b/API/v1/VM/__init__.py @@ -1,7 +1,11 @@ +from fastapi import APIRouter, HTTPException, Depends +from XenGarden.session import create_session +from app.settings import Settings +from XenAPI.XenAPI import Failure +from API.v1.Common import xenapi_failure_jsonify from xmlrpc.client import Fault +from typing import Optional -from fastapi import APIRouter, HTTPException -from XenGarden.session import create_session from XenGarden.VM import VM from API.v1.VM.bios import router as _vm_bios @@ -19,20 +23,22 @@ from API.v1.VM.specs import router as _vm_specs from API.v1.VM.vbd import router as _vm_vbd from API.v1.VM.vif import router as _vm_vif -from app.settings import Settings # === Condition Checker === -async def verify_vm_uuid(cluster_id: str, vm_uuid: str): - # KeyError Handling +async def verify_vm_uuid(cluster_id: str, vm_uuid: Optional[str] = None): + if vm_uuid is None: + return + session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) try: - vm = VM.get_by_uuid(vm_uuid) + vm = VM.get_by_uuid(session, vm_uuid) - if vm is None: - raise HTTPException(status_code=404, detail=f"VM {vm_uuid} does not exist") except Failure as xenapi_error: + if xenapi_error.details[0] == "UUID_INVALID": + raise HTTPException(status_code=404, detail=f"VM {vm_uuid} does not exist") + raise HTTPException( status_code=500, detail=xenapi_failure_jsonify(xenapi_error) ) @@ -45,8 +51,8 @@ async def verify_vm_uuid(cluster_id: str, vm_uuid: str): session.xenapi.session.logout() -# === Router Checker === -vm_router = APIRouter() +# === Router Actions === +vm_router = APIRouter(dependencies=[Depends(verify_vm_uuid)]) vm_router.include_router(_vm_bios, tags=["vm"]) vm_router.include_router(_vm_cd, tags=["vm"]) diff --git a/pyproject.toml b/pyproject.toml index 4d746f3..c5ecb52 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "XenXenXenSe" -version = "0.1.1" +version = "1.0.0" description = "An easy-to-use HTTP API of XenAPI for Web Application Developers." authors = [ "Alex4386 ", diff --git a/requirements.txt b/requirements.txt index 464b881..7d960d7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ databases[mysql] mysqlclient requests schedule -XenGarden>=1.1.3 +XenGarden>=1.1.5 pyfiglet fastapi uvicorn