def serialize(host: Host): return dict( uuid=host.get_uuid(), name=host.get_name(), description=host.get_description(), enabled=host.get_enabled(), memory=dict(free=host.get_free_memory(), total=host.get_total_memory()), cpu=host.get_cpu_info(), bios=replace_under_bar(host.get_bios_strings()), version=host.get_software_version(), )
async def serialize(host: Host): record = host.get_record() return dict( uuid=record["uuid"], name=record["name_label"], enabled=record["enabled"], memory=dict( overhead=record["memory_overhead"], free=host.get_free_memory(), total=host.get_total_memory(), ), cpu=record["cpu_info"], bios=record["bios_strings"], version=record["software_version"], )
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 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 guest_get_by_uuid( cluster_id: str = Path(default=None, title="cluster_id", description="Cluster ID"), guest_uuid: str = Path(default=None, title="guest_uuid", description="Guest UUID"), ): """ Get GuestMetrics by UUID """ try: # KeyError Handling 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") guest: GuestMetrics = Host.get_by_uuid(session=session, uuid=guest_uuid) if guest is not None: ret = dict(success=True, data=serialize(guest)) 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 host_list( cluster_id: str = Path(default=None, title="cluster_id", description="Cluster ID") ): """ Get All from Existance Host """ try: # KeyError Handling 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" ) hosts = Host.list_host(session=session) __hosts_list = [] hosts_list = __hosts_list.append for host in hosts: hosts_list(serialize(host)) ret = dict(success=True, data=__hosts_list) 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_host_uuid(cluster_id: str, host_uuid: Optional[str] = None): if host_uuid is None: return session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) try: host = Host.get_by_uuid(session, host_uuid) except Failure as xenapi_error: if xenapi_error.details[0] == "UUID_INVALID": raise HTTPException(status_code=404, detail=f"Host {host_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()