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)
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 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 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()