Signed-off-by: Zack Cerza <zack@redhat.com>
if remote not in matches.remotes:
c.add(remote, has_roles)
return c
+
+ def filter(self, func):
+ """
+ Return a cluster whose remotes are filtered by `func`.
+
+ Example::
+ cluster = ctx.cluster.filter(lambda r: r.is_online)
+ """
+ result = self.__class__()
+ for rem, roles in self.remotes.items():
+ if func(rem):
+ result.add(rem, roles)
+ return result
c_foo = c.exclude('foo', lambda role: role.startswith('b'))
assert c_foo.remotes == {r2: ['bar'], r3: ['foo']}
+ def test_filter(self):
+ r1 = Mock(_name='r1')
+ r2 = Mock(_name='r2')
+ def func(r):
+ return r._name == "r1"
+ c = cluster.Cluster(remotes=[
+ (r1, ['foo']),
+ (r2, ['bar']),
+ ])
+ assert c.filter(func).remotes == {
+ r1: ['foo']
+ }
+
class TestWriteFile(object):
""" Tests for cluster.write_file """