nmstatectl: allow route rule filtering by interface

"nmstatectl show <interface_name>" is now filtering the route rules by interface name.

Signed-off-by: Arunagirinadan Sudharshan <sudhan.aruna.97@gmail.com>
This commit is contained in:
SudhanAruna 2020-03-14 14:49:55 +05:30 committed by Gris Ge
parent 6ab66726dc
commit 7d13ec245d
2 changed files with 29 additions and 0 deletions

View File

@ -35,6 +35,7 @@ from libnmstate.error import NmstatePermissionError
from libnmstate.error import NmstateValueError
from libnmstate.schema import Interface
from libnmstate.schema import Route
from libnmstate.schema import RouteRule
def main():
@ -279,6 +280,7 @@ def _filter_state(state, whitelist):
patterns = [p for p in whitelist.split(",")]
state[Interface.KEY] = _filter_interfaces(state, patterns)
state[Route.KEY] = _filter_routes(state, patterns)
state[RouteRule.KEY] = _filter_route_rule(state)
return state
@ -394,3 +396,20 @@ def _filter_routes(state, patterns):
if fnmatch.fnmatch(route[Route.NEXT_HOP_INTERFACE], pattern):
routes[route_type].append(route)
return routes
def _filter_route_rule(state):
"""
return the rules for state's route rule that match the table_id of the
filtered route of state by interface
"""
route_rules = {RouteRule.CONFIG: []}
table_ids = []
for routes in {Route.CONFIG: [], Route.RUNNING: []}:
for route in state.get(Route.KEY, {}).get(routes, []):
if route.get(Route.TABLE_ID) not in table_ids:
table_ids.append(route.get(Route.TABLE_ID))
for rule in state.get(RouteRule.KEY, {}).get(RouteRule.CONFIG, []):
if rule.get(RouteRule.ROUTE_TABLE) in table_ids:
route_rules[RouteRule.CONFIG].append(rule)
return route_rules

View File

@ -25,6 +25,9 @@ from unittest import mock
from nmstatectl import nmstatectl
LO_JSON_STATE = """{
"route-rules": {
"config": []
},
"routes": {
"config": [],
"running": []
@ -40,6 +43,9 @@ LO_JSON_STATE = """{
"""
EMPTY_JSON_STATE = """{
"route-rules": {
"config": []
},
"routes": {
"config": [],
"running": []
@ -49,6 +55,8 @@ EMPTY_JSON_STATE = """{
"""
EMPTY_YAML_STATE = """---
route-rules:
config: []
routes:
config: []
running: []
@ -56,6 +64,8 @@ interfaces: []
"""
LO_YAML_STATE = """---
route-rules:
config: []
routes:
config: []
running: []