From 6bfddf90fa04d34abd5f1382883d73b9c8c3aa09 Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Thu, 28 Sep 2023 17:43:40 +0200 Subject: [PATCH] feat: added edit_rule --- src/nethsec/mwan/__init__.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/nethsec/mwan/__init__.py b/src/nethsec/mwan/__init__.py index 282bf3a3..db93117b 100644 --- a/src/nethsec/mwan/__init__.py +++ b/src/nethsec/mwan/__init__.py @@ -392,3 +392,33 @@ def delete_rule(e_uci: EUci, name: str): e_uci.delete('mwan3', name) e_uci.save('mwan3') + return f'mwan3.{name}' + + +def edit_rule(e_uci: EUci, name: str, policy: str, label: str, protocol: str = None, + source_address: str = None, source_port: str = None, + destination_address: str = None, destination_port: str = None): + if name not in utils.get_all_by_type(e_uci, 'mwan3', 'rule').keys(): + raise ValidationError('name', 'invalid', name) + + if policy not in utils.get_all_by_type(e_uci, 'mwan3', 'policy').keys(): + raise ValidationError('policy', 'invalid', policy) + e_uci.set('mwan3', name, 'use_policy', policy) + e_uci.set('mwan3', name, 'label', label) + if protocol is not None: + e_uci.set('mwan3', name, 'proto', protocol) + if protocol != 'tcp' and protocol != 'udp': + e_uci.delete('mwan3', name, 'src_port') + e_uci.delete('mwan3', name, 'dest_port') + else: + if destination_port is not None: + e_uci.set('mwan3', name, 'dest_port', destination_port) + if source_port is not None: + e_uci.set('mwan3', name, 'src_port', source_port) + if source_address is not None: + e_uci.set('mwan3', name, 'src_ip', source_address) + if destination_address is not None: + e_uci.set('mwan3', name, 'dest_ip', destination_address) + + e_uci.save('mwan3') + return f'mwan3.{name}'