Skip to content

Commit

Permalink
F filter now supports any for non-list values (#608)
Browse files Browse the repository at this point in the history
* F filter now supports any for non-list values

* fix tests

* fix tests

* fixed tests
  • Loading branch information
dbarrosop committed Feb 27, 2021
1 parent 98c7e57 commit 9477200
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion docs/howto/handling_connections.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
" password: a_password\r\n",
" platform: eos\r\n",
" data:\r\n",
" some_string_to_test_any_all: prefix\r\n",
" my_var: comes_from_dev1.group_1\r\n",
" www_server: nginx\r\n",
" role: www\r\n",
Expand Down Expand Up @@ -169,7 +170,7 @@
}
],
"source": [
"!sed '2,35!d' ../../tests/inventory_data/hosts.yaml"
"!sed '2,36!d' ../../tests/inventory_data/hosts.yaml"
]
}
],
Expand Down
11 changes: 9 additions & 2 deletions nornir/core/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,16 @@ def _verify_rules(data: Any, rule: List[str], value: Any) -> bool:
elif rule == ["in"]:
return bool(data in value)
elif rule == ["any"]:
return any([x in data for x in value])
if isinstance(data, list):
return any([x in data for x in value])
else:
return any([x == data for x in value])
elif rule == ["all"]:
return all([x in data for x in value])
if isinstance(data, list):
return all([x in data for x in value])
else:
# it doesn't make sense to check a single value meets more than one case
return False
else:
return bool(data.get(rule[0]) == value)

Expand Down
6 changes: 6 additions & 0 deletions tests/core/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ def test_filtering_string_in_list(self, nornir):
"dev6.group_3",
]

def test_filtering_string_any(self, nornir):
f = F(some_string_to_test_any_all__any=["prefix", "other_prefix"])
filtered = sorted(list((nornir.inventory.filter(f).hosts.keys())))

assert filtered == ["dev1.group_1", "dev3.group_2", "dev4.group_2"]

def test_filtering_list_any(self, nornir):
f = F(nested_data__a_list__any=[1, 3])
filtered = sorted(list((nornir.inventory.filter(f).hosts.keys())))
Expand Down
9 changes: 8 additions & 1 deletion tests/core/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ def test_inventory_dict(self, inv):
},
"group_2": {
"connection_options": {},
"data": {"site": "site2"},
"data": {
"some_string_to_test_any_all": "other_prefix",
"site": "site2",
},
"groups": [],
"hostname": None,
"name": "group_2",
Expand Down Expand Up @@ -191,6 +194,7 @@ def test_inventory_dict(self, inv):
},
},
"data": {
"some_string_to_test_any_all": "prefix",
"my_var": "comes_from_dev1.group_1",
"nested_data": {
"a_dict": {"a": 1, "b": 2},
Expand Down Expand Up @@ -228,6 +232,7 @@ def test_inventory_dict(self, inv):
},
},
"data": {
"some_string_to_test_any_all": "prefix_longer",
"nested_data": {
"a_dict": {"b": 2, "c": 3},
"a_list": [2, 3],
Expand Down Expand Up @@ -329,13 +334,15 @@ def test_extended_data(self, inv):
"only_default": "only_defined_in_default",
"role": "www",
"site": "site1",
"some_string_to_test_any_all": "prefix",
"www_server": "nginx",
}
assert inv.hosts["dev3.group_2"].extended_data() == {
"my_var": "comes_from_defaults",
"only_default": "only_defined_in_default",
"role": "www",
"site": "site2",
"some_string_to_test_any_all": "other_prefix",
"www_server": "apache",
}
assert inv.hosts["dev5.no_group"].extended_data() == {
Expand Down
1 change: 1 addition & 0 deletions tests/inventory_data/groups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ group_2:
password:
platform:
data:
some_string_to_test_any_all: other_prefix
site: site2
groups: []
connection_options: {}
Expand Down
2 changes: 2 additions & 0 deletions tests/inventory_data/hosts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dev1.group_1:
password: a_password
platform: eos
data:
some_string_to_test_any_all: prefix
my_var: comes_from_dev1.group_1
www_server: nginx
role: www
Expand Down Expand Up @@ -40,6 +41,7 @@ dev2.group_1:
password:
platform: junos
data:
some_string_to_test_any_all: prefix_longer
role: db
nested_data:
a_dict:
Expand Down

0 comments on commit 9477200

Please sign in to comment.