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
|
# Copyright (C) 2020-2024 Soleta Networks <opengnsys@soleta.eu>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
import argparse
from cli.utils import print_json
import json
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, 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, client_name):
res = []
children = json_data['scope']
for child in children:
_find_client_path(child, client_ip, client_name, res)
res.reverse()
return res
class OgScope():
@staticmethod
def list_scopes(rest, args):
parser = argparse.ArgumentParser(prog='ogcli list scopes')
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()
for ip in parsed_args.client_ip:
ips.add(ip)
res = rest.get('/scopes')
json_data = json.loads(res.text)
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)
else:
print_json(res.text)
return 0
|