/
cmd_ul.py
174 lines (160 loc) · 5.22 KB
/
cmd_ul.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import click
from sqlalchemy import create_engine
from kamcli.ioutils import ioutils_dbres_print
from kamcli.ioutils import ioutils_formats_list
from kamcli.cli import pass_context
from kamcli.cli import parse_user_spec
from kamcli.iorpc import command_ctl
@click.group("ul", help="Manage user location records")
@pass_context
def cli(ctx):
pass
@cli.command("show", short_help="Show details for location records in memory")
@click.option(
"brief", "--brief", is_flag=True, help="Show brief format of the records."
)
@click.option(
"table",
"--table",
default="location",
help="Name of location table (default: location)",
)
@click.argument("userid", nargs=-1, metavar="[<userid>]")
@pass_context
def ul_show(ctx, brief, table, userid):
"""Show details for location records in memory
\b
Parameters:
[<userid>] - username, AoR or SIP URI for subscriber
- it can be a list of userids
- if not provided then all records are shown
"""
if not userid:
ctx.vlog("Showing all records")
if brief:
command_ctl(ctx, "ul.dump", ["brief"])
else:
command_ctl(ctx, "ul.dump", [])
else:
for u in userid:
udata = parse_user_spec(ctx, u)
ctx.vlog(
"Showing record for [%s@%s]",
udata["username"],
udata["domain"],
)
aor = udata["username"] + "@" + udata["domain"]
command_ctl(ctx, "ul.lookup", [table, aor])
@cli.command("add", short_help="Add location record")
@click.option(
"table",
"--table",
default="location",
help="Name of location table (default: location)",
)
@click.option(
"expires", "--expires", type=int, default=0, help="Expires value"
)
@click.option("qval", "--q", type=float, default=1.0, help="Q value")
@click.option("cpath", "--path", default="", help="Path value")
@click.option("flags", "--flags", type=int, default=0, help="Flags value")
@click.option(
"bflags", "--bflags", type=int, default=0, help="Branch flags value"
)
@click.option(
"methods", "--methods", type=int, default=4294967295, help="Methods value"
)
@click.argument("userid", nargs=1, metavar="<userid>")
@click.argument("curi", nargs=1, metavar="<contact-uri>")
@pass_context
def ul_add(
ctx, table, expires, qval, cpath, flags, bflags, methods, userid, curi
):
"""Add location record
\b
Parameters:
<userid> - username, AoR or SIP URI for subscriber
<contact-uri> - contact SIP URI
"""
udata = parse_user_spec(ctx, userid)
ctx.vlog("Adding record for [%s@%s]", udata["username"], udata["domain"])
aor = udata["username"] + "@" + udata["domain"]
command_ctl(
ctx,
"ul.add",
[table, aor, curi, expires, qval, cpath, flags, bflags, methods],
)
@cli.command("rm", short_help="Delete location records")
@click.option(
"table",
"--table",
default="location",
help="Name of location table (default: location)",
)
@click.argument("userid", nargs=1, metavar="<userid>")
@click.argument("curi", nargs=-1, metavar="[<contact-uri>]")
@pass_context
def ul_rm(ctx, table, userid, curi):
"""Show details for location records in memory
\b
Parameters:
<userid> - username, AoR or SIP URI for subscriber
[<contact-uri>] - contact SIP URI
- optional, it can be a list of URIs
"""
udata = parse_user_spec(ctx, userid)
ctx.vlog("Showing record for [%s@%s]", udata["username"], udata["domain"])
aor = udata["username"] + "@" + udata["domain"]
if curi:
for c in curi:
command_ctl(ctx, "ul.rm", [table, aor, c])
else:
command_ctl(ctx, "ul.rm", [table, aor])
@cli.command(
"showdb", short_help="Show details for location records in database"
)
@click.option(
"oformat",
"--output-format",
"-F",
type=click.Choice(ioutils_formats_list),
default=None,
help="Format the output",
)
@click.option(
"ostyle",
"--output-style",
"-S",
default=None,
help="Style of the output (tabulate table format)",
)
@click.argument("userid", nargs=-1, metavar="[<userid>]")
@pass_context
def ul_showdb(ctx, oformat, ostyle, userid):
"""Show details for location records in database
\b
Parameters:
[<userid>] - username, AoR or SIP URI for subscriber
- it can be a list of userids
- if not provided then all records are shown
"""
if not userid:
ctx.vlog("Showing all records")
e = create_engine(ctx.gconfig.get("db", "rwurl"))
res = e.execute("select * from location")
ioutils_dbres_print(ctx, oformat, ostyle, res)
else:
for u in userid:
udata = parse_user_spec(ctx, u)
ctx.vlog(
"Showing records for [%s@%s]",
udata["username"],
udata["domain"],
)
e = create_engine(ctx.gconfig.get("db", "rwurl"))
res = e.execute(
"select * from location where username=%s and domain=%s",
udata["username"],
udata["domain"],
)
ioutils_dbres_print(ctx, oformat, ostyle, res)