async def vdi_get_by_uuid(cluster_id: str, vdi_uuid: str): """ Delete SR by UUID """ try: try: session = create_session( _id=cluster_id, get_xen_clusters=Settings.get_xen_clusters() ) except KeyError as key_error: raise HTTPException( status_code=400, detail=f"{key_error} is not a valid path" ) vdi: VDI = VDI.get_by_uuid(session=session, uuid=vdi_uuid) if vdi is not None: ret = dict(success=vdi.destroy()) else: ret = dict(success=False) session.xenapi.session.logout() return ret 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)
async def vdi_get_by_uuid(cluster_id: str, vdi_uuid: str): """Get VDI by UUID""" try: session = create_session(_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) vdi: VDI = VDI.get_by_uuid(session=session, uuid=vdi_uuid) if vdi is not None: ret = dict(success=True, data=await serialize(vdi)) else: ret = dict(success=False) session.xenapi.session.logout() return ret 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)
async def vdi_get_sr(cluster_id: str, vdi_uuid: str, url_after: str = ""): """Redirect To SR""" try: session = create_session( _id=cluster_id, get_xen_clusters=Settings.get_xen_clusters() ) vdi: VDI = VDI.get_by_uuid(session=session, uuid=vdi_uuid) sr = None try: sr = vdi.get_SR() except Exception as e: session.xenapi.session.logout() print(e) raise HTTPException( status_code=404, detail=f"VDI {vdi_uuid} does not have proper SR" ) sr_uuid = sr.get_uuid() session.xenapi.session.logout() return RedirectResponse(f"/v1/{cluster_id}/sr/{sr_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)
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()
async def get_cd_insert_inurl(cluster_id: str, vm_uuid: str, vdi_uuid: str): try: try: session = create_session( _id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) except KeyError as key_error: raise HTTPException(status_code=400, detail=f"{key_error} is not a valid path") vm: VM = VM.get_by_uuid(session=session, uuid=vm_uuid) if vm is not None: new_vbd = vm.get_CD() if new_vbd is not None: try: from XenGarden.VDI import VDI except ModuleNotFoundError as e: raise HTTPException(status_code=500, detail=e.name) from API.v1.VDI.serialize import serialize as _vdi_serialize vdi: VDI = VDI.get_by_uuid(session=session, uuid=vdi_uuid) if vdi is not None: success = new_vbd.insert(vdi) if success: ret = dict(success=success, data=_vdi_serialize(vdi)) else: ret = dict(success=success) else: ret = dict(success=False) else: ret = dict(success=False) else: ret = dict(success=False) session.xenapi.session.logout() return ret 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)
async def vdi_copy(cluster_id: str, vdi_uuid: str, args: SRCopyArgs): """Get VDI by UUID""" try: session = create_session( _id=cluster_id, get_xen_clusters=Settings.get_xen_clusters() ) vdi: VDI = VDI.get_by_uuid(session=session, uuid=vdi_uuid) sr: SR = SR.get_by_uuid(session=session, uuid=args.sr_uuid) new_vdi: VDI = await vdi.copy(sr) if new_vdi is not None: vdi_uuid = new_vdi new_vdi_uuid = new_vdi.get_uuid() ret = Response( "", status_code=302, headers={"Location": f"/v1/{cluster_id}/vdi/{new_vdi_uuid}"}, ) else: ret = dict(success=False) session.xenapi.session.logout() return ret 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)
async def vbd_create(cluster_id: str, create_args: VBDCreateArgs): """Create VBD""" try: session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) vm = VM.get_by_uuid(session, create_args.vm_uuid) vdi = VDI.get_by_uuid(session, create_args.vdi_uuid) vbd: VBD = VBD.create( session, vm, vdi, **create_args.dict(), ) if vbd is not None: vbd_uuid = vbd.get_uuid() ret = Response( "", status_code=302, headers={"Location": f"/v1/{cluster_id}/vbd/{vbd_uuid}"}, ) else: ret = dict(success=False) session.xenapi.session.logout() return ret 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)
async def vdi_resize(cluster_id: str, vdi_uuid: str, args: SizeArgs): """Resize VDI""" try: session = create_session(_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) vdi: VDI = VDI.get_by_uuid(session=session, uuid=vdi_uuid) await vdi.resize(args.size) ret = dict(success=True) session.xenapi.session.logout() return ret 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)