async def serialize(sr: SR): return dict( name=sr.get_name(), description=sr.get_description(), size=sr.get_physical_size(), used=sr.get_physical_utilisation(), free=str( int(sr.get_physical_size()) - int(sr.get_physical_utilisation())), uuid=sr.get_uuid(), content_type=sr.get_content_type(), type=sr.get_type(), )
async def sr_get_by_uuid(cluster_id: str, sr_uuid: str): """Get SR by UUID""" try: session = create_session( cluster_id, get_xen_clusters=Settings.get_xen_clusters() ) sr: SR = SR.get_by_uuid(session=session, uuid=sr_uuid) if sr is not None: ret = dict(success=True, data=await serialize(sr)) 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 sr_scan(cluster_id: str, sr_uuid: str): """ Scan Storage Repository """ 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") sr: SR = SR.get_by_uuid(session=session, uuid=sr_uuid) if sr is not None: sr.scan() ret = dict(success=True) 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_SR(self): from XenGarden.SR import SR data = self.session.xenapi.VDI.get_SR(self.vdi) sr = SR(self.session, data) return sr
async def sr_list(cluster_id: str): """ Get All from Storage Repos """ 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" ) srs = SR.get_all(session=session) __sant_sr = [] sant_sr = __sant_sr.append for sr in srs: sant_sr(serialize(sr)) ret = dict(success=True, data=__sant_sr) 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 sr_vdis(cluster_id: str, sr_uuid: str): """Get VDIs by SR""" from API.v1.VDI.serialize import serialize as _vdi_serialize try: session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) sr: SR = SR.get_by_uuid(session=session, uuid=sr_uuid) vdis = sr.get_VDIs() if vdis is not None: vdis = await asyncio.gather(*[_vdi_serialize(vdi) for vdi in vdis]) else: pass if sr is not None: ret = dict(success=True, data=vdis) 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 find_cd_by_name(cluster_id: str, args: NameArgs): """Find SR by Name""" try: session = create_session( _id=cluster_id, get_xen_clusters=Settings.get_xen_clusters() ) name = args.name srs = SR.get_by_name(session=session, name=name) sr = None __srs_list = [] for sr in srs: __srs_list.append(await serialize(sr)) if sr is not None: ret = dict(success=True, data=__srs_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 insert_cd_inurl_name(cluster_id: str, iso_name: str): """ Find SR 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") srs = SR.get_by_name(session=session, name=iso_name) if srs is not None: __srs_list = [] srs_list = __srs_list.append for sr in srs: srs_list(serialize(sr)) ret = dict(success=True, data=__srs_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 serialize(sr: SR): vdis = sr.get_VDIs() __vdi_list = [] vdi_list = __vdi_list.append if vdis is not None: for vdi in vdis: vdi_list(_vdi_serialize(vdi)) else: __vdi_list = None return dict( name=sr.get_name(), description=sr.get_description(), size=sr.get_physical_size(), uuid=sr.get_uuid(), content_type=sr.get_content_type(), type=sr.get_type(), vdis=__vdi_list, )
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 sr_list(cluster_id: str): """Get All from Storage Repos""" try: session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) srs = SR.get_all(session=session) __sant_sr = await asyncio.gather(*[serialize(sr) for sr in srs]) ret = dict(success=True, data=__sant_sr) 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 instance_copy(cluster_id: str, vm_uuid: str, args: CopyArgs): """Copy Instance (VM/Template)""" try: session = create_session(_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) _vm: VM = VM.get_by_uuid(session=session, uuid=vm_uuid) _sr: SR = SR.get_by_uuid(session=session, uuid=args.sr_uuid) new_vm = await _vm.copy(args.name, _sr) if new_vm is not None: if args.provision: await new_vm.provision() new_vm_uuid = new_vm.get_uuid() ret = Response( "", status_code=302, headers={"Location": f"/v1/{cluster_id}/vm/{new_vm_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 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()