-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
542 additions
and
106 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
class F_OP_BASE(object): | ||
|
||
def __init__(self, op1, op2): | ||
self.op1 = op1 | ||
self.op2 = op2 | ||
|
||
def __and__(self, other): | ||
return AND(self, other) | ||
|
||
def __or__(self, other): | ||
return OR(self, other) | ||
|
||
def __repr__(self): | ||
return "( {} {} {} )".format(self.op1, self.__class__.__name__, self.op2) | ||
|
||
|
||
class AND(F_OP_BASE): | ||
|
||
def __call__(self, host): | ||
return self.op1(host) and self.op2(host) | ||
|
||
|
||
class OR(F_OP_BASE): | ||
|
||
def __call__(self, host): | ||
return self.op1(host) or self.op2(host) | ||
|
||
|
||
class F(object): | ||
|
||
def __init__(self, **kwargs): | ||
self.filters = kwargs | ||
|
||
def __call__(self, host): | ||
return all( | ||
F._verify_rules(host, k.split("__"), v) for k, v in self.filters.items() | ||
) | ||
|
||
def __and__(self, other): | ||
return AND(self, other) | ||
|
||
def __or__(self, other): | ||
return OR(self, other) | ||
|
||
def __invert__(self): | ||
return NOT_F(**self.filters) | ||
|
||
def __repr__(self): | ||
return "<Filter ({})>".format(self.filters) | ||
|
||
@staticmethod | ||
def _verify_rules(data, rule, value): | ||
if len(rule) > 1: | ||
return F._verify_rules(data.get(rule[0], {}), rule[1:], value) | ||
|
||
elif len(rule) == 1: | ||
operator = "__{}__".format(rule[0]) | ||
if hasattr(data, operator): | ||
return getattr(data, operator)(value) | ||
|
||
elif hasattr(data, rule[0]): | ||
if callable(getattr(data, rule[0])): | ||
return getattr(data, rule[0])(value) | ||
|
||
else: | ||
return getattr(data, rule[0]) == value | ||
|
||
else: | ||
return data.get(rule[0]) == value | ||
|
||
else: | ||
raise Exception( | ||
"I don't know how I got here:\n{}\n{}\n{}".format(data, rule, value) | ||
) | ||
|
||
|
||
class NOT_F(F): | ||
|
||
def __call__(self, host): | ||
return not any( | ||
F._verify_rules(host, k.split("__"), v) for k, v in self.filters.items() | ||
) | ||
|
||
def __invert__(self): | ||
return F(**self.filters) | ||
|
||
def __repr__(self): | ||
return "<Filter NOT ({})>".format(self.filters) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import os | ||
|
||
from nornir.core.filter import F | ||
from nornir.plugins.inventory.simple import SimpleInventory | ||
|
||
dir_path = os.path.dirname(os.path.realpath(__file__)) | ||
inventory = SimpleInventory( | ||
"{}/../inventory_data/hosts.yaml".format(dir_path), | ||
"{}/../inventory_data/groups.yaml".format(dir_path), | ||
) | ||
|
||
|
||
class Test(object): | ||
|
||
def test_simple(self): | ||
f = F(site="site1") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1", "dev2.group_1"] | ||
|
||
def test_and(self): | ||
f = F(site="site1") & F(role="www") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1"] | ||
|
||
def test_or(self): | ||
f = F(site="site1") | F(role="www") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1", "dev2.group_1", "dev3.group_2"] | ||
|
||
def test_combined(self): | ||
f = F(site="site2") | (F(role="www") & F(my_var="comes_from_dev1.group_1")) | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1", "dev3.group_2", "dev4.group_2"] | ||
|
||
f = (F(site="site2") | F(role="www")) & F(my_var="comes_from_dev1.group_1") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1"] | ||
|
||
def test_contains(self): | ||
f = F(groups__contains="group_1") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1", "dev2.group_1"] | ||
|
||
def test_negate(self): | ||
f = ~F(groups__contains="group_1") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev3.group_2", "dev4.group_2"] | ||
|
||
def test_negate_and_second_negate(self): | ||
f = F(site="site1") & ~F(role="www") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev2.group_1"] | ||
|
||
def test_negate_or_both_negate(self): | ||
f = ~F(site="site1") | ~F(role="www") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev2.group_1", "dev3.group_2", "dev4.group_2"] | ||
|
||
def test_nested_data_a_string(self): | ||
f = F(nested_data__a_string="asdasd") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1"] | ||
|
||
def test_nested_data_a_string_contains(self): | ||
f = F(nested_data__a_string__contains="asd") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1"] | ||
|
||
def test_nested_data_a_dict_contains(self): | ||
f = F(nested_data__a_dict__contains="a") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1"] | ||
|
||
def test_nested_data_a_dict_element(self): | ||
f = F(nested_data__a_dict__a=1) | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1"] | ||
|
||
def test_nested_data_a_dict_doesnt_contain(self): | ||
f = ~F(nested_data__a_dict__contains="a") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev2.group_1", "dev3.group_2", "dev4.group_2"] | ||
|
||
def test_nested_data_a_list_contains(self): | ||
f = F(nested_data__a_list__contains=2) | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1", "dev2.group_1"] | ||
|
||
def test_filtering_by_callable_has_parent_group(self): | ||
f = F(has_parent_group="parent_group") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1", "dev2.group_1"] | ||
|
||
def test_filtering_by_attribute_name(self): | ||
f = F(name="dev1.group_1") | ||
filtered = sorted(list((inventory.filter(f).hosts.keys()))) | ||
|
||
assert filtered == ["dev1.group_1"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.