From 86054a4cb805dc011ef7eff8a6df75dd3531b77d Mon Sep 17 00:00:00 2001 From: Javier Sánchez Parra Date: Thu, 4 Aug 2022 18:27:21 +0200 Subject: Initial support of ogServer requests "routing" This commit adds a helper function ("get_server_from_clients()") to get the adequate ogServer according to the targeted clients. It also makes "Client details" command use this function. --- ogcp/views.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/ogcp/views.py b/ogcp/views.py index 44c395a..ffa890d 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -197,13 +197,30 @@ def multi_request(method, uri, payload=None): else: raise Exception('Invalid method, use get or post') - response['server'] = server.name - response['status_code'] = r.status_code + response['server'] = server response['json'] = r.json() responses.append(response) return responses + +def get_server_from_clients(selected_clients): + server = None + + responses = multi_request('get', '/scopes') + for r in responses: + server_clients = [c['ip'] for c in parse_scopes_from_tree(r['json'], + 'computer')] + if all(client in server_clients for client in selected_clients): + server = r['server'] + break + + if not server: + raise Exception('Selected clients not found in any server') + + return server + + def get_scopes(ips=set()): list_scopes = [] responses = multi_request('get', '/scopes') @@ -732,8 +749,10 @@ def action_client_info(): if not validate_elements(ips, max_len=1): return redirect(url_for('commands')) + server = get_server_from_clients(list(ips)) + payload = {'client': list(ips)} - r = g.server.get('/client/info', payload) + r = server.get('/client/info', payload) db_client = r.json() form.name.data = db_client['name'] @@ -750,24 +769,24 @@ def action_client_info(): form.room.data = db_client['room'] form.boot.data = db_client['boot'] - r = g.server.get('/oglive/list') + r = server.get('/oglive/list') available_oglives = r.json()['oglive'] for oglive in available_oglives: choice = (oglive.get('directory'), oglive.get('directory')) form.livedir.choices.append(choice) - r = g.server.get('/mode') + r = server.get('/mode') available_modes = [(mode, mode) for mode in r.json()['modes']] form.boot.choices = list(available_modes) - r = g.server.get('/scopes') + r = server.get('/scopes') rooms = parse_scopes_from_tree(r.json(), 'room') rooms = [(room['id'], room['name']) for room in rooms] form.room.choices = list(rooms) form.create.render_kw = {"style": "visibility:hidden;"} - r = g.server.get('/images') + r = server.get('/images') images = r.json()['images'] ip = list(ips)[0] -- cgit v1.2.3-18-g5258