async def vbd_list(cluster_id: str, vdi_uuid: str): """ Get VBD 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") vbds = VBD.get_all(session=session) __vbd_list = [] _vbd_list = __vbd_list.append for vbd in vbds: if vbd.get_VDI().get_uuid() == vdi_uuid: _vbd_list(serialize(vbd)) if vbds is not None: ret = dict(success=True, data=__vbd_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 _vbd_plug(cluster_id: str, vbd_uuid: str): """Plug VBD into VM""" try: session = create_session(_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) vbd: VBD = VBD.get_by_uuid(session=session, uuid=vbd_uuid) if vbd is not None: ret = dict(success=vbd.plug()) 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_list(cluster_id: str): """Get VBD by UUID""" try: session = create_session( cluster_id, get_xen_clusters=Settings.get_xen_clusters() ) vbds = VBD.get_all(session) __vbd_list = await asyncio.gather(*[serialize(vbd) for vbd in vbds]) if vbds is not None: ret = dict(success=True, data=__vbd_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 vbd_get_by_uuid(cluster_id: str, vbd_uuid: str): """ Get VBD 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") vbd: VBD = VBD.get_by_uuid(session=session, uuid=vbd_uuid) if vbd is not None: ret = dict(success=True, data=serialize(vbd)) 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 verify_vbd_uuid(cluster_id: str, vbd_uuid: Optional[str] = None): if vbd_uuid is None: return session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) try: vbd = VBD.get_by_uuid(session, vbd_uuid) except Failure as xenapi_error: if xenapi_error.details[0] == "UUID_INVALID": raise HTTPException( status_code=404, detail=f"VBD {vbd_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()
def get_VBDs(self, vbd_type=None): from XenGarden.VBD import VBD vbds = self.session.xenapi.VM.get_VBDs(self.vm) vbd_list = [] for vbd in vbds: try: vbd_obj = VBD(self.session, vbd) vbd_obj.get_uuid() if vbd_type is not None: if vbd_type == vbd_obj.get_type(): vbd_list.append(vbd_obj) else: vbd_list.append(vbd_obj) except: pass return vbd_list
def serialize(vbd: VBD): vm = vbd.get_VM() vdi = vbd.get_VDI() if vm is not None: vm = _vm_serialize(vm) if vdi is not None: vdi = _vdi_serialize(vdi) return dict( vm=vm, vdi=vdi, bootable=vbd.get_bootable(), attached=vbd.get_currently_attached(), unpluggable=vbd.get_unpluggable(), device=vbd.get_device(), type=vbd.get_type(), uuid=vbd.get_uuid(), mode=vbd.get_mode(), )
async def serialize(vbd: VBD): from API.v1.VDI.serialize import serialize as _vdi_serialize from API.v1.VM.serialize import serialize as _vm_serialize vm = vbd.get_VM() vdi = vbd.get_VDI() if vm is not None: vm = await _vm_serialize(vm) if vdi is not None: vdi = await _vdi_serialize(vdi) return dict( vm=vm, vdi=vdi, bootable=vbd.get_bootable(), attached=vbd.get_currently_attached(), unpluggable=vbd.get_unpluggable(), device=vbd.get_device(), type=vbd.get_type(), uuid=vbd.get_uuid(), mode=vbd.get_mode(), )
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)