Skip to content

Commit

Permalink
feat: added delete api
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaile committed Sep 25, 2023
1 parent d7604ab commit eb8b8ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/nethsec/mwan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}']
21 changes: 21 additions & 0 deletions tests/test_mwan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from euci import EUci

from nethsec import mwan
from nethsec.utils import ValidationError

network_db = """
config interface 'loopback'
Expand Down Expand Up @@ -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

0 comments on commit eb8b8ea

Please sign in to comment.