Exemple #1
0
 def get_by_ip(cls, vrf, ip, reql=False):
     query = cls.filter(lambda p:
             r.ip_prefix_contains(
                 r.ip_prefix(p['network'], p['length']),
                 r.ip_address(ip)
             ), reql=True) \
             .filter({'vrf': vrf}) \
             .order_by(r.desc("length")).nth(0)
     if reql:
         return query
     else:
         try:
             return query.run(get_connection())
         except r.errors.ReqlNonExistenceError:
             raise RethinkObjectNotFound("no prefix found for IP %s" % ip)
Exemple #2
0
 def validate_name(self, value):
     possibles = []
     for part in value.split(".")[:0:-1]:
         suffix = "" if len(possibles) == 0 else ("." + possibles[-1])
         possibles.append(part + suffix)
     try:
         zone = DNSZoneSerializer.filter(
             lambda zone: r.expr(possibles).contains(zone['name']),
             reql=True).order_by(r.desc(r.row['name'].count())).nth(0).run(
                 self.conn)
     except r.errors.ReqlNonExistenceError:
         raise serializers.ValidationError(
             "no zone matching %s could be found" % value)
     if 'request' in self.context and not self.context[
             'request'].user.is_superuser:
         user_groups = set(
             self.context['request'].user.groups.all().values_list(
                 'name', flat=True))
         if self.instance is not None and len(
                 user_groups.intersection(
                     set(
                         self.instance.get('permissions', {}).get(
                             'write', [])))) > 0:
             pass
         elif len(
                 user_groups.intersection(
                     set(
                         zone.get('permissions', {}).get('create', []) +
                         zone.get('permissions', {}).get('write', [])))
         ) == 0:
             raise serializers.ValidationError(
                 "you do not have permission to create names in %s" %
                 zone['name'])
     try:
         ip_address = IPAddressSerializer.get(name=value)
         if self.instance is None or ip_address['id'] != self.instance['id']:
             raise serializers.ValidationError(
                 "%r is already in use by %s" % (value, ip_address['ip']))
     except RethinkObjectNotFound:
         pass
     return value