From 04986798d74d99b0bdb4c80fc4631eafb2185d4f Mon Sep 17 00:00:00 2001 From: Alex4386 Date: Sun, 8 May 2022 10:47:11 +0900 Subject: [PATCH] chore: implement network reference from VIF --- API/v1/VIF/__init__.py | 2 ++ API/v1/VIF/network.py | 59 +++++++++++++++++++++++++++++++++++++++++ API/v1/VIF/serialize.py | 6 +++++ requirements.txt | 2 +- 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 API/v1/VIF/network.py diff --git a/API/v1/VIF/__init__.py b/API/v1/VIF/__init__.py index c2d466e..ee59dca 100644 --- a/API/v1/VIF/__init__.py +++ b/API/v1/VIF/__init__.py @@ -14,6 +14,7 @@ from API.v1.VIF.ipv6 import router as _vif_ipv6 from API.v1.VIF.ipv6_allowed import router as _vif_ipv6_allowed from API.v1.VIF.list import router as _vif_list +from API.v1.VIF.network import router as _vif_network from API.v1.VIF.lock import router as _vif_lock from API.v1.VIF.qos import router as _vif_qos from app.settings import Settings @@ -58,4 +59,5 @@ async def verify_vif_uuid(cluster_id: str, vif_uuid: Optional[str] = None): vif_router.include_router(_vif_ipv6, tags=["vif"]) vif_router.include_router(_vif_ipv6_allowed, tags=["vif"]) vif_router.include_router(_vif_lock, tags=["vif"]) +vif_router.include_router(_vif_network, tags=["vif"]) vif_router.include_router(_vif_qos, tags=["vif"]) diff --git a/API/v1/VIF/network.py b/API/v1/VIF/network.py new file mode 100644 index 0000000..5be5920 --- /dev/null +++ b/API/v1/VIF/network.py @@ -0,0 +1,59 @@ +from http.client import RemoteDisconnected +from xmlrpc.client import Fault + +from fastapi import APIRouter, HTTPException +from XenAPI.XenAPI import Failure +from starlette.responses import RedirectResponse +from XenGarden.session import create_session +from XenGarden.VIF import VIF + +from API.v1.Common import xenapi_failure_jsonify +from API.v1.VIF.serialize import serialize +from app.settings import Settings + +router = APIRouter() + + +@router.get("/{cluster_id}/vif/{vif_uuid}/network{url_after:path}") +@router.post("/{cluster_id}/vif/{vif_uuid}/network{url_after:path}") +@router.patch("/{cluster_id}/vif/{vif_uuid}/network{url_after:path}") +@router.put("/{cluster_id}/vif/{vif_uuid}/network{url_after:path}") +@router.delete("/{cluster_id}/vif/{vif_uuid}/network{url_after:path}") +async def vif_get_network(cluster_id: str, vif_uuid: str, url_after: str = ""): + """Redirect To VIF's Network""" + try: + session = create_session( + _id=cluster_id, get_xen_clusters=Settings.get_xen_clusters() + ) + + vif: VIF = VIF.get_by_uuid(session=session, uuid=vif_uuid) + + if vif is None: + session.xenapi.session.logout() + raise HTTPException( + status_code=404, detail=f"VIF {vif_uuid} does not exist" + ) + + network = vif.get_network() + + if network is None: + session.xenapi.session.logout() + raise HTTPException( + status_code=404, detail=f"VIF {vif_uuid} does not exist on ANY Network" + ) + + network_uuid = network.get_uuid() + + session.xenapi.session.logout() + return RedirectResponse(f"/v1/{cluster_id}/network/{network_uuid}{url_after}") + except Failure as xenapi_error: + 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, + ) + except RemoteDisconnected as rd_error: + raise HTTPException(status_code=500, detail=rd_error.strerror) diff --git a/API/v1/VIF/serialize.py b/API/v1/VIF/serialize.py index 2d48fb9..1b6c0e1 100644 --- a/API/v1/VIF/serialize.py +++ b/API/v1/VIF/serialize.py @@ -3,14 +3,20 @@ async def serialize(vif: VIF): from API.v1.VM.serialize import serialize as _vm_serialize + from API.v1.Network.serialize import serialize as _network_serialize vm = vif.get_vm() if vm is not None: vm = await _vm_serialize(vm) + network = vif.get_network() + if network is not None: + network = await _network_serialize(network) + return dict( attached=vif.get_attached(), vm=vm, + network=network, uuid=vif.get_uuid(), mac=vif.get_mac(), mtu=vif.get_mtu(), diff --git a/requirements.txt b/requirements.txt index 8af0949..787e357 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ databases[mysql] mysqlclient requests schedule -XenGarden==1.2.9 +XenGarden==1.2.10 pyfiglet fastapi uvicorn