async def get_first_vif(cluster_id: str, vm_uuid: str, url_after: str = ""): """Redirect To First Instance VIFs""" 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) vif = None try: vif = vm.get_VIF() except Exception: session.xenapi.session.logout() raise HTTPException( status_code=404, detail=f"VM {vm_uuid} does not have VIF Interface") vif_uuid = vif.get_uuid() session.xenapi.session.logout() return RedirectResponse(f"/v1/{cluster_id}/vif/{vif_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 instance_info(cluster_id: str, vm_uuid: str): """Get an Info of VM or 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) if vm is not None: ret = dict(success=True, data=await serialize(vm)) 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 instance_get_metadata(cluster_id: str, vm_uuid: str): """Get Instance (VM/Template) Metadata""" 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) ret = dict( success=True, data={ "name": _vm.get_name(), "description": _vm.get_description() }, ) 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_console_uuid(cluster_id: str, console_uuid: Optional[str] = None): if console_uuid is None: return session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) try: console = Console.get_by_uuid(session, console_uuid) except Failure as xenapi_error: if xenapi_error.details[0] == "UUID_INVALID": raise HTTPException( status_code=404, detail=f"Console {console_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 vm_set_vCPU(cluster_id: str, vm_uuid: str, args: VCpuArgs): """Set VM vCPU count""" 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) if _vm is not None: ret = dict(success=_vm.set_vCPUs(args.vCPU_count)) 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 vm_get_memory(cluster_id: str, vm_uuid: str): """Get VM Memory (needs troubleshooting)""" 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) if _vm is not None: ret = dict(success=True, data=_vm.get_memory()) 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_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 vif_get_ipv4_by_uuid(cluster_id: str, vif_uuid: str): """Get VIF IPv4 by UUID""" try: session = create_session(_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) vif: VIF = VIF.get_by_uuid(session=session, uuid=vif_uuid) ret = dict( success=True, data=dict( address=vif.get_address_v4(), gateway=vif.get_gateway_v4(), ), ) 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): """Delete SR 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=vdi.destroy()) 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 get_cd(cluster_id: str, vm_uuid: str, url_after: str = ""): from XenGarden.VBD import VBD 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) vbd: VBD = vm.get_CD() vbd_uuid = vbd.get_uuid() session.xenapi.session.logout() return RedirectResponse( url=f"/v1/{cluster_id}/vbd/{vbd_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 vm_consoles(cluster_id: str, vm_uuid: str): """Get all consoles are available to the VM""" 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) consoles = vm.get_consoles() __consoleList = [] for console in consoles: __consoleList.append(_console_serialize(console)) ret = dict(success=True, data=__consoleList) 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 vm_console(cluster_id: str, vm_uuid: str, url_after: str = ""): """Get the first console of the VM""" 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) consoles: Console = vm.get_consoles() if len(consoles) == 0: raise HTTPException( status_code=404, detail=f"Console doesn't exist on VM {vm_uuid}", ) console: Console = consoles[0] console_uuid = console.get_uuid() return RedirectResponse( url=f"/v1/{cluster_id}/console/{console_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 vif_set_ipv6_by_uuid( cluster_id: str, vif_uuid: str, addresses: IPAddressesModel ): """Set VIF IPv6 by UUID""" try: session = create_session( _id=cluster_id, get_xen_clusters=Settings.get_xen_clusters() ) vif: VIF = VIF.get_by_uuid(session=session, uuid=vif_uuid) vif.set_allowed_address_v6(addresses.addresses) 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)
async def host_list(cluster_id: str = Path(default=None, title="cluster_id", description="Cluster ID")): """Get All from Existance Host""" try: session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) hosts = Host.list_host(session=session) __hosts_list = await asyncio.gather( *[serialize(host) for host in hosts]) ret = dict(success=True, data=__hosts_list) 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 host_get_by_uuid( cluster_id: str = Path(default=None, title="cluster_id", description="Cluster ID"), host_uuid: str = Path(default=None, title="host_uuid", description="Host UUID"), ): """Get Host by UUID""" try: session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) host: Host = Host.get_by_uuid(session=session, uuid=host_uuid) if host is not None: ret = dict(success=True, data=await serialize(host)) 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_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 template_list(cluster_id: str): """Gets Templates available on Xen Server""" try: session = create_session( _id=cluster_id, get_xen_clusters=Settings.get_xen_clusters() ) vms = VM.list_templates(session=session) __sat = [] for vm in vms: __sat.append(await serialize(vm)) ret = dict(success=True, data=__sat) 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 vif_get_by_uuid(cluster_id: str, vif_uuid: str): """Get VIF by UUID""" try: session = create_session(_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) vif: VIF = VIF.get_by_uuid(session=session, uuid=vif_uuid) if vif is not None: ret = dict(success=True, data=await serialize(vif)) 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_clone(cluster_id: str, vm_uuid: str, args: CloneArgs): """Clone 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) new_vm = await _vm.clone(args.name) 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 pif_list(cluster_id: str): """Get All PIF from cluster""" try: session = create_session( _id=cluster_id, get_xen_clusters=Settings.get_xen_clusters() ) pifs = PIF.get_all(session=session) __santilized_pifs = await asyncio.gather(*[serialize(pif) for pif in pifs]) ret = dict(success=True, data=__santilized_pifs) 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 vif_get_qos_type_by_uuid(cluster_id: str, vif_uuid: str, data: QoSTypeArgs): """Set VIF QoS Data by UUID""" try: session = create_session(_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) vif: VIF = VIF.get_by_uuid(session=session, uuid=vif_uuid) result = True if data.type is not None: vif.set_qos_type(data.type) if data.info is not None: vif.set_qos_info(data.info) ret = dict(success=True, data=result) 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: 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: sr.scan() ret = dict(success=True) 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_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 console_get_by_uuid( cluster_id: str, console_uuid: str, ): """Get Console by UUID""" try: pass session = create_session( cluster_id, get_xen_clusters=Settings.get_xen_clusters() ) console: Console = Console.get_by_uuid(session, console_uuid) if console is not None: ret = dict(success=True, data=await serialize(console)) else: ret = dict(success=False) 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_cluster_id(cluster_id: str): # KeyError Handling try: session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) session.xenapi.session.logout() except KeyError as key_error: raise HTTPException(status_code=404, detail=f"{key_error} cluster does not exist") 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, )
async def instance_get_bios(cluster_id: str, vm_uuid: str): """Get Instance (VM/Template) BIOS""" 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) bios_strings = vm.get_bios_strings() session.xenapi.session.logout() return dict(success=True, data=bios_strings) 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 vm_delete(cluster_id: str, vm_uuid: str): """Delete VM""" 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) ret = dict(success=await _vm.delete()) 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_vbds(cluster_id: str, vm_uuid: str): """Show Instance VBDs""" 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) if vm is not None: newVBDs = vm.get_VBDs() __vbd_serialized = [] if newVBDs is not None: __vbd_serialized = await asyncio.gather( *[_vbd_serialize(vbd) for vbd in newVBDs] ) ret = dict(success=True, data=__vbd_serialized) else: ret = dict(success=False) 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)