summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-09-12 13:26:42 +0200
committerAlejandro Sirgo Rica <asirgo@soleta.eu>2024-09-12 13:26:42 +0200
commit59f2f501aa6dfc2f238f670625c11d9fd3d49cef (patch)
tree34ac86ab3f09335d749967f1423a3b41158409c2
parent8f5b709212aa1ea28e85a0367747976cf81e0f3a (diff)
scopes: add scope filtering by namev0.3.3-13
Implement the --name flag in scope list to filter by client name. Example usage: ogcli list scope --name test
-rw-r--r--cli/objects/scopes.py47
1 files changed, 30 insertions, 17 deletions
diff --git a/cli/objects/scopes.py b/cli/objects/scopes.py
index 7f70cdd..548b93c 100644
--- a/cli/objects/scopes.py
+++ b/cli/objects/scopes.py
@@ -10,27 +10,30 @@ import argparse
from cli.utils import print_json
import json
-def _find_client_path(json_data, client_ip, res):
+def _find_client_path(json_data, client_ip, client_name, res):
if json_data['type'] == 'computer':
if json_data['ip'] == client_ip:
res.append(f'{json_data["type"]}: {client_ip}')
return True
+ elif json_data['name'] == client_name:
+ res.append(f'{json_data["type"]}: {client_name}')
+ return True
return False
children = json_data['scope']
for child in children:
- found = _find_client_path(child, client_ip, res)
+ found = _find_client_path(child, client_ip, client_name, res)
if found:
res.append(f'{json_data["type"]}: {json_data["name"]}')
return True
return False
-def _get_client_path(json_data, client_ip):
+def _get_client_path(json_data, client_ip, client_name):
res = []
children = json_data['scope']
for child in children:
- _find_client_path(child, client_ip, res)
+ _find_client_path(child, client_ip, client_name, res)
res.reverse()
return res
@@ -39,11 +42,15 @@ class OgScope():
@staticmethod
def list_scopes(rest, args):
parser = argparse.ArgumentParser(prog='ogcli list scope')
- parser.add_argument('--client-ip',
- action='append',
- default=[],
- required=False,
- help='Client(s) IP')
+ group = parser.add_mutually_exclusive_group(required=False)
+ group.add_argument('--client-ip',
+ action='append',
+ default=[],
+ help='Client(s) IP')
+ group.add_argument('--name',
+ nargs='?',
+ help='Name of the client')
+
parsed_args = parser.parse_args(args)
ips = set()
@@ -51,15 +58,21 @@ class OgScope():
ips.add(ip)
r = rest.get('/scopes')
+ json_data = json.loads(r.text)
- if not ips:
+ if parsed_args.name:
+ path = _get_client_path(json_data, None, parsed_args.name)
+ for i, item in enumerate(path):
+ print(' ' * i + item)
+ elif parsed_args.client_ip:
+ for idx, client_ip in enumerate(parsed_args.client_ip):
+ if idx != 0:
+ print('\n')
+ path = _get_client_path(json_data, client_ip, None)
+ for i, item in enumerate(path):
+ print(' ' * i + item)
+ return None
+ else:
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)