def get_VIFs(self): from XenGarden.VIF import VIF vifs = self.session.xenapi.VM.get_VIFs(self.vm) vif_list = [] for vif in vifs: try: thisVIF = VIF(self.session, vif) thisVIF.get_uuid() vif_list.append(thisVIF) except: pass return vif_list
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 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 vif_get_qos_speed_by_uuid(cluster_id: str, vif_uuid: str): """ Set VIF QoS Type 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" ) vif: VIF = VIF.get_by_uuid(session=session, uuid=vif_uuid) if vif is not None: ret = dict(success=True, data=vif.get_qos_info()["kbps"]) 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 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 vif_list(cluster_id: str): """ Get All from Storage Repos """ 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" ) vifs = VIF.get_all(session=session) __santilized_vifs = [] santilized_vifs = __santilized_vifs.append for vif in vifs: santilized_vifs(serialize(vif)) ret = dict(success=True, data=__santilized_vifs) 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 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 vif_list(cluster_id: str): """Get All from Storage Repos""" try: session = create_session(_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()) vifs = VIF.get_all(session=session) __santilized_vifs = await asyncio.gather( *[serialize(vif) for vif in vifs]) ret = dict(success=True, data=__santilized_vifs) 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_set_qos_speed_by_uuid(cluster_id: str, vif_uuid: str, speed: str): """ Set VIF QoS Speed 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" ) vif: VIF = VIF.get_by_uuid(session=session, uuid=vif_uuid) speedNum = speed try: speedNum = int(speed) except ValueError: return dict(success=False) if speedNum <= 0: a = vif.set_qos_type("") b = vif.set_qos_info({}) ret = dict(success=a and b) else: if vif is not None: if vif.get_qos_type() != "ratelimit": vif.set_qos_type("ratelimit") ret = dict(success=vif.set_qos_info(dict(kbps=speed))) 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 vif_get_network(cluster_id: str, vif_uuid: str, url_after: str = ""): """Redirect To VIF's Network""" 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 None: session.xenapi.session.logout() raise HTTPException( status_code=404, detail=f"VIF {vif_uuid} does not exist" ) network = vif.get_network() if network is None: session.xenapi.session.logout() raise HTTPException( status_code=404, detail=f"VIF {vif_uuid} does not exist on ANY Network" ) network_uuid = network.get_uuid() session.xenapi.session.logout() return RedirectResponse(f"/v1/{cluster_id}/network/{network_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_ipv4_by_uuid(cluster_id: str, vif_uuid: str, data: IPModel): """Set VIF IPv4 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 = vif.config_ipv4("Static", data.address, "" if data.gateway is None else data.gateway) 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 verify_vif_uuid(cluster_id: str, vif_uuid: Optional[str] = None): if vif_uuid is None: return session = create_session(cluster_id, get_xen_clusters=Settings.get_xen_clusters()) try: vif = VIF.get_by_uuid(session, vif_uuid) except Failure as xenapi_error: if xenapi_error.details[0] == "UUID_INVALID": raise HTTPException(status_code=404, detail=f"VIF {vif_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 vif_clear_lock_by_uuid(cluster_id: str, vif_uuid: str, data: ModeModel): """Clear VIF Lock Data to Default 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_locking_mode("network_default") 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 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)
def serialize(vif: VIF): vm = vif.get_vm() if vm is not None: vm = _vm_serialize(vm) return dict( attached=vif.get_attached(), vm=vm, uuid=vif.get_uuid(), mac=vif.get_mac(), mtu=vif.get_mtu(), qos=dict( type=vif.get_qos_type(), info=vif.get_qos_info(), supported=vif.supported_qos_types(), ), ipv4=dict(address=vif.get_address_v4(), gateway=vif.get_gateway_v4()), ipv6=dict(address=vif.get_address_v6(), gateway=vif.get_gateway_v6()), )
async def serialize(vif: VIF): from API.v1.Network.serialize import serialize as _network_serialize from API.v1.VM.serialize import serialize as _vm_serialize vm = vif.get_vm() if vm is not None: vm = await _vm_serialize(vm) network = vif.get_network() if network is not None: network = await _network_serialize(network) return dict( attached=vif.get_attached(), vm=vm, network=network, uuid=vif.get_uuid(), mac=vif.get_mac(), mtu=vif.get_mtu(), qos=dict( type=vif.get_qos_type(), info=vif.get_qos_info(), supported=vif.supported_qos_types(), ), device=vif.get_device(), locking_mode=vif.get_locking_mode(), ipv4=dict(address=vif.get_address_v4(), gateway=vif.get_gateway_v4()), ipv6=dict(address=vif.get_address_v6(), gateway=vif.get_gateway_v6()), )