def serialize(vdi: VDI): return dict( name=vdi.get_name(), description=vdi.get_description(), uuid=vdi.get_uuid(), location=vdi.get_location(), type=vdi.get_type(), )
def get_VDI(self) -> VDI: """ get VDI attached to the specified VBD """ try: vdi = self.session.xenapi.VBD.get_VDI(self.vbd) vdi = VDI(self.session, vdi) vdi.get_uuid() return vdi except Failure as xenapi_error: if xenapi_error.details[0] == "HANDLE_INVALID": return None else: raise xenapi_error
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 find_VDI_by_name(cluster_id: str, args: NameArgs): """Find VDI by Name""" try: session = create_session(_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) name = args.name vdis = VDI.get_by_name(session=session, name=name) if vdis is not None: __vdis_list = [] for vdi in vdis: __vdis_list.append(await serialize(vdi)) ret = dict(success=True, data=__vdis_list) 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_list(cluster_id: str): """Get VDI by UUID""" try: session = create_session(_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) vdis = VDI.get_all(session=session) __vdi_list = await asyncio.gather(*[serialize(vdi) for vdi in vdis]) if vdis is not None: ret = dict(success=True, data=__vdi_list) 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_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_list(cluster_id: str): """ Get VDI 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") vdis = VDI.get_all(session=session) __vdi_list = [] _vdi_list = __vdi_list.append for vdi in vdis: _vdi_list(serialize(vdi)) if vdis is not None: ret = dict(success=True, data=__vdi_list) 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_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 insert_cd_inurl_name(cluster_id: str, iso_name: str): """ Find VDI by Name """ 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" ) vdis = VDI.get_by_name(session=session, name=iso_name) print(vdis) if vdis is not None: __vdis_list = [] vdis_list = __vdis_list.append for vdi in vdis: vdis_list(serialize(vdi)) ret = dict(success=True, data=__vdis_list) 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)
def get_VDIs(self): vdis = self.session.xenapi.SR.get_VDIs(self.sr) vdi_list = [] for vdi in vdis: vdi_list.append(VDI(self.session, vdi)) return vdi_list
def get_VDI(self) -> VDI: """ get VDI attached to the specified VBD """ try: vdi = self.session.xenapi.VBD.get_VDI(self.vbd) if vdi is not None: return VDI(self.session, vdi) else: return None except Exception as e: print("VBD.get_VDI Exception", e) return None
def get_VDIs(self): try: vdis = self.session.xenapi.SR.get_VDIs(self.sr) vdi_list = [] for vdi in vdis: vdi_list.append(VDI(self.session, vdi)) return vdi_list except Exception as e: print("SR.get_VDIs Exception", e) return
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)