Change in ...osmo-remsim[master]: add contrib/remsim-apitool.py to control the REST-interface of osmo-r...

roh gerrit-no-reply at lists.osmocom.org
Fri Jul 26 16:39:35 UTC 2019


roh has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-remsim/+/14968


Change subject: add contrib/remsim-apitool.py to control the REST-interface of osmo-remsim-server
......................................................................

add contrib/remsim-apitool.py to control the REST-interface of osmo-remsim-server

Change-Id: Ia01b0a89e21af18b898becd8f5440c32936772b7
---
M Makefile.am
A contrib/remsim-apitool.py
2 files changed, 114 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-remsim refs/changes/68/14968/1

diff --git a/Makefile.am b/Makefile.am
index 4843641..0ff9363 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -5,7 +5,7 @@
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libosmo-rspro.pc
 
-EXTRA_DIST = asn1 .version README.md
+EXTRA_DIST = asn1 .version README.md contrib/remsim-apitool.py
 
 pkgcofigdir = $(libdir)/pkgconfig
 
diff --git a/contrib/remsim-apitool.py b/contrib/remsim-apitool.py
new file mode 100755
index 0000000..8f8f29f
--- /dev/null
+++ b/contrib/remsim-apitool.py
@@ -0,0 +1,113 @@
+#!/usr/bin/env python3
+
+import sys
+import argparse
+import requests
+
+version = "0.1"
+
+#access rest
+def build_url(suffix):
+    BASE_PATH = "/api/backend/v1"
+    return ("http://" + server_host + ":" + server_port + BASE_PATH + suffix)
+
+def rest_get(suffix):
+    if verbose:
+        print("GET "+ build_url(suffix))
+    resp = requests.get(build_url(suffix))
+    if resp.ok:
+        try:
+            js = resp.json()
+            print("%s: %s" % (suffix, js))
+        except:
+            return
+    else:
+        return None
+
+def rest_post(suffix, js = None):
+    if verbose:
+        print("POST "+ build_url(suffix)+ str(js))
+    resp = requests.post(build_url(suffix), json=js)
+    if not resp.ok:
+        print("post failed")
+
+def rest_delete(suffix):
+    if verbose:
+        print("DELETE "+ build_url(suffix))
+    resp = requests.delete(build_url(suffix))
+    if not resp.ok:
+        print("delete failed")
+
+#rest calls
+def slotmap_create(bank_id, bank_slot, client_id, client_slot):
+    js = {
+        'bank': {'bankId': bank_id, 'slotNr': bank_slot},
+        'client': {'clientId': client_id, 'slotNr': client_slot},
+        }
+    return rest_post('/slotmaps', js)
+
+def slotmap_delete(bank_id, bank_slot):
+    slotmap_id = bank_id * 65536 + bank_slot
+    return rest_delete("/slotmaps/%u"%slotmap_id)
+
+def reset_global():
+    return rest_post('/global-reset')
+
+def main(argv):
+    global server_port, server_host, verbose
+
+    parser = argparse.ArgumentParser()
+    parser.add_argument("-H", "--host",    help="host to connect to",                        default="127.0.0.1")
+    parser.add_argument("-p", "--port",    help="port to connect to",                        default="9997")
+    parser.add_argument("-v", "--verbose", help="increase output verbosity", action="count", default=0)
+    group = parser.add_mutually_exclusive_group()
+    group.add_argument("-r", "--reset",    help="remove all mappings  (POST /global-reset)", action="store_true")
+    group.add_argument("-c", "--clients",  help="show clients         (GET /clients)",nargs='?',const="all",default=None)
+    group.add_argument("-b", "--banks",    help="show banks           (GET /banks)",nargs='?',const="all",default=None)
+    group.add_argument("-s", "--slotmaps", help="show slotmaps        (GET /slotmaps)",nargs='?',const="all",default=None)
+    group.add_argument("-m", "--map",      help="create new slotmapping   (POST /slotmaps)",nargs=4,metavar=('bank_id', 'bank_slot','client_id','client_slot'))
+    group.add_argument("-d", "--delete",   help="delete slotmapping       (DELETE /slotmaps/:id)", type=int, nargs=2, metavar=('bank_id','bank_slot'))
+    group.add_argument("-a", "--all",      help="show all (default if no argument given)", action="store_true")
+
+    args = parser.parse_args()
+    if args.verbose:
+        print("verbosity = ", args.verbose)
+
+    server_host = args.host
+    server_port = args.port
+    verbose = args.verbose
+
+    if args.reset:
+        reset_global()
+        return
+    if args.clients:
+        if args.clients == "all":
+            rest_get("/clients")
+        else:
+            rest_get("/clients/" + str(args.clients))
+        return
+    if args.banks:
+        if args.banks == "all":
+            rest_get("/banks")
+        else:
+            rest_get("/banks/" + str(args.banks))
+        return
+    if args.slotmaps:
+        if args.slotmaps == "all":
+            rest_get("/slotmaps")
+        else:
+            rest_get("/slotmaps/" + str(args.slotmaps))
+        return
+    if args.map:
+        slotmap_create(args.map[0],args.map[1],args.map[2],args.map[3])
+        return
+    if args.delete:
+        slotmap_delete(args.delete[0],args.delete[1])
+        return
+    rest_get("/clients")
+    rest_get("/banks")
+    rest_get("/slotmaps")
+
+if __name__ == "__main__":
+    main(sys.argv)
+

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-remsim/+/14968
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-remsim
Gerrit-Branch: master
Gerrit-Change-Id: Ia01b0a89e21af18b898becd8f5440c32936772b7
Gerrit-Change-Number: 14968
Gerrit-PatchSet: 1
Gerrit-Owner: roh <jsteiger at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190726/0f3ce8d8/attachment.html>


More information about the gerrit-log mailing list