def prefix_get(self, prefix, vrf_id): """ Get prefix object :param vrf_id: :param prefix: :return: """ # Search for prefixes matching prefix & vrf_id query = { 'operator': 'and', 'val1': { 'operator': 'equals', 'val1': 'prefix', 'val2': prefix, }, 'val2': { 'operator': 'equals', 'val1': 'vrf_id', 'val2': vrf_id } } self.lock.acquire() try: prefixes = Prefix.search(query)['result'] except Exception as e: self.lock.release() raise e else: self.lock.release() return prefixes[0] if len(prefixes) == 1 else None
def search_prefix(self): """ Search prefixes. Does not yet incorporate all the functions of the search_prefix API function due to difficulties with transferring a complete 'dict-to-sql' encoded data structure. Instead, a list of prefix attributes can be given which will be matched with the 'equals' operator if notheing else is specified. If multiple attributes are given, they will be combined with the 'and' operator. Currently, it is not possible to specify different operators for different attributes. """ # extract operator if 'operator' in request.json: operator = request.json['operator'] else: operator = 'equals' # fetch attributes from request.json attr = XhrController.extract_prefix_attr(request.json) # build query dict n = 0 q = {} for key, val in attr.items(): if n == 0: q = { 'operator': operator, 'val1': key, 'val2': val } else: q = { 'operator': 'and', 'val1': { 'operator': operator, 'val1': key, 'val2': val }, 'val2': q } n += 1 # extract search options search_opts = {} if 'children_depth' in request.json: search_opts['children_depth'] = request.json['children_depth'] if 'parents_depth' in request.json: search_opts['parents_depth'] = request.json['parents_depth'] if 'include_neighbors' in request.json: search_opts['include_neighbors'] = request.json['include_neighbors'] if 'max_result' in request.json: search_opts['max_result'] = request.json['max_result'] if 'offset' in request.json: search_opts['offset'] = request.json['offset'] try: result = Prefix.search(q, search_opts) except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
def view_prefix(arg, opts): """ View a single prefix. """ s = get_schema() res = Prefix.search(s, { 'operator': 'equals', 'val1': 'prefix', 'val2': arg }, {}) if len(res['result']) == 0: print "Address %s not found." % arg return p = res['result'][0] print "-- Address " print " %-15s : %s" % ("Prefix", p.prefix) print " %-15s : %s" % ("Display prefix", p.display_prefix) print " %-15s : %s" % ("Type", p.type) print " %-15s : IPv%s" % ("Family", p.family) print " %-15s : %s" % ("Description", p.description) print " %-15s : %s" % ("Node", p.node) print " %-15s : %s" % ("Order", p.order_id) print " %-15s : %s" % ("VRF", p.vrf) print " %-15s : %s" % ("Alarm priority", p.alarm_priority) print " %-15s : %s" % ("Monitor", p.monitor) print "-- Comment" print p.comment
def find_prefix(self, rt, prefix): """ Find a prefix for a given route target (VRF) :param rt: string such as '1.1.1.0/24' :param prefix: string such as '1.1.1.0/24' :return: a Prefix object or None """ retVal = None try: # retVal = VRF.search({'val1': 'id', 'operator': 'equals', 'val2': '10'})['result'][0] retVal = Prefix.search({'val1': 'prefix', 'operator': 'equals', 'val2': prefix}) if not retVal['result']: retVal = None return retVal for myPrefix in retVal['result']: if myPrefix.vrf.rt == rt: return myPrefix except: e = sys.exc_info()[0] logging.error("Error: could not find prefix: %s" % e) retVal = None return retVal
import pynipap from pynipap import Prefix a = pynipap.AuthOptions({ 'authoritative_source': 'NIPAP-Sync/1.0' }) pynipap.xmlrpc_uri = "{nipapurl}" query = { 'operator': 'equals', 'val1': 'type', 'val2': 'host' } search_options = { 'max_result': 1000 } search_result = Prefix.search(query, search_options) file = open("/tmp/nipap.hosts", "w") for p in search_result['result']: ip = p.prefix.split('/')[0] host = p.description.replace(" ", "_") file.write(ip + " " + host + "\n") file.close()
def search_prefix(self): """ Search prefixes. Does not yet incorporate all the functions of the search_prefix API function due to difficulties with transferring a complete 'dict-to-sql' encoded data structure. Instead, a list of prefix attributes can be given which will be matched with the 'equals' operator if notheing else is specified. If multiple attributes are given, they will be combined with the 'and' operator. Currently, it is not possible to specify different operators for different attributes. """ # extract operator if 'operator' in request.json: operator = request.json['operator'] else: operator = 'equals' # fetch attributes from request.json attr = XhrController.extract_prefix_attr(request.json) # build query dict n = 0 q = {} for key, val in attr.items(): if n == 0: q = {'operator': operator, 'val1': key, 'val2': val} else: q = { 'operator': 'and', 'val1': { 'operator': operator, 'val1': key, 'val2': val }, 'val2': q } n += 1 # extract search options search_opts = {} if 'children_depth' in request.json: search_opts['children_depth'] = request.json['children_depth'] if 'parents_depth' in request.json: search_opts['parents_depth'] = request.json['parents_depth'] if 'include_neighbors' in request.json: search_opts['include_neighbors'] = request.json[ 'include_neighbors'] if 'max_result' in request.json: search_opts['max_result'] = request.json['max_result'] if 'offset' in request.json: search_opts['offset'] = request.json['offset'] try: result = Prefix.search(q, search_opts) except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ })
def replace(pattern, replacement): # Fetch prefixes matching the string to replace print "Fetching prefixes from NIPAP... ", sys.stdout.flush() n = 1 prefix_list = [] t0 = time.time() query = { 'operator': 'or', 'val1': { 'operator': 'regex_match', 'val1': 'description', 'val2': pattern }, 'val2': { 'operator': 'regex_match', 'val1': 'node', 'val2': pattern } } full_result = Prefix.search(query, { 'parents_depth': -1, 'max_result': BATCH_SIZE }) prefix_result = full_result['result'] prefix_list += prefix_result print len(prefix_list), sys.stdout.flush() while len(prefix_result) == 100: full_result = Prefix.smart_search(pattern, { 'parents_depth': -1, 'max_result': BATCH_SIZE, 'offset': n * BATCH_SIZE }) prefix_result = full_result['result'] prefix_list += prefix_result print len(prefix_list), sys.stdout.flush() n += 1 t1 = time.time() print " done in %.1f seconds" % (t1 - t0) # Display list print_pattern = "%-2s%-14s%-2s%-30s%-20s%s" print "\n\nPrefixes to change:" print print_pattern % ("", "VRF", "", "Prefix", "Node", "Description") i_match = 0 for i, prefix in enumerate(prefix_list): if prefix.match: print COLOR_RESET, print " -- %d --" % i color = COLOR_RED else: color = COLOR_RESET print (color + print_pattern) % ( "-" if prefix.match else "", prefix.vrf.rt, prefix.type[0].upper(), ((" " * prefix.indent) + prefix.display_prefix)[:min([ len(prefix.display_prefix) + 2*prefix.indent, 30 ])], (prefix.node or '')[:min([ len(prefix.node or ''), 20 ])], (prefix.description or '')[:min([ len(prefix.description or ''), 900 ])] ) if prefix.match: new_prefix_node = re.sub(pattern, replacement, (prefix.node or ''), flags=re.IGNORECASE) new_prefix_desc = re.sub(pattern, replacement, (prefix.description or ''), flags=re.IGNORECASE) print (COLOR_GREEN + print_pattern) % ( "+", prefix.vrf.rt, prefix.type[0].upper(), (" " * prefix.indent + prefix.display_prefix)[:min([ len(prefix.display_prefix) + 2*prefix.indent, 30 ])], new_prefix_node[:min([ len(new_prefix_node), 20 ])], new_prefix_desc[:min([ len(new_prefix_desc), 90 ])] ) # reset colors print COLOR_RESET, # Perform action? print "Select replacements to perform" print "Enter comma-separated selection (eg. 5,7,10) or \"all\" for all prefixes." print "Prefix list with ! to invert selection (eg !5,7,10 to perform operation on all except the entered prefixes)" inp = raw_input("Selection: ").strip() if len(inp) == 0: print "Empty selection, quitting." sys.exit(0) invert = False if inp[0] == "!": inp = inp[1:] invert = True rename_all = False if inp == 'all': rename_all = True selection = [] else: selection = inp.split(",") try: selection = map(lambda x: int(x.strip()), selection) except ValueError as e: print >> sys.stderr, "Could not parse selection: %s" % str(e) sys.exit(1) for i, prefix in enumerate(prefix_list): if prefix.match and ((invert and i not in selection) or (not invert and i in selection) or rename_all): if prefix.node is not None: prefix.node = re.sub(pattern, replacement, prefix.node, flags=re.IGNORECASE) if prefix.description is not None: prefix.description = re.sub(pattern, replacement, prefix.description, flags=re.IGNORECASE) print "Saving prefix %s..." % prefix.display_prefix prefix.save()
def test_search_prefix(self): """ We should be able to execute search_prefix as read-only user """ p = Prefix.search({ 'val1': 'id', 'operator': 'equals', 'val2': 0 })
def replace(pattern, replacement): # Fetch prefixes matching the string to replace print "Fetching prefixes from NIPAP... ", sys.stdout.flush() n = 1 prefix_list = [] t0 = time.time() query = { 'operator': 'or', 'val1': { 'operator': 'regex_match', 'val1': 'description', 'val2': pattern }, 'val2': { 'operator': 'regex_match', 'val1': 'node', 'val2': pattern } } full_result = Prefix.search(query, { 'parents_depth': -1, 'max_result': BATCH_SIZE }) prefix_result = full_result['result'] prefix_list += prefix_result print len(prefix_list), sys.stdout.flush() while len(prefix_result) == 100: full_result = Prefix.smart_search( pattern, { 'parents_depth': -1, 'max_result': BATCH_SIZE, 'offset': n * BATCH_SIZE }) prefix_result = full_result['result'] prefix_list += prefix_result print len(prefix_list), sys.stdout.flush() n += 1 t1 = time.time() print " done in %.1f seconds" % (t1 - t0) # Display list print_pattern = "%-2s%-14s%-2s%-30s%-20s%s" print "\n\nPrefixes to change:" print print_pattern % ("", "VRF", "", "Prefix", "Node", "Description") i_match = 0 for i, prefix in enumerate(prefix_list): if prefix.match: print COLOR_RESET, print " -- %d --" % i color = COLOR_RED else: color = COLOR_RESET print(color + print_pattern) % ( "-" if prefix.match else "", prefix.vrf.rt, prefix.type[0].upper(), ((" " * prefix.indent) + prefix.display_prefix )[:min([len(prefix.display_prefix) + 2 * prefix.indent, 30])], (prefix.node or '')[:min([len(prefix.node or ''), 20])], (prefix.description or '')[:min([len(prefix.description or ''), 900])]) if prefix.match: new_prefix_node = re.sub(pattern, replacement, (prefix.node or ''), flags=re.IGNORECASE) new_prefix_desc = re.sub(pattern, replacement, (prefix.description or ''), flags=re.IGNORECASE) print(COLOR_GREEN + print_pattern) % ( "+", prefix.vrf.rt, prefix.type[0].upper(), (" " * prefix.indent + prefix.display_prefix )[:min([len(prefix.display_prefix) + 2 * prefix.indent, 30])], new_prefix_node[:min([len(new_prefix_node), 20])], new_prefix_desc[:min([len(new_prefix_desc), 90])]) # reset colors print COLOR_RESET, # Perform action? print "Select replacements to perform" print "Enter comma-separated selection (eg. 5,7,10) or \"all\" for all prefixes." print "Prefix list with ! to invert selection (eg !5,7,10 to perform operation on all except the entered prefixes)" inp = raw_input("Selection: ").strip() if len(inp) == 0: print "Empty selection, quitting." sys.exit(0) invert = False if inp[0] == "!": inp = inp[1:] invert = True rename_all = False if inp == 'all': rename_all = True selection = [] else: selection = inp.split(",") try: selection = map(lambda x: int(x.strip()), selection) except ValueError as e: print >> sys.stderr, "Could not parse selection: %s" % str(e) sys.exit(1) for i, prefix in enumerate(prefix_list): if prefix.match and ((invert and i not in selection) or (not invert and i in selection) or rename_all): if prefix.node is not None: prefix.node = re.sub(pattern, replacement, prefix.node, flags=re.IGNORECASE) if prefix.description is not None: prefix.description = re.sub(pattern, replacement, prefix.description, flags=re.IGNORECASE) print "Saving prefix %s..." % prefix.display_prefix prefix.save()