Skip to content

Commit

Permalink
chore: implement network reference from VIF
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed May 8, 2022
1 parent 7d4dbe6 commit 0498679
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
2 changes: 2 additions & 0 deletions API/v1/VIF/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
59 changes: 59 additions & 0 deletions API/v1/VIF/network.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions API/v1/VIF/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ databases[mysql]
mysqlclient
requests
schedule
XenGarden==1.2.9
XenGarden==1.2.10
pyfiglet
fastapi
uvicorn
Expand Down

0 comments on commit 0498679

Please sign in to comment.