85 lines
3.6 KiB
Python
Executable File
85 lines
3.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
# command line tool for RESTful HTTP service of osmo-lpmgd
|
|
#
|
|
# (C) 2023 by Harald Welte <laforge@osmocom.org>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import sys
|
|
import argparse
|
|
import requests
|
|
|
|
from osmocom.lpmgd.client import ApiClient
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("-H", "--host", help="host to connect to", default="localhost")
|
|
parser.add_argument("-p", "--port", help="HTTP port to connect to", type=int, default=9996)
|
|
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="count", default=0)
|
|
subp = parser.add_subparsers()
|
|
|
|
getst_p = subp.add_parser("get-status", help="Get current resource status")
|
|
getst_p.add_argument("-r", "--resource", help="name of resource", required=True)
|
|
getst_p.set_defaults(operation="get-status")
|
|
|
|
obtut_p = subp.add_parser("obtain-usage-token", help="Obtain usage token for resource")
|
|
obtut_p.add_argument("-n", "--user-name", help="User name", required=True)
|
|
obtut_p.add_argument("-u", "--usage", help="Usage (string describing reason for use)", required=True)
|
|
obtut_p.add_argument("-d", "--duration", help="Duration of usage in seconds)", type=int, required=True)
|
|
obtut_p.add_argument("-r", "--resource", help="name of resource", required=True)
|
|
obtut_p.set_defaults(operation="obtain-usage-token")
|
|
|
|
relut_p = subp.add_parser("release-usage-token")
|
|
relut_p.add_argument("-t", "--token", help="Usage token (previously obtained)", required=True)
|
|
relut_p.add_argument("-r", "--resource", help="name of resource", required=True)
|
|
relut_p.set_defaults(operation="release-usage-token")
|
|
|
|
shtok_p = subp.add_parser("show-usage-token")
|
|
shtok_p.add_argument("-r", "--resource", help="name of resource", required=True)
|
|
shtok_p.add_argument("-t", "--token", help="Usage token (previously obtained)", required=True)
|
|
shtok_p.set_defaults(operation="show-usage-token")
|
|
|
|
lsres_p = subp.add_parser("list-resources")
|
|
lsres_p.set_defaults(operation="list-resources")
|
|
|
|
lstok_p = subp.add_parser("list-tokens")
|
|
lstok_p.add_argument("-r", "--resource", help="name of resource", required=True)
|
|
lstok_p.set_defaults(operation="list-tokens")
|
|
|
|
args = parser.parse_args()
|
|
|
|
api = ApiClient(args.host, args.port)
|
|
|
|
if args.operation == "get-status":
|
|
rsp = api.get_resource_status(args.resource)
|
|
elif args.operation == "obtain-usage-token":
|
|
rsp = api.obtain_usage_token(args.resource, args.user_name, args.usage, args.duration)
|
|
elif args.operation == "release-usage-token":
|
|
rsp = api.release_usage_token(args.resource, args.token)
|
|
elif args.operation == "show-usage-token":
|
|
rsp = api.show_usage_token(args.resource, args.token)
|
|
elif args.operation == "list-resources":
|
|
rsp = api.list_resources()
|
|
elif args.operation == "list-tokens":
|
|
rsp = api.list_usage_tokens(args.resource)
|
|
else:
|
|
raise Exception("Unknown/Unsupported operation")
|
|
|
|
print(rsp)
|