diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-06-03 12:46:28 +0200 |
---|---|---|
committer | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-06-03 12:46:28 +0200 |
commit | 10d7b972cabaaffa099465c08f31dfe676ba5bbb (patch) | |
tree | 5fd5a1b2fa6d9f4e01db71589882566d7f4c85e1 /cli/objects | |
parent | e92e38bcca93f32228bb4cd1943e4e0652947326 (diff) |
cli: add list scopes --client-ip
Implement a --client-ip filter to ease the task of finding the
hierarchy associated to a client or list of clients.
Usage:
/ogcli list scopes --client-ip 10.141.10.23
/ogcli list scopes --client-ip 10.141.10.23 --client-ip 10.141.10.22
Diffstat (limited to 'cli/objects')
-rw-r--r-- | cli/objects/scopes.py | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/cli/objects/scopes.py b/cli/objects/scopes.py index 1900b1b..6a9b71d 100644 --- a/cli/objects/scopes.py +++ b/cli/objects/scopes.py @@ -8,11 +8,58 @@ import argparse from cli.utils import print_json +import json +def _find_client_path(json_data, client_ip, res): + if json_data['type'] == 'computer': + if json_data['ip'] == client_ip: + res.append(f'{json_data['type']}: {client_ip}') + return True + return False + + children = json_data['scope'] + for child in children: + found = _find_client_path(child, client_ip, res) + if found: + res.append(f'{json_data['type']}: {json_data['name']}') + return True + return False + + +def _get_client_path(json_data, client_ip): + res = [] + children = json_data['scope'] + for child in children: + _find_client_path(child, client_ip, res) + res.reverse() + return res class OgScope(): @staticmethod - def list_scopes(rest): + def list_scopes(rest, args): + parser = argparse.ArgumentParser(prog='ogcli list scopes') + parser.add_argument('--client-ip', + action='append', + default=[], + required=False, + help='Client(s) IP') + parsed_args = parser.parse_args(args) + + ips = set() + for ip in parsed_args.client_ip: + ips.add(ip) + r = rest.get('/scopes') - print_json(r.text) + + if not ips: + print_json(r.text) + return None + + json_data = json.loads(r.text) + for idx, client_ip in enumerate(ips): + if idx != 0: + print('\n') + path = _get_client_path(json_data, client_ip) + for i, item in enumerate(path): + print(' ' * i + item) |