grpc: added node_id and net2_id data to interface protos to allow querying a node to provide the node and networks an interface is associated with

This commit is contained in:
Blake Harnden 2020-06-30 12:34:20 -07:00
parent ab17cb1053
commit beaebcfa24
4 changed files with 42 additions and 23 deletions

View file

@ -453,32 +453,35 @@ def iface_to_data(iface: CoreInterface) -> InterfaceData:
)
def iface_to_proto(iface: CoreInterface) -> core_pb2.Interface:
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
"""
net_id = None
if iface.net:
net_id = iface.net.id
ip4 = None
ip4_mask = None
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()
if ip4_net:
ip4 = str(ip4_net.ip)
ip4_mask = ip4_net.prefixlen
ip6 = None
ip6_mask = None
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()
if ip6_net:
ip6 = str(ip6_net.ip)
ip6_mask = ip6_net.prefixlen
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=iface.node_id,
id=_id,
net_id=net_id,
net2_id=net2_id,
node_id=node_id,
name=iface.name,
mac=str(iface.mac),
mac=mac,
mtu=iface.mtu,
flow_id=iface.flow_id,
ip4=ip4,

View file

@ -688,7 +688,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
ifaces = []
for iface_id in node.ifaces:
iface = node.ifaces[iface_id]
iface_proto = grpcutils.iface_to_proto(iface)
iface_proto = grpcutils.iface_to_proto(request.node_id, iface)
ifaces.append(iface_proto)
node_proto = grpcutils.get_node_proto(session, node)
return core_pb2.GetNodeResponse(node=node_proto, ifaces=ifaces)
@ -880,9 +880,9 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
iface1_proto = None
iface2_proto = None
if node1_iface:
iface1_proto = grpcutils.iface_to_proto(node1_iface)
iface1_proto = grpcutils.iface_to_proto(node1_id, node1_iface)
if node2_iface:
iface2_proto = grpcutils.iface_to_proto(node2_iface)
iface2_proto = grpcutils.iface_to_proto(node2_id, node2_iface)
return core_pb2.AddLinkResponse(
result=True, iface1=iface1_proto, iface2=iface2_proto
)

View file

@ -742,6 +742,8 @@ message Interface {
int32 net_id = 8;
int32 flow_id = 9;
int32 mtu = 10;
int32 node_id = 11;
int32 net2_id = 12;
}
message SessionLocation {

View file

@ -109,9 +109,9 @@ def print_iface_header() -> None:
def print_iface(iface: Interface) -> None:
iface_ip4 = f"{iface.ip4}/{iface.ip4_mask}" if iface.ip4 else None
iface_ip6 = f"{iface.ip6}/{iface.ip6_mask}" if iface.ip6 else None
print(f"{iface.id:<3} | {iface.mac:<11} | {iface_ip4:<18} | {iface_ip6}")
iface_ip4 = f"{iface.ip4}/{iface.ip4_mask}" if iface.ip4 else ""
iface_ip6 = f"{iface.ip6}/{iface.ip6_mask}" if iface.ip6 else ""
print(f"{iface.id:<3} | {iface.mac:<17} | {iface_ip4:<18} | {iface_ip6}")
def query_sessions(args: Namespace) -> None:
@ -166,6 +166,11 @@ def query_session(args: Namespace) -> None:
def query_node(args: Namespace) -> None:
core = CoreGrpcClient()
with core.context_connect():
names = {}
response = core.get_session(args.id)
for node in response.session.nodes:
names[node.id] = node.name
response = core.get_node(args.id, args.node)
if args.json:
json = MessageToJson(response, preserving_proto_field_name=True)
@ -176,8 +181,17 @@ def query_node(args: Namespace) -> None:
print("ID | Name | Type")
print(f"{node.id:<4} | {node.name:<7} | {node_type}")
print("Interfaces")
print("Connected To | ", end="")
print_iface_header()
for iface in response.ifaces:
if iface.net_id == node.id:
if iface.node_id:
name = names[iface.node_id]
else:
name = names[iface.net2_id]
else:
name = names[iface.net_id]
print(f"{name:<12} | ", end="")
print_iface(iface)
@ -268,7 +282,7 @@ def add_link(args: Namespace) -> None:
json = MessageToJson(response, preserving_proto_field_name=True)
print(json)
else:
print(f"edit link: {response.result}")
print(f"add link: {response.result}")
def edit_link(args: Namespace) -> None: