async def vm_power_unpause(cluster_id: str, vm_uuid: str): """Unpause 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) ret = dict(success=await _vm.unpause()) 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_vifs(cluster_id: str, vm_uuid: str): """ Show Instnace VIFs """ 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") vm: VM = VM.get_by_uuid(session=session, uuid=vm_uuid) if vm is not None: new_vifs = vm.get_VIFs() __vif_serialized = [] vif_serialized = __vif_serialized.append if new_vifs is not None: for vif in new_vifs: if vif is not None: vif_serialized(_vif_serialize(vif)) ret = dict(success=True, data=__vif_serialized) else: ret = dict(success=False) 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 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)
async def instance_set_description(cluster_id: str, vm_uuid: str, args: NameDescriptionArgs): """Set Instance (VM/Template) Description""" 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=_vm.set_description(args.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 vm_shutdown(cluster_id: str, vm_uuid: str): """Shutdown VM, can return false if the system does not support ACPI shutdown. System powerstate must be checked beforehand""" 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.shutdown()) 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_power(cluster_id: str, vm_uuid: str): """Get VM's power status, Can be "Paused", "Halted", "Running" """ 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=_vm.get_power_state()) 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_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)
async def vif_get_qos_by_uuid(cluster_id: str, vm_uuid: str): """ Set VIF QoS by VM """ 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") vm: VM = VM.get_by_uuid(session=session, uuid=vm_uuid) if vm is not None: vif = vm.get_VIF() if vif is not None: ret = dict( success=True, data=dict(type=vif.get_qos_type(), info=vif.get_qos_info()), ) else: ret = dict(success=False) 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 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 vm_guest(cluster_id: str, vm_uuid: str, url_after: str): """Get VM Guest Info""" 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) guest_metrics: GuestMetrics = vm.get_guest_metrics() guest_uuid = guest_metrics.get_uuid() session.xenapi.session.logout() return RedirectResponse(url=f"/v1/{cluster_id}/guest/{guest_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 verify_vm_uuid(cluster_id: str, vm_uuid: Optional[str] = None): if vm_uuid is None: return session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) try: vm = VM.get_by_uuid(session, vm_uuid) except Failure as xenapi_error: if xenapi_error.details[0] == "UUID_INVALID": raise HTTPException(status_code=404, detail=f"VM {vm_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 instance_set_bios_property(request: Request, cluster_id: str, vm_uuid: str): """Set Instance (VM/Template) BIOS Property by Name""" try: body = ujson.decode(await request.body()) 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=vm.set_bios_strings(body)) 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_get_platform(cluster_id: str, vm_uuid: str): """Get Instance (VM/Template) Platform""" 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_platform()) 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_create(cluster_id: str, vif_model: VIFCreateModel): """Create VIF with provided parameters""" try: params = vif_model.dict(exclude_unset=True) if params.get("other_config") is None: params["other_config"] = dict() if params.get("qos_algorithm_type") is None: params["qos_algorithm_type"] = "" params["qos_algorithm_params"] = dict() mtu = params.get("mtu") if mtu is None: params["mtu"] = 1500 mac = params.get("mac") if mac is None: import random sample_mac = "52:54:00:%02x:%02x:%02x" % ( random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), ) params["mac"] = sample_mac if params.get("vm") is None or params.get("network") is None: raise HTTPException( status_code=400, detail=dict(type="invalid_request", error="missing required parameter"), ) session = create_session(_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) vm = VM.get_by_uuid(session, params.get("vm")) network = Network.get_by_uuid(session, params.get("network")) if vm is None or network is None: raise HTTPException( status_code=404, detail=dict(type="not_found", error="requested resource could not be found"), ) params["network"] = network params["vm"] = vm vif: VIF = VIF.create(session, **params) 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 test(session): uuid = "2988e088-ff23-f543-334e-454352a6024a" vm = VM.get_by_uuid(session, uuid) print(await vm.get_record()) """