grpc: updated core-cli to use the wrapped client, removed json output for now

This commit is contained in:
Blake Harnden 2021-05-03 16:55:53 -07:00
parent 917c45e70b
commit 1cbe891dab

View file

@ -9,25 +9,24 @@ from argparse import (
) )
from functools import wraps from functools import wraps
from pathlib import Path from pathlib import Path
from typing import Any, Optional, Tuple from typing import Optional, Tuple
import grpc import grpc
import netaddr import netaddr
from google.protobuf.json_format import MessageToJson
from netaddr import EUI, AddrFormatError, IPNetwork from netaddr import EUI, AddrFormatError, IPNetwork
from core.api.grpc.client import CoreGrpcClient from core.api.grpc.clientw import CoreGrpcClient
from core.api.grpc.core_pb2 import ( from core.api.grpc.wrappers import (
Geo, Geo,
Interface, Interface,
Link,
LinkOptions, LinkOptions,
Node, Node,
NodeType, NodeType,
Position, Position,
SessionState,
) )
NODE_TYPES = [k for k, v in NodeType.Enum.items() if v != NodeType.PEER_TO_PEER] NODE_TYPES = [x for x in NodeType if x != NodeType.PEER_TO_PEER]
def coreclient(func): def coreclient(func):
@ -105,11 +104,11 @@ def file_type(value: str) -> str:
def get_current_session(core: CoreGrpcClient, session_id: Optional[int]) -> int: def get_current_session(core: CoreGrpcClient, session_id: Optional[int]) -> int:
if session_id: if session_id:
return session_id return session_id
response = core.get_sessions() sessions = core.get_sessions()
if not response.sessions: if not sessions:
print("no current session to interact with") print("no current session to interact with")
sys.exit(1) sys.exit(1)
return response.sessions[0].id return sessions[0].id
def create_iface(iface_id: int, mac: str, ip4_net: IPNetwork, ip6_net: IPNetwork) -> Interface: def create_iface(iface_id: int, mac: str, ip4_net: IPNetwork, ip6_net: IPNetwork) -> Interface:
@ -137,24 +136,16 @@ def print_iface(iface: Interface) -> None:
print(f"{iface.id:<3} | {iface.mac:<17} | {iface_ip4:<18} | {iface_ip6}") print(f"{iface.id:<3} | {iface.mac:<17} | {iface_ip4:<18} | {iface_ip6}")
def print_json(message: Any) -> None:
json = MessageToJson(message, preserving_proto_field_name=True)
print(json)
@coreclient @coreclient
def get_wlan_config(core: CoreGrpcClient, args: Namespace) -> None: def get_wlan_config(core: CoreGrpcClient, args: Namespace) -> None:
session_id = get_current_session(core, args.session) session_id = get_current_session(core, args.session)
response = core.get_wlan_config(session_id, args.node) config = core.get_wlan_config(session_id, args.node)
if args.json: size = 0
print_json(response) for option in config.values():
else: size = max(size, len(option.name))
size = 0 print(f"{'Name':<{size}.{size}} | Value")
for option in response.config.values(): for option in config.values():
size = max(size, len(option.name)) print(f"{option.name:<{size}.{size}} | {option.value}")
print(f"{'Name':<{size}.{size}} | Value")
for option in response.config.values():
print(f"{option.name:<{size}.{size}} | {option.value}")
@coreclient @coreclient
@ -171,110 +162,82 @@ def set_wlan_config(core: CoreGrpcClient, args: Namespace) -> None:
config["jitter"] = str(args.jitter) config["jitter"] = str(args.jitter)
if args.range: if args.range:
config["range"] = str(args.range) config["range"] = str(args.range)
response = core.set_wlan_config(session_id, args.node, config) result = core.set_wlan_config(session_id, args.node, config)
if args.json: print(f"set wlan config: {result}")
print_json(response)
else:
print(f"set wlan config: {response.result}")
@coreclient @coreclient
def open_xml(core: CoreGrpcClient, args: Namespace) -> None: def open_xml(core: CoreGrpcClient, args: Namespace) -> None:
response = core.open_xml(args.file, args.start) result, session_id = core.open_xml(args.file, args.start)
if args.json: print(f"opened xml: {result},{session_id}")
print_json(response)
else:
print(f"opened xml: {response.result}")
@coreclient @coreclient
def query_sessions(core: CoreGrpcClient, args: Namespace) -> None: def query_sessions(core: CoreGrpcClient, args: Namespace) -> None:
response = core.get_sessions() sessions = core.get_sessions()
if args.json: print("Session ID | Session State | Nodes")
print_json(response) for session in sessions:
else: print(f"{session.id:<10} | {session.state.name:<13} | {session.nodes}")
print("Session ID | Session State | Nodes")
for s in response.sessions:
state = SessionState.Enum.Name(s.state)
print(f"{s.id:<10} | {state:<13} | {s.nodes}")
@coreclient @coreclient
def query_session(core: CoreGrpcClient, args: Namespace) -> None: def query_session(core: CoreGrpcClient, args: Namespace) -> None:
response = core.get_session(args.id) session = core.get_session(args.id)
if args.json: print("Nodes")
print_json(response) print("Node ID | Node Name | Node Type")
else: for node in session.nodes.values():
print("Nodes") print(f"{node.id:<7} | {node.name:<9} | {node.type.name}")
print("Node ID | Node Name | Node Type") print("\nLinks")
names = {} for link in session.links:
for node in response.session.nodes: n1 = session.nodes[link.node1_id].name
names[node.id] = node.name n2 = session.nodes[link.node2_id].name
node_type = NodeType.Enum.Name(node.type) print(f"Node | ", end="")
print(f"{node.id:<7} | {node.name:<9} | {node_type}") print_iface_header()
print(f"{n1:<6} | ", end="")
print("\nLinks") if link.iface1:
for link in response.session.links: print_iface(link.iface1)
n1 = names[link.node1_id] else:
n2 = names[link.node2_id]
print(f"Node | ", end="")
print_iface_header()
print(f"{n1:<6} | ", end="")
if link.HasField("iface1"):
print_iface(link.iface1)
else:
print()
print(f"{n2:<6} | ", end="")
if link.HasField("iface2"):
print_iface(link.iface2)
else:
print()
print() print()
print(f"{n2:<6} | ", end="")
if link.iface2:
print_iface(link.iface2)
else:
print()
print()
@coreclient @coreclient
def query_node(core: CoreGrpcClient, args: Namespace) -> None: def query_node(core: CoreGrpcClient, args: Namespace) -> None:
names = {} session = core.get_session(args.id)
response = core.get_session(args.id) node, ifaces, _ = core.get_node(args.id, args.node)
for node in response.session.nodes: print("ID | Name | Type")
names[node.id] = node.name print(f"{node.id:<4} | {node.name:<7} | {node.type.name}")
print("Interfaces")
response = core.get_node(args.id, args.node) print("Connected To | ", end="")
if args.json: print_iface_header()
print_json(response) for iface in ifaces:
else: if iface.net_id == node.id:
node = response.node if iface.node_id:
node_type = NodeType.Enum.Name(node.type) name = session.nodes[iface.node_id].name
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: else:
name = names.get(iface.net_id, "") name = session.nodes[iface.net2_id].name
print(f"{name:<12} | ", end="") else:
print_iface(iface) net_node = session.nodes.get(iface.net_id)
name = net_node.name if net_node else ""
print(f"{name:<12} | ", end="")
print_iface(iface)
@coreclient @coreclient
def delete_session(core: CoreGrpcClient, args: Namespace) -> None: def delete_session(core: CoreGrpcClient, args: Namespace) -> None:
response = core.delete_session(args.id) result = core.delete_session(args.id)
if args.json: print(f"delete session({args.id}): {result}")
print_json(response)
else:
print(f"delete session({args.id}): {response.result}")
@coreclient @coreclient
def add_node(core: CoreGrpcClient, args: Namespace) -> None: def add_node(core: CoreGrpcClient, args: Namespace) -> None:
session_id = get_current_session(core, args.session) session_id = get_current_session(core, args.session)
node_type = NodeType.Enum.Value(args.type) node_type = NodeType[args.type]
pos = None pos = None
if args.pos: if args.pos:
x, y = args.pos x, y = args.pos
@ -294,11 +257,8 @@ def add_node(core: CoreGrpcClient, args: Namespace) -> None:
position=pos, position=pos,
geo=geo, geo=geo,
) )
response = core.add_node(session_id, node) node_id = core.add_node(session_id, node)
if args.json: print(f"created node: {node_id}")
print_json(response)
else:
print(f"created node: {response.node_id}")
@coreclient @coreclient
@ -312,21 +272,15 @@ def edit_node(core: CoreGrpcClient, args: Namespace) -> None:
if args.geo: if args.geo:
lon, lat, alt = args.geo lon, lat, alt = args.geo
geo = Geo(lon=lon, lat=lat, alt=alt) geo = Geo(lon=lon, lat=lat, alt=alt)
response = core.edit_node(session_id, args.id, pos, args.icon, geo) result = core.edit_node(session_id, args.id, pos, args.icon, geo)
if args.json: print(f"edit node: {result}")
print_json(response)
else:
print(f"edit node: {response.result}")
@coreclient @coreclient
def delete_node(core: CoreGrpcClient, args: Namespace) -> None: def delete_node(core: CoreGrpcClient, args: Namespace) -> None:
session_id = get_current_session(core, args.session) session_id = get_current_session(core, args.session)
response = core.delete_node(session_id, args.id) result = core.delete_node(session_id, args.id)
if args.json: print(f"deleted node: {result}")
print_json(response)
else:
print(f"deleted node: {response.result}")
@coreclient @coreclient
@ -346,11 +300,9 @@ def add_link(core: CoreGrpcClient, args: Namespace) -> None:
dup=args.duplicate, dup=args.duplicate,
unidirectional=args.uni, unidirectional=args.uni,
) )
response = core.add_link(session_id, args.node1, args.node2, iface1, iface2, options) link = Link(args.node1, args.node2, iface1=iface1, iface2=iface2, options=options)
if args.json: result, _, _ = core.add_link(session_id, link)
print_json(response) print(f"add link: {result}")
else:
print(f"add link: {response.result}")
@coreclient @coreclient
@ -364,23 +316,21 @@ def edit_link(core: CoreGrpcClient, args: Namespace) -> None:
dup=args.duplicate, dup=args.duplicate,
unidirectional=args.uni, unidirectional=args.uni,
) )
response = core.edit_link( iface1 = Interface(args.iface1)
session_id, args.node1, args.node2, options, args.iface1, args.iface2 iface2 = Interface(args.iface2)
) link = Link(args.node1, args.node2, iface1=iface1, iface2=iface2, options=options)
if args.json: result = core.edit_link(session_id, link)
print_json(response) print(f"edit link: {result}")
else:
print(f"edit link: {response.result}")
@coreclient @coreclient
def delete_link(core: CoreGrpcClient, args: Namespace) -> None: def delete_link(core: CoreGrpcClient, args: Namespace) -> None:
session_id = get_current_session(core, args.session) session_id = get_current_session(core, args.session)
response = core.delete_link(session_id, args.node1, args.node2, args.iface1, args.iface2) iface1 = Interface(args.iface1)
if args.json: iface2 = Interface(args.iface2)
print_json(response) link = Link(args.node1, args.node2, iface1=iface1, iface2=iface2)
else: result = core.delete_link(session_id, link)
print(f"delete link: {response.result}") print(f"delete link: {result}")
def setup_sessions_parser(parent: _SubParsersAction) -> None: def setup_sessions_parser(parent: _SubParsersAction) -> None:
@ -544,9 +494,6 @@ def setup_wlan_parser(parent: _SubParsersAction) -> None:
def main() -> None: def main() -> None:
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument(
"-js", "--json", action="store_true", help="print responses to terminal as json"
)
subparsers = parser.add_subparsers(help="supported commands") subparsers = parser.add_subparsers(help="supported commands")
subparsers.required = True subparsers.required = True
subparsers.dest = "command" subparsers.dest = "command"