forked from bitleak/lmstfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken-cli
executable file
·62 lines (54 loc) · 2.4 KB
/
token-cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
import argparse
import requests
# arguments
examples = """examples:
./token-cli 127.0.0.1:7778 --pools # list pools
./token-cli 127.0.0.1:7778 -c -p pool -n namespace -D description # create new namespace
./token-cli 127.0.0.1:7778 -d -p pool -n namespace # delete the namespace
./token-cli 127.0.0.1:7778 -l -p pool -n namespace # list tokens in namespace
"""
parser = argparse.ArgumentParser(
description="Manage the namepace and tokens",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=examples)
parser.add_argument("server", type=str, help="the server address(host:port)")
parser.add_argument("-D", "--description", type=str, help="the description")
parser.add_argument("-p", "--pool", type=str, help="the name of pool")
parser.add_argument("-n", "--namespace", type=str, help="the name of namespace")
parser.add_argument("-t", "--token", type=str, help="the token to be created or deleted")
parser.add_argument("-c", "--create", action="store_true", help="create the namespace")
parser.add_argument("-d", "--delete", action="store_true", help="delete the namespace")
parser.add_argument("-l", "--list", action="store_true", help="list tokens")
parser.add_argument("-P", "--pools", action="store_true", help="list pools")
args = parser.parse_args()
addr = "http://"+args.server
if args.pools:
r = requests.get(addr+"/pools")
print(r.text)
exit(0)
if args.namespace is None:
print("param 'namespace' is missing, please use -n to assign the namespace")
exit(1)
if args.create and args.description is None:
print("param 'description' is missing, please use -D to assign the description")
exit(1)
if args.delete and args.token is None:
print("param 'token' is missing, please use -t to assign the token")
exit(1)
if args.pool is None or args.pool == "default":
args.pool = ""
if args.list:
r = requests.get(addr+"/token/"+args.namespace+"?pool="+args.pool)
print(r.text)
elif args.create:
r = requests.post(addr+"/token/"+args.namespace+"?pool="+args.pool, data = {'description':args.description, 'token':args.token})
print(r.text)
elif args.delete:
r = requests.delete(addr+"/token/"+args.namespace+"/"+args.token+"?pool="+args.pool)
if r.status_code == 204:
print("ok")
else:
print(r.text)
else:
print("aha, I'm do nothing.")