# debug-only utilities that are imported as bash functions if the shell is interactive
[[ "$-" == *i* ]] \
&& ag-devices-count() {
# USAGE:
# ag-devices-count
python3 -- - "$@" <<'EOF'
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
n=0
with grpc.insecure_channel("ag-devices:9047") as c:
    s = ag.DevicesStub(c)
    for d in s.Range(ag.DevicesRange.Request()):
        n+=1
print(n)
EOF
} \
&& ag-devices-range() {
# USAGE:
# ag-devices-range
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel("ag-devices:9047") as c:
    s = ag.DevicesStub(c)
    for d in s.Range(ag.DevicesRange.Request()):
        json.dump(jsonpb.MessageToDict(d, including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
        sys.stdout.write("\n")
EOF
} \
&& ag-devices-load() {
# USAGE:
# ag-devices-load ID
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel("ag-devices:9047") as c:
    json.dump(jsonpb.MessageToDict(ag.DevicesStub(c).Load(ag.DevicesLoad.Request(id=sys.argv[1])), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-devices-delete() {
# USAGE:
# ag-devices-delete ID
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel("ag-devices:9047") as c:
    json.dump(jsonpb.MessageToDict(ag.DevicesStub(c).Delete(ag.DevicesDelete.Request(id=sys.argv[1])), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-devices-publish() {
# USAGE:
# ag-devices-publish JSON_MESSAGE...
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel("ag-devices:9047") as c:
    s = ag.DevicesStub(c)
    for argv in sys.argv[1:]:
        json.dump(jsonpb.MessageToDict(s.Publish(jsonpb.Parse(argv, ag.DevicesPublish.Request())), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
        sys.stdout.write("\n")
EOF
} \
&& ag-devices-status() {
# USAGE:
# ag-devices-status
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel("ag-devices:9047") as c:
    json.dump(jsonpb.MessageToDict(ag.DevicesStub(c).Status(ag.DevicesStatus.Request()), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-devices-delete-job() {
# USAGE:
# ag-devices-delete-job
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops.automation_gateway import job
with grpc.insecure_channel("ag-devices:9047") as c:
    json.dump(jsonpb.MessageToDict(job.NmapScanStub(c).DeleteJob(job.NmapScanDeleteJobRequest()), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-devices-get-job() {
# USAGE:
# ag-devices-get-job
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops.automation_gateway import job
with grpc.insecure_channel("ag-devices:9047") as c:
    json.dump(jsonpb.MessageToDict(job.NmapScanStub(c).GetJob(job.NmapScanGetJobRequest()), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-remote-nmap-scan() {
# USAGE:
# ag-remote-nmap-scan NODE_IP TARGETS...
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel(sys.argv[1]+":9045") as c:
    s = ag.RemoteStub(c)
    json.dump(jsonpb.MessageToDict(s.NmapScan(ag.NmapScan.Request(targets=sys.argv[2:])), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-remote-guacamole-session-create() {
# USAGE:
# ag-remote-guacamole-session-create NODE_IP JSON_MESSAGE
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel(sys.argv[1]+":9045") as c:
    s = ag.RemoteStub(c)
    json.dump(jsonpb.MessageToDict(s.CreateGuacamoleSession(jsonpb.Parse(sys.argv[2], ag.CreateGuacamoleSessionRequest())), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-remote-guacamole-session-get() {
# USAGE:
# ag-remote-guacamole-session-get NODE_IP NAME
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel(sys.argv[1]+":9045") as c:
    s = ag.RemoteStub(c)
    json.dump(jsonpb.MessageToDict(s.GetGuacamoleSession(ag.GetGuacamoleSessionRequest(name=sys.argv[2])), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-remote-guacamole-session-update() {
# USAGE:
# ag-remote-guacamole-session-update NODE_IP JSON_MESSAGE
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel(sys.argv[1]+":9045") as c:
    s = ag.RemoteStub(c)
    json.dump(jsonpb.MessageToDict(s.UpdateGuacamoleSession(jsonpb.Parse(sys.argv[2], ag.UpdateGuacamoleSessionRequest())), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-remote-guacamole-session-delete() {
# USAGE:
# ag-remote-guacamole-session-delete NODE_IP NAME
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel(sys.argv[1]+":9045") as c:
    s = ag.RemoteStub(c)
    json.dump(jsonpb.MessageToDict(s.DeleteGuacamoleSession(ag.DeleteGuacamoleSessionRequest(name=sys.argv[2])), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-remote-guacamole-session-list() {
# USAGE:
# ag-remote-guacamole-session-list NODE_IP
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import (
    json_format as jsonpb,
    empty_pb2,
)
from netops import automation_gateway as ag
with grpc.insecure_channel(sys.argv[1]+":9045") as c:
    s = ag.RemoteStub(c)
    for v in s.ListGuacamoleSessions(empty_pb2.Empty()).guacamole_sessions:
        json.dump(jsonpb.MessageToDict(v, including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
        sys.stdout.write("\n")
EOF
} \
&& ag-auth-create() {
# USAGE:
# ag-auth-create NODE SESSION
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel("ag-auth:9046") as c:
    s = ag.AuthStub(c)
    json.dump(jsonpb.MessageToDict(s.CreateToken(ag.CreateTokenRequest(session=sys.argv[2], node=sys.argv[1])), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-auth-validate() {
# USAGE:
# ag-auth-validate TOKEN
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel("ag-auth:9046") as c:
    s = ag.AuthStub(c)
    json.dump(jsonpb.MessageToDict(s.ValidateToken(ag.ValidateTokenRequest(token=sys.argv[1])), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ag-auth-refresh() {
# USAGE:
# ag-auth-refresh NODE SESSION
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops import automation_gateway as ag
with grpc.insecure_channel("ag-auth:9046") as c:
    s = ag.AuthStub(c)
    json.dump(jsonpb.MessageToDict(s.RefreshToken(ag.RefreshTokenRequest(token=sys.argv[1], session=sys.argv[2])), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ip-access-remote-system-sessions() {
# USAGE:
# ip-access-remote-system-sessions NODE_IP
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops.ip_access import remote
with grpc.insecure_channel(f"{sys.argv[1]}:8980") as c:
    for v in remote.SystemStub(c).Sessions(remote.SystemSessionsRequest()):
        json.dump(jsonpb.MessageToDict(v, including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
        sys.stdout.write("\n")
EOF
} \
&& ip-access-remote-sessions-disconnect() {
# USAGE:
# ip-access-remote-sessions-disconnect NODE_IP SESSION_ID
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops.ip_access import remote
with grpc.insecure_channel(f"{sys.argv[1]}:8980") as c:
    json.dump(jsonpb.MessageToDict(remote.SessionsStub(c).Disconnect(remote.SessionsDisconnectRequest(session_id=sys.argv[2])), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
&& ip-access-remote-sessions-connect() {
# USAGE:
# ip-access-remote-sessions-connect NODE_IP SESSION_ID
python3 -- - "$@" <<'EOF'
import sys
import json
import grpc
from google.protobuf import json_format as jsonpb
from netops.ip_access import remote
with grpc.insecure_channel(f"{sys.argv[1]}:8980") as c:
    json.dump(jsonpb.MessageToDict(remote.SessionsStub(c).Connect(remote.SessionsConnectRequest(session_id=sys.argv[2])), including_default_value_fields=True, preserving_proto_field_name=True), sys.stdout, separators=(',', ':'))
    sys.stdout.write("\n")
EOF
} \
|| true
