Skip to content

Commit ef736a0

Browse files
committed
tcf/get: move command implementation to new API
Shall be more scalable for big deployments
1 parent 2dc437b commit ef736a0

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

tcf

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,6 @@ servers: {len(tcfl.ttb_client.rest_target_brokers)}""")
101101

102102

103103

104-
def _target_get(args):
105-
# pure target get w/o going through the cache
106-
107-
if args.projection:
108-
data = { 'projections': json.dumps(args.projection) }
109-
else:
110-
data = None
111-
rtb, rt = ttb_client._rest_target_find_by_id(args.target)
112-
r = rtb.send_request("GET", "targets/" + rt['id'], data = data,
113-
raw = True)
114-
# Keep the order -- even if json spec doesn't contemplate it, we
115-
# use it so the client can tell (if they want) the order in which
116-
# for example, power rail components are defined in interfaces.power
117-
rt = json.loads(r.text, object_pairs_hook = collections.OrderedDict)
118-
print(json.dumps(rt, skipkeys = True, indent = 4))
119-
120104
def _target_patch(args):
121105
# set data
122106
data = collections.OrderedDict() # respect user's order
@@ -448,18 +432,7 @@ if __name__ == "__main__":
448432

449433
# Semi advanced commands
450434

451-
ap = arg_subparsers.add_parser(
452-
"get", help = "Return target information straight from the "
453-
"server formated as JSON (unlike 'list', which will add some "
454-
"client fields)")
455-
ap.add_argument(
456-
"-p", "--projection", action = "append",
457-
help = "List of fields to return (*? [CHARS] or [!CHARS] supported)"
458-
" as per python's fnmatch module")
459-
ap.add_argument("target", metavar = "TARGET", action = "store",
460-
default = None, help = "Target's name")
461-
ap.set_defaults(func = _target_get)
462-
435+
tcfl.ui_cli_targets._cmdline_setup_advanced(arg_subparsers)
463436

464437
ap = arg_subparsers.add_parser("property-set",
465438
help = "Set a target's property")

tcfl/ui_cli_targets.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@
2020
2121
"""
2222

23+
import argparse
24+
import json
2325
import logging
2426
import math
2527
import os
2628
import sys
2729

2830
import commonl
31+
import tcfl.ui_cli
2932

3033
logger = logging.getLogger("ui_cli_testcases")
3134

3235

33-
3436
def _cmdline_targets_init(args):
3537
# initialize reporting based on what the commandline wants
3638

@@ -51,6 +53,24 @@ def _cmdline_targets_init(args):
5153

5254

5355

56+
def _cmdline_target_get(cli_args: argparse.Namespace):
57+
58+
def _target_get(target, _cli_args):
59+
projections = cli_args.project
60+
server = tcfl.server_c.servers[target.rt['server']]
61+
rt = server.targets_get(target_id = target.id,
62+
projections = cli_args.project)
63+
# rt is a list of dicts keyed by fullid, we care only for the first
64+
json.dump(rt[0][target.fullid], sys.stdout, indent = 4)
65+
66+
return tcfl.ui_cli.run_fn_on_each_targetspec(
67+
# note we scan ignoring --projections, since that we'll use
68+
# later; we want to identify the target to get as soon as
69+
# possible and then in _target_get() we do the stuff
70+
_target_get, cli_args, only_one = True)
71+
72+
73+
5474
def _targets_list_v0_table(l):
5575

5676
if not l: # avoid divide by zero errors
@@ -211,3 +231,19 @@ def _cmdline_setup(arg_subparsers):
211231
help = "consider only the given fields "
212232
"(default depends on verbosity")
213233
ap.set_defaults(func = _cmdline_ls)
234+
235+
236+
237+
def _cmdline_setup_advanced(arg_subparsers):
238+
ap = arg_subparsers.add_parser(
239+
"get", help = "Return target information straight from the "
240+
"server formated as JSON (unlike 'ls', which will add some "
241+
"client fields)")
242+
tcfl.ui_cli.args_verbosity_add(ap)
243+
tcfl.ui_cli.args_targetspec_add(ap, targetspec_n = 1)
244+
ap.add_argument(
245+
"-p", "--project", "--projection", metavar = "FIELD",
246+
action = "append", type = str,
247+
help = "consider only the given fields "
248+
"(default depends on verbosity")
249+
ap.set_defaults(func = _cmdline_target_get)

0 commit comments

Comments
 (0)