def iface_to_data(iface: CoreInterface) -> InterfaceData: ip4 = iface.get_ip4() ip4_addr = str(ip4.ip) if ip4 else None ip4_mask = ip4.prefixlen if ip4 else None ip6 = iface.get_ip6() ip6_addr = str(ip6.ip) if ip6 else None ip6_mask = ip6.prefixlen if ip6 else None return InterfaceData( id=iface.node_id, name=iface.name, mac=str(iface.mac), ip4=ip4_addr, ip4_mask=ip4_mask, ip6=ip6_addr, ip6_mask=ip6_mask, )
def iface_to_proto(node_id: int, iface: CoreInterface) -> core_pb2.Interface: """ Convenience for converting a core interface to the protobuf representation. :param node_id: id of node to convert interface for :param iface: interface to convert :return: interface proto """ if iface.node and iface.node.id == node_id: _id = iface.node_id else: _id = iface.net_id net_id = iface.net.id if iface.net else None node_id = iface.node.id if iface.node else None net2_id = iface.othernet.id if iface.othernet else None ip4_net = iface.get_ip4() ip4 = str(ip4_net.ip) if ip4_net else None ip4_mask = ip4_net.prefixlen if ip4_net else None ip6_net = iface.get_ip6() ip6 = str(ip6_net.ip) if ip6_net else None ip6_mask = ip6_net.prefixlen if ip6_net else None mac = str(iface.mac) if iface.mac else None return core_pb2.Interface( id=_id, net_id=net_id, net2_id=net2_id, node_id=node_id, name=iface.name, mac=mac, mtu=iface.mtu, flow_id=iface.flow_id, ip4=ip4, ip4_mask=ip4_mask, ip6=ip6, ip6_mask=ip6_mask, )