Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter object #155

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
79 changes: 79 additions & 0 deletions nornir/plugins/inventory/helpers/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import copy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should consider this core functionality so I'd place it under nornir/core/filter.py



class F(object):
AND = "AND"
OR = "OR"

def __init__(self, **kwargs):
self.operator = self.AND
self.operands = []
self.filters = list(kwargs.items())
self.negate = False

def __call__(self, object):
if self.operator == self.AND:
reducer_func = all
elif self.operator == self.OR:
reducer_func = any
matching_result = [o(object) for o in self.operands]
matching_result += [
F._verify_rules(object, k.split("__"), v) for k, v in self.filters
]
if self.negate:
return not reducer_func(matching_result)

else:
return reducer_func(matching_result)

def __and__(self, other):
res = F()
if self.operator == self.AND and other.operator == self.AND:
res.operator = self.AND
res.operands = self.operands + other.operands
res.filters = self.filters + other.filters
else:
res.operator = self.AND
res.operands = [self, other]
return res

def __or__(self, other):
res = F()
if self.operator == self.OR and other.operator == self.OR:
res.operator = self.OR
res.oprands = self.operands + other.operands
res.filters = self.filters + other.filters
else:
res.operator = self.OR
res.operands = [self, other]
return res

def __invert__(self):
res = copy.deepcopy(self)
res.negate = ~res.negate
return res

def __repr__(self):
if self.negate:
template = "<Filter NOT {} {}>"
else:
template = "<Filter {} {}>"
return template.format(self.operator, self.operands + 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)

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)
)
Empty file.
43 changes: 43 additions & 0 deletions tests/plugins/inventory/helpers/test_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from nornir.plugins.inventory.helpers.filters import F
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per above comment this should be "tests/core/test_filter.py".

Also, please, follow the same pattern as with the rest of the tests, please.



def test_simple(nornir):
f = F(site="site1")
filtered = sorted(list((nornir.filter(filter_func=f).inventory.hosts.keys())))

assert filtered == ["dev1.group_1", "dev2.group_1"]


def test_and(nornir):
f = F(site="site1") & F(role="www")
filtered = sorted(list((nornir.filter(filter_func=f).inventory.hosts.keys())))

assert filtered == ["dev1.group_1"]


def test_or(nornir):
f = F(site="site1") | F(role="www")
filtered = sorted(list((nornir.filter(filter_func=f).inventory.hosts.keys())))

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


def test_combined(nornir):
f = F(site="site2") | (F(role="www") & F(my_var="comes_from_dev1.group_1"))
filtered = sorted(list((nornir.filter(filter_func=f).inventory.hosts.keys())))

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


def test_contains(nornir):
f = F(groups__contains="group_1")
filtered = sorted(list((nornir.filter(filter_func=f).inventory.hosts.keys())))

assert filtered == ["dev1.group_1", "dev2.group_1"]


def test_negate(nornir):
f = ~F(groups__contains="group_1")
filtered = sorted(list((nornir.filter(filter_func=f).inventory.hosts.keys())))

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