|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# |
| 4 | +# Copyright (C) 2025 Nethesis S.r.l. |
| 5 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | +# |
| 7 | + |
| 8 | +import json |
| 9 | +import sys |
| 10 | +import os |
| 11 | + |
| 12 | +import agent |
| 13 | + |
| 14 | +request = json.load(sys.stdin) |
| 15 | + |
| 16 | +def resolve_module_id_from_uuid(uuid): |
| 17 | + # Redis stores module info under module/{id}/uuid -> value |
| 18 | + # We need to search for the module id that has this uuid |
| 19 | + with agent.redis_connect() as rdb: |
| 20 | + # Only method: check environment hashes where MODULE_UUID is stored (user-provided format) |
| 21 | + for key in rdb.scan_iter(match='module/*/environment'): |
| 22 | + try: |
| 23 | + k = key.decode() if isinstance(key, bytes) else key |
| 24 | + module_uuid_val = rdb.hget(k, 'MODULE_UUID') |
| 25 | + if module_uuid_val and module_uuid_val.decode() == uuid: |
| 26 | + parts = k.split('/') |
| 27 | + if len(parts) >= 2: |
| 28 | + return parts[1] |
| 29 | + except Exception: |
| 30 | + continue |
| 31 | + return None |
| 32 | + |
| 33 | + |
| 34 | +module_uuid = request.get('module_uuid') |
| 35 | +enable_integration = request.get('enable_integration', False) |
| 36 | + |
| 37 | +if not module_uuid: |
| 38 | + print(json.dumps({'exit_code': 2, 'message': 'module_uuid is required'})) |
| 39 | + sys.exit(2) |
| 40 | + |
| 41 | +# Resolve module uuid to module id |
| 42 | +module_id = resolve_module_id_from_uuid(module_uuid) |
| 43 | +if module_id is None: |
| 44 | + print(json.dumps({'exit_code': 2, 'message': f'module_uuid {module_uuid} not found via module/*/environment'})) |
| 45 | + sys.exit(2) |
| 46 | + |
| 47 | +# Build agent id for module |
| 48 | +matrix_agent_id = f'module/{module_id}' |
| 49 | + |
| 50 | +# Call get-configuration on current nethvoice module to obtain hostname |
| 51 | +response = agent.tasks.run(agent_id=os.environ['AGENT_ID'], action='get-configuration', data={}) |
| 52 | +agent.assert_exp(response['exit_code'] == 0) |
| 53 | + |
| 54 | +nconfig = response.get('result', {}) |
| 55 | +nethvoice_host = nconfig.get('nethvoice_host', os.environ.get('NETHVOICE_HOST', '')) |
| 56 | + |
| 57 | +# Call get-configuration on the matrix module and use its response for configure-module |
| 58 | +response = agent.tasks.run(agent_id=matrix_agent_id, action='get-configuration', data={}) |
| 59 | +agent.assert_exp(response['exit_code'] == 0) |
| 60 | + |
| 61 | +matrix_config = response.get('result', {}) or {} |
| 62 | + |
| 63 | +if enable_integration: |
| 64 | + nethvoice_auth_url = f'https://{nethvoice_host}/freepbx/rest/testextauth' if nethvoice_host else '/freepbx/rest/testextauth' |
| 65 | +else: |
| 66 | + nethvoice_auth_url = '' |
| 67 | + |
| 68 | +# Merge/override nethvoice_auth_url into matrix configuration |
| 69 | +matrix_config['nethvoice_auth_url'] = nethvoice_auth_url |
| 70 | + |
| 71 | +# Call configure-module on matrix module with merged configuration |
| 72 | +response = agent.tasks.run(agent_id=matrix_agent_id, action='configure-module', data=matrix_config) |
| 73 | +agent.assert_exp(response['exit_code'] == 0) |
0 commit comments