def check_node_limits(self, models, nodes, role, limits, limit_reached=True, limit_types=['min', 'max', 'recommended']): """Check nodes limits for current role :param models: objects which represent models in restrictions :type models: dict :param nodes: list of nodes to check limits count for role :type nodes: list :param role: node role name :type role: string :param limits: object with min|max|recommended values and overrides :type limits: dict :param limit_reached: flag to check possibility adding/removing nodes :type limit_reached: bool :param limit_types: List of possible limit types (min|max|recommended) :type limit_types: list :returns: dict -- object with bool 'valid' flag and related information """ self.checked_limit_types = {} self.models = models self.overrides = limits.get('overrides', []) self.limit_reached = limit_reached self.limit_types = limit_types self.limit_values = { 'max': self._evaluate_expression(limits.get('max'), self.models), 'min': self._evaluate_expression(limits.get('min'), self.models), 'recommended': self._evaluate_expression(limits.get('recommended'), self.models) } self.count = len( filter( lambda node: not (node.pending_deletion) and (role in node.roles), nodes)) self.messages = compact( flatten(map(self._check_override, self.overrides))) self.messages += compact( flatten(map(self._check_limit_type, self.limit_types))) self.messages = compact(flatten(map(self._get_message, limit_types))) self.messages = '. '.join(self.messages) return { 'count': self.count, 'limits': self.limit_values, 'messages': self.messages, 'valid': not self.messages }
def check_node_limits(self, models, nodes, role, limits, limit_reached=True, limit_types=['min', 'max', 'recommended']): """Check nodes limits for current role :param models: objects which represent models in restrictions :type models: dict :param nodes: list of nodes to check limits count for role :type nodes: list :param role: node role name :type role: string :param limits: object with min|max|recommended values and overrides :type limits: dict :param limit_reached: flag to check possibility adding/removing nodes :type limit_reached: bool :param limit_types: List of possible limit types (min|max|recommended) :type limit_types: list :returns: dict -- object with bool 'valid' flag and related information """ self.checked_limit_types = {} self.models = models self.overrides = limits.get('overrides', []) self.limit_reached = limit_reached self.limit_types = limit_types self.limit_values = { 'max': self._evaluate_expression( limits.get('max'), self.models), 'min': self._evaluate_expression( limits.get('min'), self.models), 'recommended': self._evaluate_expression( limits.get('recommended'), self.models) } self.count = len(filter( lambda node: not(node.pending_deletion) and (role in node.roles), nodes)) self.messages = compact(flatten( map(self._check_override, self.overrides))) self.messages += compact(flatten( map(self._check_limit_type, self.limit_types))) self.messages = compact(flatten( map(self._get_message, limit_types))) self.messages = '. '.join(self.messages) return { 'count': self.count, 'limits': self.limit_values, 'messages': self.messages, 'valid': not self.messages }
def validate_regex(data): attr_regex = data.get('regex', {}) if attr_regex: attr_value = data.get('value') pattern = re.compile(attr_regex.get('source')) error = attr_regex.get('error') def test_regex(value, pattern=pattern, error=error): if not pattern.search(value): return error if isinstance(attr_value, six.string_types): return test_regex(attr_value) elif isinstance(attr_value, list): errors = map(test_regex, attr_value) if compact(errors): return errors else: return ('Value {0} is of invalid type, cannot check ' 'regexp'.format(attr_value))
def test_compact(self): self.assertListEqual( compact([1, '', 5, False, None, False, 'test']), [1, 5, 'test'])
def test_compact(self): self.assertListEqual(compact([1, '', 5, False, None, False, 'test']), [1, 5, 'test'])