core-cli: add function for printing protobuf responses as json
This commit is contained in:
parent
beaebcfa24
commit
4a0fdf3307
1 changed files with 15 additions and 19 deletions
|
@ -7,7 +7,7 @@ from argparse import (
|
||||||
Namespace,
|
Namespace,
|
||||||
_SubParsersAction,
|
_SubParsersAction,
|
||||||
)
|
)
|
||||||
from typing import Tuple
|
from typing import Any, Tuple
|
||||||
|
|
||||||
import netaddr
|
import netaddr
|
||||||
from google.protobuf.json_format import MessageToJson
|
from google.protobuf.json_format import MessageToJson
|
||||||
|
@ -114,13 +114,17 @@ 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)
|
||||||
|
|
||||||
|
|
||||||
def query_sessions(args: Namespace) -> None:
|
def query_sessions(args: Namespace) -> None:
|
||||||
core = CoreGrpcClient()
|
core = CoreGrpcClient()
|
||||||
with core.context_connect():
|
with core.context_connect():
|
||||||
response = core.get_sessions()
|
response = core.get_sessions()
|
||||||
if args.json:
|
if args.json:
|
||||||
json = MessageToJson(response, preserving_proto_field_name=True)
|
print_json(response)
|
||||||
print(json)
|
|
||||||
else:
|
else:
|
||||||
print("Session ID | Session State | Nodes")
|
print("Session ID | Session State | Nodes")
|
||||||
for s in response.sessions:
|
for s in response.sessions:
|
||||||
|
@ -133,8 +137,7 @@ def query_session(args: Namespace) -> None:
|
||||||
with core.context_connect():
|
with core.context_connect():
|
||||||
response = core.get_session(args.id)
|
response = core.get_session(args.id)
|
||||||
if args.json:
|
if args.json:
|
||||||
json = MessageToJson(response, preserving_proto_field_name=True)
|
print_json(response)
|
||||||
print(json)
|
|
||||||
else:
|
else:
|
||||||
print("Nodes")
|
print("Nodes")
|
||||||
print("Node ID | Node Name | Node Type")
|
print("Node ID | Node Name | Node Type")
|
||||||
|
@ -173,8 +176,7 @@ def query_node(args: Namespace) -> None:
|
||||||
|
|
||||||
response = core.get_node(args.id, args.node)
|
response = core.get_node(args.id, args.node)
|
||||||
if args.json:
|
if args.json:
|
||||||
json = MessageToJson(response, preserving_proto_field_name=True)
|
print_json(response)
|
||||||
print(json)
|
|
||||||
else:
|
else:
|
||||||
node = response.node
|
node = response.node
|
||||||
node_type = NodeType.Enum.Name(node.type)
|
node_type = NodeType.Enum.Name(node.type)
|
||||||
|
@ -221,8 +223,7 @@ def add_node(args: Namespace) -> None:
|
||||||
)
|
)
|
||||||
response = core.add_node(session_id, node)
|
response = core.add_node(session_id, node)
|
||||||
if args.json:
|
if args.json:
|
||||||
json = MessageToJson(response, preserving_proto_field_name=True)
|
print_json(response)
|
||||||
print(json)
|
|
||||||
else:
|
else:
|
||||||
print(f"created node: {response.node_id}")
|
print(f"created node: {response.node_id}")
|
||||||
|
|
||||||
|
@ -241,8 +242,7 @@ def edit_node(args: Namespace) -> None:
|
||||||
with core.context_connect():
|
with core.context_connect():
|
||||||
response = core.edit_node(session_id, args.id, pos, args.icon, geo)
|
response = core.edit_node(session_id, args.id, pos, args.icon, geo)
|
||||||
if args.json:
|
if args.json:
|
||||||
json = MessageToJson(response, preserving_proto_field_name=True)
|
print_json(response)
|
||||||
print(json)
|
|
||||||
else:
|
else:
|
||||||
print(f"edit node: {response.result}")
|
print(f"edit node: {response.result}")
|
||||||
|
|
||||||
|
@ -253,8 +253,7 @@ def delete_node(args: Namespace) -> None:
|
||||||
with core.context_connect():
|
with core.context_connect():
|
||||||
response = core.delete_node(session_id, args.id)
|
response = core.delete_node(session_id, args.id)
|
||||||
if args.json:
|
if args.json:
|
||||||
json = MessageToJson(response, preserving_proto_field_name=True)
|
print_json(response)
|
||||||
print(json)
|
|
||||||
else:
|
else:
|
||||||
print(f"deleted node: {response.result}")
|
print(f"deleted node: {response.result}")
|
||||||
|
|
||||||
|
@ -279,8 +278,7 @@ def add_link(args: Namespace) -> None:
|
||||||
with core.context_connect():
|
with core.context_connect():
|
||||||
response = core.add_link(session_id, args.node1, args.node2, iface1, iface2, options)
|
response = core.add_link(session_id, args.node1, args.node2, iface1, iface2, options)
|
||||||
if args.json:
|
if args.json:
|
||||||
json = MessageToJson(response, preserving_proto_field_name=True)
|
print_json(response)
|
||||||
print(json)
|
|
||||||
else:
|
else:
|
||||||
print(f"add link: {response.result}")
|
print(f"add link: {response.result}")
|
||||||
|
|
||||||
|
@ -301,8 +299,7 @@ def edit_link(args: Namespace) -> None:
|
||||||
session_id, args.node1, args.node2, options, args.iface1, args.iface2
|
session_id, args.node1, args.node2, options, args.iface1, args.iface2
|
||||||
)
|
)
|
||||||
if args.json:
|
if args.json:
|
||||||
json = MessageToJson(response, preserving_proto_field_name=True)
|
print_json(response)
|
||||||
print(json)
|
|
||||||
else:
|
else:
|
||||||
print(f"edit link: {response.result}")
|
print(f"edit link: {response.result}")
|
||||||
|
|
||||||
|
@ -313,8 +310,7 @@ def delete_link(args: Namespace) -> None:
|
||||||
with core.context_connect():
|
with core.context_connect():
|
||||||
response = core.delete_link(session_id, args.node1, args.node2, args.iface1, args.iface2)
|
response = core.delete_link(session_id, args.node1, args.node2, args.iface1, args.iface2)
|
||||||
if args.json:
|
if args.json:
|
||||||
json = MessageToJson(response, preserving_proto_field_name=True)
|
print_json(response)
|
||||||
print(json)
|
|
||||||
else:
|
else:
|
||||||
print(f"delete link: {response.result}")
|
print(f"delete link: {response.result}")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue