Skip to content

Commit

Permalink
Dedupe/simplify roster matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
kojiromike committed Oct 15, 2018
1 parent e57a110 commit 192dd12
Showing 1 changed file with 35 additions and 40 deletions.
75 changes: 35 additions & 40 deletions salt/utils/roster_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from __future__ import absolute_import, print_function, unicode_literals

# Import python libs
import copy
import fnmatch
import functools
import logging
import re
import copy

# Try to import range from https://github.com/ytoolshed/range
HAS_RANGE = False
Expand All @@ -31,6 +32,18 @@ def targets(conditioned_raw, tgt, tgt_type, ipv='ipv4'):
return rmatcher.targets()


def _tgt_set(tgt):
'''
Return the tgt as a set of literal names
'''
try:
# A comma-delimited string
return set(tgt.split(','))
except AttributeError:
# Assume tgt is already a non-string iterable.
return set(tgt)


class RosterMatcher(object):
'''
Matcher for the roster data structure
Expand All @@ -50,59 +63,47 @@ def targets(self):
except AttributeError:
return {}

def ret_glob_minions(self):
def _ret_minions(self, filter_):
'''
Return minions that match via glob
Filter minions by a generic filter.
'''
minions = {}
for minion in self.raw:
if fnmatch.fnmatch(minion, self.tgt):
data = self.get_data(minion)
if data:
minions[minion] = data.copy()
for minion in filter_(self.raw):
data = self.get_data(minion)
if data:
minions[minion] = data.copy()
return minions

def ret_glob_minions(self):
'''
Return minions that match via glob
'''
fnfilter = functools.partial(fnmatch.filter, pat=self.tgt)
return self._ret_minions(fnfilter)

def ret_pcre_minions(self):
'''
Return minions that match via pcre
'''
minions = {}
for minion in self.raw:
if re.match(self.tgt, minion):
data = self.get_data(minion)
if data:
minions[minion] = data.copy()
return minions
tgt = re.compile(self.tgt)
refilter = functools.partial(filter, tgt.match)
return self._ret_minions(refilter)

def ret_list_minions(self):
'''
Return minions that match via list
'''
minions = {}
if not isinstance(self.tgt, list):
self.tgt = self.tgt.split(',')
for minion in self.raw:
if minion in self.tgt:
data = self.get_data(minion)
if data:
minions[minion] = data.copy()
return minions
tgt = _tgt_set(self.tgt)
return self._ret_minions(tgt.intersection)

def ret_nodegroup_minions(self):
'''
Return minions which match the special list-only groups defined by
ssh_list_nodegroups
'''
minions = {}
nodegroup = __opts__.get('ssh_list_nodegroups', {}).get(self.tgt, [])
if not isinstance(nodegroup, list):
nodegroup = nodegroup.split(',')
for minion in self.raw:
if minion in nodegroup:
data = self.get_data(minion)
if data:
minions[minion] = data.copy()
return minions
nodegroup = _tgt_set(nodegroup)
return self._ret_minions(nodegroup.intersection)

def ret_range_minions(self):
'''
Expand All @@ -113,13 +114,7 @@ def ret_range_minions(self):

minions = {}
range_hosts = _convert_range_to_list(self.tgt, __opts__['range_server'])

for minion in self.raw:
if minion in range_hosts:
data = self.get_data(minion)
if data:
minions[minion] = data.copy()
return minions
return self._ret_minions(range_hosts.__contains__)

def get_data(self, minion):
'''
Expand Down

0 comments on commit 192dd12

Please sign in to comment.