From eb8b8ea9c9e20d62302ac2af4835ebc5a973443c Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Mon, 25 Sep 2023 16:57:37 +0200 Subject: [PATCH] feat: added delete api --- src/nethsec/mwan/__init__.py | 8 ++++++++ tests/test_mwan.py | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/nethsec/mwan/__init__.py b/src/nethsec/mwan/__init__.py index c5f1c1c0..0c65020c 100644 --- a/src/nethsec/mwan/__init__.py +++ b/src/nethsec/mwan/__init__.py @@ -292,3 +292,11 @@ def index_policies(e_uci: EUci) -> list[dict]: # append policy to data data.append(policy_data) return data + + +def delete_policy(e_uci: EUci, name: str) -> list[str]: + if name not in utils.get_all_by_type(e_uci, 'mwan3', 'policy').keys(): + raise ValidationError('name', 'invalid', name) + e_uci.delete('mwan3', name) + e_uci.save('mwan3') + return [f'mwan3.{name}'] diff --git a/tests/test_mwan.py b/tests/test_mwan.py index 1def2d6b..1bcad9a1 100644 --- a/tests/test_mwan.py +++ b/tests/test_mwan.py @@ -4,6 +4,7 @@ from euci import EUci from nethsec import mwan +from nethsec.utils import ValidationError network_db = """ config interface 'loopback' @@ -287,3 +288,23 @@ def test_unique_rule(e_uci): mwan.store_rule(e_uci, 'additional rule', 'ns_default') assert e.value.args[0] == 'name' assert e.value.args[1] == 'invalid' + + +def test_delete_non_existent_policy(e_uci): + with pytest.raises(ValidationError) as e: + mwan.delete_policy(e_uci, 'ns_default') + assert e.value.args[0] == 'name' + assert e.value.args[1] == 'invalid' + assert e.value.args[2] == 'ns_default' + + +def test_delete_policy(e_uci): + mwan.store_policy(e_uci, 'default', [ + { + 'name': 'RED_1', + 'metric': '20', + 'weight': '100', + } + ]) + assert mwan.delete_policy(e_uci, 'ns_default') == ['mwan3.ns_default'] + assert e_uci.get('mwan3', 'ns_default', default=None) is None