From 98bd8864acd29ecf1d04f748cb1a04bb18cf6d3b Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Wed, 27 Sep 2023 15:19:01 +0200 Subject: [PATCH] feat: added index_rules --- src/nethsec/mwan/__init__.py | 24 ++++++++++++++++ tests/test_mwan.py | 53 ++++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/src/nethsec/mwan/__init__.py b/src/nethsec/mwan/__init__.py index c73d07c1..f0f36864 100644 --- a/src/nethsec/mwan/__init__.py +++ b/src/nethsec/mwan/__init__.py @@ -325,3 +325,27 @@ def delete_policy(e_uci: EUci, name: str) -> list[str]: e_uci.delete('mwan3', name) e_uci.save('mwan3') return [f'mwan3.{name}'] + + +def index_rules(e_uci: EUci) -> list[dict]: + data = [] + rules = utils.get_all_by_type(e_uci, 'mwan3', 'rule') + for rule_key in rules.keys(): + rule_data = {} + rule_value = rules[rule_key] + rule_data['name'] = rule_key + rule_data['policy'] = {} + rule_data['policy']['name'] = rule_value['use_policy'] + if rule_value['use_policy'] in utils.get_all_by_type(e_uci, 'mwan3', 'policy').keys(): + rule_data['policy']['label'] = utils.get_all_by_type(e_uci, 'mwan3', 'policy')[rule_value['use_policy']]['label'] + if 'label' in rule_value: + rule_data['label'] = rule_value['label'] + if 'proto' in rule_value: + rule_data['protocol'] = rule_value['proto'] + if 'src_ip' in rule_value: + rule_data['source_addresses'] = rule_value['src_ip'] + if 'dest_ip' in rule_value: + rule_data['destination_addresses'] = rule_value['dest_ip'] + + data.append(rule_data) + return data diff --git a/tests/test_mwan.py b/tests/test_mwan.py index c6d88453..617cc9d7 100644 --- a/tests/test_mwan.py +++ b/tests/test_mwan.py @@ -122,7 +122,9 @@ def test_create_interface(e_uci): def test_fail_create_invalid_interface(e_uci): with pytest.raises(ValueError) as err: mwan.__store_interface(e_uci, 'RED_4') - assert err.value.args[0] == ('RED_4', 'invalid') + assert err.value.args[0] == 'name' + assert err.value.args[1] == 'invalid' + assert err.value.args[2] == 'RED_4' def test_interface_avoid_edit_of_metric(e_uci): @@ -286,16 +288,16 @@ def test_unique_rule(e_uci): with pytest.raises(ValueError) as e: mwan.store_rule(e_uci, 'additional rule', 'ns_default') mwan.store_rule(e_uci, 'additional rule', 'ns_default') - assert e.value.args[0] == 'name' - assert e.value.args[1] == 'invalid' + assert e.value.args[0] == 'name' + assert e.value.args[1] == 'unique' 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' + 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): @@ -343,6 +345,39 @@ def test_edit_policy(e_uci): def test_missing_policy(e_uci): with pytest.raises(ValidationError) as e: mwan.edit_policy(e_uci, 'dummy', '', []) - assert e.value[0] == 'name' - assert e.value[1] == 'invalid' - assert e.value[2] == 'dummy' + assert e.value.args[0] == 'name' + assert e.value.args[1] == 'invalid' + assert e.value.args[2] == 'dummy' + + +def test_index_rules(e_uci): + mwan.store_policy(e_uci, 'default', [ + { + 'name': 'RED_1', + 'metric': '10', + 'weight': '100', + }, + { + 'name': 'RED_2', + 'metric': '10', + 'weight': '100', + } + ]) + mwan.store_rule(e_uci, 'additional rule', 'ns_default') + index = mwan.index_rules(e_uci) + assert index[0] == { + 'name': 'ns_default_rule', + 'label': 'Default Rule', + 'policy': { + 'name': 'ns_default', + 'label': 'default', + } + } + assert index[1] == { + 'name': 'ns_additional_r', + 'label': 'additional rule', + 'policy': { + 'name': 'ns_default', + 'label': 'default', + } + }