mirror of https://git.48k.eu/ogcli/
66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
# 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, 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, 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')
|
|
|
|
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)
|