def test_load_data(self): """ """ th = TestHelper() p1 = th.add_prefix('192.168.0.0/16', 'reservation', 'test') p2 = th.add_prefix('192.168.0.0/20', 'reservation', 'test') p3 = th.add_prefix('192.168.0.0/24', 'reservation', 'test') p4 = th.add_prefix('192.168.1.0/24', 'reservation', 'test') p5 = th.add_prefix('192.168.2.0/24', 'reservation', 'test') p6 = th.add_prefix('192.168.32.0/20', 'reservation', 'test') p7 = th.add_prefix('192.168.32.0/24', 'reservation', 'test') ps1 = th.add_prefix('2001:db8:1::/48', 'reservation', 'test') ps2 = th.add_prefix('2001:db8:1::/64', 'reservation', 'test') ps3 = th.add_prefix('2001:db8:2::/48', 'reservation', 'test') pool1 = Pool() pool1.name = 'upgrade-test' pool1.save() p2.pool = pool1 p2.save() ps1.pool = pool1 ps1.save() pool2 = Pool() pool2.name = 'upgrade-test2' pool2.save() vrf1 = VRF() vrf1.name = 'foo' vrf1.rt = '123:123' vrf1.save()
def test_edit_vrf(self): """ We should NOT be able to execute edit_vrf as read-only user """ v = VRF() v.id = 123 with self.assertRaises(NipapAuthorizationError): v.save()
def test_remove_vrf(self): """ We should NOT be able to execute remove_vrf as read-only user """ v = VRF() v.id = 0 with self.assertRaises(NipapAuthorizationError): v.remove()
def add_vrf(self): """ Add a new VRF to NIPAP and return its data. """ v = VRF() if 'rt' in request.params: if request.params['rt'].strip() != '': v.rt = request.params['rt'].strip() if 'name' in request.params: if request.params['name'].strip() != '': v.name = request.params['name'].strip() if 'description' in request.params: v.description = request.params['description'] if 'tags' in request.params: v.tags = json.loads(request.params['tags']) if 'avps' in request.params: v.avps = json.loads(request.params['avps']) try: v.save() except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ })
def add_vrf(self): """ Add a new VRF to NIPAP and return its data. """ v = VRF() if 'rt' in request.json: v.rt = validate_string(request.json, 'rt') if 'name' in request.json: v.name = validate_string(request.json, 'name') if 'description' in request.json: v.description = validate_string(request.json, 'description') if 'tags' in request.json: v.tags = request.json['tags'] if 'avps' in request.json: v.avps = request.json['avps'] try: v.save() log.info('add_vrf (%s) %s' % (session['user'], request.json)) except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ })
def smart_search_vrf(self): """ Perform a smart VRF search. The "smart" search function tries extract a query from a text string. This query is then passed to the search_vrf function, which performs the search. """ search_options = {} extra_query = None if 'query_id' in request.params: search_options['query_id'] = request.params['query_id'] if 'max_result' in request.params: search_options['max_result'] = request.params['max_result'] if 'offset' in request.params: search_options['offset'] = request.params['offset'] if 'vrf_id' in request.params: extra_query = { 'val1': 'id', 'operator': 'equals', 'val2': request.params['vrf_id'] } try: result = VRF.smart_search(request.params['query_string'], search_options, extra_query ) except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
def test_search_vrf(self): """ We should be able to execute search_vrf as read-only user """ v = VRF.search({ 'val1': 'id', 'operator': 'equals', 'val2': 0 }) self.assertEqual(v['result'][0].id, 0)
def remove(self, id): """ Removes a VRF. """ v = VRF.get(int(id)) v.remove() redirect(url(controller='vrf', action='list'))
def list_vrf(self): """ List VRFs and return JSON encoded result. """ try: vrfs = VRF.list() except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
def edit_vrf(self, id): """ Edit a VRF. """ try: v = VRF.get(int(id)) except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
def remove(self, id): """ Removes a VRF. """ v = VRF.get(int(id)) v.remove() redirect(url(controller="vrf", action="list"))
def test_find_free_prefix(self): """ We should be able to execute find_free_prefix as read-only user """ v = VRF.get(0) p = Prefix.find_free(v, { 'from-prefix': ['1.3.3.0/24'], 'prefix_length': 27 })
def edit_prefix(self, id): """ Edit a prefix. """ try: p = Prefix.get(int(id)) # extract attributes if 'prefix' in request.json: p.prefix = validate_string(request.json, 'prefix') if 'type' in request.json: p.type = validate_string(request.json, 'type') if 'description' in request.json: p.description = validate_string(request.json, 'description') if 'expires' in request.json: p.expires = validate_string(request.json, 'expires') if 'comment' in request.json: p.comment = validate_string(request.json, 'comment') if 'node' in request.json: p.node = validate_string(request.json, 'node') if 'status' in request.json: p.status = validate_string(request.json, 'status') if 'pool' in request.json: if request.json['pool'] is None: p.pool = None else: try: p.pool = Pool.get(int(request.json['pool'])) except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__}) if 'alarm_priority' in request.json: p.alarm_priority = validate_string(request.json, 'alarm_priority') if 'monitor' in request.json: if request.json['monitor'] == 'true': p.monitor = True else: p.monitor = False if 'country' in request.json: p.country = validate_string(request.json, 'country') if 'order_id' in request.json: p.order_id = validate_string(request.json, 'order_id') if 'customer_id' in request.json: p.customer_id = validate_string(request.json, 'customer_id') if 'vrf' in request.json: try: if request.json['vrf'] is None or len(unicode(request.json['vrf'])) == 0: p.vrf = None else: p.vrf = VRF.get(int(request.json['vrf'])) except ValueError: return json.dumps({'error': 1, 'message': "Invalid VRF ID '%s'" % request.json['vrf']}) except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
def remove_vrf(self, id): """ Remove a VRF. """ try: vrf = VRF.get(int(id)) vrf.remove() except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
def edit(self, id): """ Edit a prefix. """ # find prefix c.prefix = Prefix.get(int(id)) # we got a HTTP POST - edit object if request.method == 'POST': c.prefix.prefix = request.params['prefix_prefix'] c.prefix.description = request.params['prefix_description'] if request.params['prefix_node'].strip() == '': c.prefix.node = None else: c.prefix.node = request.params['prefix_node'] if request.params['prefix_country'].strip() == '': c.prefix.country = None else: c.prefix.country = request.params['prefix_country'] if request.params['prefix_comment'].strip() == '': c.prefix.comment = None else: c.prefix.comment = request.params['prefix_comment'] if request.params['prefix_order_id'].strip() == '': c.prefix.order_id = None else: c.prefix.order_id = request.params['prefix_order_id'] if request.params['prefix_customer_id'].strip() == '': c.prefix.customer_id = None else: c.prefix.customer_id = request.params['prefix_customer_id'] if request.params['prefix_vrf'].strip() == '': c.prefix.vrf = None else: # TODO: handle non-existent VRF... c.prefix.vrf = VRF.list({ 'rt': request.params['prefix_vrf'] })[0] if request.params.get('prefix_monitor') is not None: c.prefix.monitor = True else: c.prefix.monitor = False c.prefix.alarm_priority = request.params['prefix_alarm_priority'] c.prefix.save() redirect(url(controller='prefix', action='list')) return render('/prefix_edit.html')
def edit(self, id): """ Edit a prefix. """ # find prefix c.prefix = Prefix.get(int(id)) # we got a HTTP POST - edit object if request.method == 'POST': c.prefix.prefix = request.params['prefix_prefix'] c.prefix.description = request.params['prefix_description'] if request.params['prefix_node'].strip() == '': c.prefix.node = None else: c.prefix.node = request.params['prefix_node'] if request.params['prefix_country'].strip() == '': c.prefix.country = None else: c.prefix.country = request.params['prefix_country'] if request.params['prefix_comment'].strip() == '': c.prefix.comment = None else: c.prefix.comment = request.params['prefix_comment'] if request.params['prefix_order_id'].strip() == '': c.prefix.order_id = None else: c.prefix.order_id = request.params['prefix_order_id'] if request.params['prefix_customer_id'].strip() == '': c.prefix.customer_id = None else: c.prefix.customer_id = request.params['prefix_customer_id'] if request.params['prefix_vrf'].strip() == '': c.prefix.vrf = None else: # TODO: handle non-existent VRF... c.prefix.vrf = VRF.list({'rt': request.params['prefix_vrf']})[0] if request.params.get('prefix_monitor') != None: c.prefix.monitor = True else: c.prefix.monitor = False c.prefix.alarm_priority = request.params['prefix_alarm_priority'] c.prefix.save() redirect(url(controller='prefix', action='list')) return render('/prefix_edit.html')
def add_vrf(self, name, rt, description, tags=[]): try: vrf = VRF() vrf.rt = rt vrf.name = name vrf.description = description vrf.tags = tags vrf.save() return vrf except NipapError as exc: print("Error: could not add vrf to NIPAP: %s" % str(exc)) return None
def edit_vrf(self, id): """ Edit a VRF. """ try: v = VRF.get(int(id)) except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ })
def list_vrf(self): """ List VRFs and return JSON encoded result. """ try: vrfs = VRF.list() except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ })
def test_stats2(self): """ Check stats are correct when adding and removing prefixes """ th = TestHelper() # add some top level prefixes to the default VRF p1 = th.add_prefix('1.0.0.0/24', 'reservation', 'test') p2 = th.add_prefix('1.0.0.128/25', 'assignment', 'test') p3 = th.add_prefix('2001:db8:1::/48', 'reservation', 'test') p4 = th.add_prefix('2001:db8:1:1::/64', 'reservation', 'test') # check stats for VRF res = VRF.get(0) # ipv4 self.assertEqual(2, res.num_prefixes_v4) self.assertEqual(256, res.total_addresses_v4) self.assertEqual(128, res.used_addresses_v4) self.assertEqual(128, res.free_addresses_v4) # ipv6 self.assertEqual(2, res.num_prefixes_v6) self.assertEqual(1208925819614629174706176, res.total_addresses_v6) self.assertEqual(18446744073709551616, res.used_addresses_v6) self.assertEqual(1208907372870555465154560, res.free_addresses_v6) # remove some prefixes p1.remove() p3.remove() # check stats for VRF res = VRF.get(0) # ipv4 self.assertEqual(1, res.num_prefixes_v4) self.assertEqual(128, res.total_addresses_v4) self.assertEqual(0, res.used_addresses_v4) self.assertEqual(128, res.free_addresses_v4) # ipv6 self.assertEqual(1, res.num_prefixes_v6) self.assertEqual(18446744073709551616, res.total_addresses_v6) self.assertEqual(0, res.used_addresses_v6) self.assertEqual(18446744073709551616, res.free_addresses_v6)
def test_stats1(self): """ Check stats are correct when adding and removing prefixes """ th = TestHelper() # add some top level prefixes to the default VRF p1 = th.add_prefix('1.0.0.0/24', 'reservation', 'test') p2 = th.add_prefix('2.0.0.0/24', 'reservation', 'test') p3 = th.add_prefix('2001:db8:1::/48', 'reservation', 'test') p4 = th.add_prefix('2001:db8:2::/48', 'reservation', 'test') # check stats for VRF res = VRF.get(0) # ipv4 self.assertEqual(2, res.num_prefixes_v4) self.assertEqual(512, res.total_addresses_v4) self.assertEqual(0, res.used_addresses_v4) self.assertEqual(512, res.free_addresses_v4) # ipv6 self.assertEqual(2, res.num_prefixes_v6) self.assertEqual(2417851639229258349412352, res.total_addresses_v6) self.assertEqual(0, res.used_addresses_v6) self.assertEqual(2417851639229258349412352, res.free_addresses_v6) # remove some prefixes p1.remove() p3.remove() # check stats for VRF res = VRF.get(0) # ipv4 self.assertEqual(1, res.num_prefixes_v4) self.assertEqual(256, res.total_addresses_v4) self.assertEqual(0, res.used_addresses_v4) self.assertEqual(256, res.free_addresses_v4) # ipv6 self.assertEqual(1, res.num_prefixes_v6) self.assertEqual(1208925819614629174706176, res.total_addresses_v6) self.assertEqual(0, res.used_addresses_v6) self.assertEqual(1208925819614629174706176, res.free_addresses_v6)
def test_load_data(self): """ """ th = TestHelper() p1 = th.add_prefix('192.168.0.0/16', 'reservation', 'test') p2 = th.add_prefix('192.168.0.0/20', 'reservation', 'test') p3 = th.add_prefix('192.168.0.0/24', 'reservation', 'test') p4 = th.add_prefix('192.168.1.0/24', 'reservation', 'test') p5 = th.add_prefix('192.168.2.0/24', 'reservation', 'test') p6 = th.add_prefix('192.168.32.0/20', 'reservation', 'test') p7 = th.add_prefix('192.168.32.0/24', 'reservation', 'test') p8 = th.add_prefix('192.168.32.1/32', 'reservation', 'test') ps1 = th.add_prefix('2001:db8:1::/48', 'reservation', 'test') ps2 = th.add_prefix('2001:db8:1::/64', 'reservation', 'test') ps3 = th.add_prefix('2001:db8:2::/48', 'reservation', 'test') pool1 = Pool() pool1.name = 'upgrade-test' pool1.ipv4_default_prefix_length = 31 pool1.ipv6_default_prefix_length = 112 pool1.save() p2.pool = pool1 p2.save() ps1.pool = pool1 ps1.save() pool2 = Pool() pool2.name = 'upgrade-test2' pool2.save() vrf1 = VRF() vrf1.name = 'foo' vrf1.rt = '123:123' vrf1.save()
def add_prefix(self): """ Add prefix according to the specification. The following keys can be used: vrf ID of VRF to place the prefix in prefix the prefix to add if already known family address family (4 or 6) description A short description expires Expiry time of assignment comment Longer comment node Hostname of node type Type of prefix; reservation, assignment, host status Status of prefix; assigned, reserved, quarantine pool ID of pool country Country where the prefix is used added Timestamp of added prefix last_modified Timestamp of last modify order_id Order identifier customer_id Customer identifier vlan VLAN ID alarm_priority Alarm priority of prefix monitor If the prefix should be monitored or not from-prefix A prefix the prefix is to be allocated from from-pool A pool (ID) the prefix is to be allocated from prefix_length Prefix length of allocated prefix """ p = Prefix() # Sanitize input parameters if 'vrf' in request.json: try: if request.json['vrf'] is None or len( unicode(request.json['vrf'])) == 0: p.vrf = None else: p.vrf = VRF.get(int(request.json['vrf'])) except ValueError: return json.dumps({ 'error': 1, 'message': "Invalid VRF ID '%s'" % request.json['vrf'] }) except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ })
def add_current_vrf(self): """ Add VRF to filter list session variable """ vrf_id = request.params.get('vrf_id') if vrf_id is not None: if vrf_id == 'null': vrf = VRF() else: vrf = VRF.get(int(vrf_id)) session['current_vrfs'][vrf_id] = { 'id': vrf.id, 'rt': vrf.rt, 'name': vrf.name, 'description': vrf.description } session.save() return json.dumps(session.get('current_vrfs', {}))
def test_add_vrf(self): """ We should NOT be able to execute add_vrf as read-only user """ v = VRF() v.rt = '123:456' v.name = 'test' with self.assertRaises(NipapAuthorizationError): v.save()
def remove_vrf(self, id): """ Remove a VRF. """ try: vrf = VRF.get(int(id)) vrf.remove() except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ })
def search_vrf(self, rt): """ This method wildcard searches a vrf, for example seaching for 209:123 will return 209:123, 209:123xxxx :param rt: :return: a vrf instance """ try: retVal = VRF.smart_search(rt) except: logging.debug("Exception search for vrf {0}".format(rt)) retVal = None return retVal
def find_vrf(self, property, value): """ Find an exact match for a VRF based on property such as rt "209:123", description :param property: :param value: :return: a VRF instance """ retVal = None try: retVal = VRF.search({'val1': property, 'operator': 'equals', 'val2': value})['result'][0] except (KeyError, IndexError): retVal = None return retVal
def remove_vrf(self, id): """ Remove a VRF. """ try: vrf = VRF.get(int(id)) name = vrf.name vrf.remove() log.info('remove_vrf (%s) %s' % (session['user'], name)) except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ })
def add_vrf(self): """ Add a new VRF to NIPAP and return its data. """ v = VRF() if 'rt' in request.json: v.rt = validate_string(request.json, 'rt') if 'name' in request.json: v.name = validate_string(request.json, 'name') if 'description' in request.json: v.description = validate_string(request.json, 'description') if 'tags' in request.json: v.tags = request.json['tags'] if 'avps' in request.json: v.avps = request.json['avps'] try: v.save() except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
def smart_search_vrf(self): """ Perform a smart VRF search. The "smart" search function tries extract a query from a text string. This query is then passed to the search_vrf function, which performs the search. """ search_options = {} extra_query = None if 'query_id' in request.json: search_options['query_id'] = request.json['query_id'] if 'max_result' in request.json: search_options['max_result'] = request.json['max_result'] if 'offset' in request.json: search_options['offset'] = request.json['offset'] if 'vrf_id' in request.json: extra_query = { 'val1': 'id', 'operator': 'equals', 'val2': request.json['vrf_id'] } try: result = VRF.smart_search(request.json['query_string'], search_options, extra_query) # Remove error key in result from backend as it interferes with the # error handling of the web interface. # TODO: Reevaluate how to deal with different types of errors; soft # errors like query string parser errors and hard errors like lost # database. del result['error'] except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ })
def add(self): """ Add a new VRF. """ c.action = 'add' if 'action' in request.params: if request.params['action'] == 'add': v = VRF() if request.params['rt'].strip() != '': v.rt = request.params['rt'] if request.params['name'].strip() != '': v.name = request.params['name'] v.description = request.params['description'] v.save() redirect(url(controller='vrf', action='list')) return render('/vrf_add.html')
def add_vrf(self): """ Add a new VRF to NIPAP and return its data. """ v = VRF() if 'rt' in request.params: if request.params['rt'].strip() != '': v.rt = request.params['rt'].strip() if 'name' in request.params: if request.params['name'].strip() != '': v.name = request.params['name'].strip() if 'description' in request.params: v.description = request.params['description'] if 'tags' in request.params: v.tags = json.loads(request.params['tags']) if 'avps' in request.params: v.avps = json.loads(request.params['avps']) try: v.save() except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
def add_prefix(self): """ Add prefix according to the specification. The following keys can be used: vrf ID of VRF to place the prefix in prefix the prefix to add if already known family address family (4 or 6) description A short description expires Expiry time of assignment comment Longer comment node Hostname of node type Type of prefix; reservation, assignment, host status Status of prefix; assigned, reserved, quarantine pool ID of pool country Country where the prefix is used order_id Order identifier customer_id Customer identifier vlan VLAN ID alarm_priority Alarm priority of prefix monitor If the prefix should be monitored or not from-prefix A prefix the prefix is to be allocated from from-pool A pool (ID) the prefix is to be allocated from prefix_length Prefix length of allocated prefix """ p = Prefix() # Sanitize input parameters if 'vrf' in request.json: try: if request.json['vrf'] is None or len(unicode(request.json['vrf'])) == 0: p.vrf = None else: p.vrf = VRF.get(int(request.json['vrf'])) except ValueError: return json.dumps({'error': 1, 'message': "Invalid VRF ID '%s'" % request.json['vrf']}) except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
def smart_search_vrf(self): """ Perform a smart VRF search. The "smart" search function tries extract a query from a text string. This query is then passed to the search_vrf function, which performs the search. """ search_options = {} extra_query = None if 'query_id' in request.json: search_options['query_id'] = request.json['query_id'] if 'max_result' in request.json: search_options['max_result'] = request.json['max_result'] if 'offset' in request.json: search_options['offset'] = request.json['offset'] if 'vrf_id' in request.json: extra_query = { 'val1': 'id', 'operator': 'equals', 'val2': request.json['vrf_id'] } try: result = VRF.smart_search(request.json['query_string'], search_options, extra_query ) # Remove error key in result from backend as it interferes with the # error handling of the web interface. # TODO: Reevaluate how to deal with different types of errors; soft # errors like query string parser errors and hard errors like lost # database. del result['error'] except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
def get_current_vrfs(self): """ Return VRF filter list from session variable Before returning list, make a search for all VRFs currently in the list to verify that they still exist. """ # Verify that all currently selected VRFs still exists cur_vrfs = session.get('current_vrfs', {}).items() if len(cur_vrfs) > 0: q = {'operator': 'equals', 'val1': 'id', 'val2': cur_vrfs[0][0]} if len(cur_vrfs) > 1: for vrf_id, vrf in cur_vrfs[1:]: q = { 'operator': 'or', 'val1': q, 'val2': { 'operator': 'equals', 'val1': 'id', 'val2': vrf_id } } res = VRF.search(q) session['current_vrfs'] = {} for vrf in res['result']: session['current_vrfs'][vrf.id] = { 'id': vrf.id, 'rt': vrf.rt, 'name': vrf.name, 'description': vrf.description } session.save() return json.dumps(session.get('current_vrfs', {}))
def get_current_vrfs(self): """ Return VRF filter list from session variable Before returning list, make a search for all VRFs currently in the list to verify that they still exist. """ # Verify that all currently selected VRFs still exists cur_vrfs = session.get('current_vrfs', {}).items() if len(cur_vrfs) > 0: q = { 'operator': 'equals', 'val1': 'id', 'val2': cur_vrfs[0][0] } if len(cur_vrfs) > 1: for vrf_id, vrf in cur_vrfs[1:]: q = { 'operator': 'or', 'val1': q, 'val2': { 'operator': 'equals', 'val1': 'id', 'val2': vrf_id } } res = VRF.search(q) session['current_vrfs'] = {} for vrf in res['result']: session['current_vrfs'][vrf.id] = { 'id': vrf.id, 'rt': vrf.rt, 'name': vrf.name, 'description': vrf.description } session.save() return json.dumps(session.get('current_vrfs', {}))
def edit(self, id): """ Edit a VRF """ c.action = 'edit' c.edit_vrf = VRF.get(int(id)) # Did we have any action passed to us? if 'action' in request.params: if request.params['action'] == 'edit': if request.params['rt'].strip() == '': c.edit_vrf.rt = None else: c.edit_vrf.rt = request.params['rt'].strip() if request.params['name'].strip() == '': c.edit_vrf.name = None else: c.edit_vrf.name = request.params['name'].strip() c.edit_vrf.description = request.params['description'] c.edit_vrf.save() return render('/vrf_edit.html')
def edit(self, id): """ Edit a VRF """ c.action = "edit" c.edit_vrf = VRF.get(int(id)) # Did we have any action passed to us? if "action" in request.params: if request.params["action"] == "edit": if request.params["rt"].strip() == "": c.edit_vrf.rt = None else: c.edit_vrf.rt = request.params["rt"].strip() if request.params["name"].strip() == "": c.edit_vrf.name = None else: c.edit_vrf.name = request.params["name"].strip() c.edit_vrf.description = request.params["description"] c.edit_vrf.save() return render("/vrf_edit.html")
def get_vrfs(self): """ Get the list of VRFs from nipap self.vrfs = { 1: { "label": "global [65001:1234]", "vrf": nipap.VRF }, .... } self.vrf_labels = { "global [65001:1234]": 1, "default": 2, ... } :return: """ self.lock.acquire() try: vrf_list = VRF.list() except Exception as e: self.lock.release() raise e # Populate `self.vrfs` and `self.vrf_labels` for vrf in vrf_list: self.vrfs[vrf.id] = { 'label': "%s [%s]" % (vrf.name, vrf.rt), 'vrf': vrf } # If there's no RT (such in global), don't display brackets label = "VRF %s [%s]" % (vrf.name, vrf.rt) if vrf.rt else "VRF %s" % vrf.name self.vrf_labels[label] = str(vrf.id) self.lock.release() return True if self.vrfs else False
def smart_search_vrf(self): """ Perform a smart VRF search. The "smart" search function tries extract a query from a text string. This query is then passed to the search_vrf function, which performs the search. """ search_options = {} extra_query = None if 'query_id' in request.params: search_options['query_id'] = request.params['query_id'] if 'max_result' in request.params: search_options['max_result'] = request.params['max_result'] if 'offset' in request.params: search_options['offset'] = request.params['offset'] if 'vrf_id' in request.params: extra_query = { 'val1': 'id', 'operator': 'equals', 'val2': request.params['vrf_id'] } try: result = VRF.smart_search(request.params['query_string'], search_options, extra_query) except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ })
def add(self): """ Add a new VRF. """ c.action = "add" if "action" in request.params: if request.params["action"] == "add": v = VRF() if request.params["rt"].strip() != "": v.rt = request.params["rt"] if request.params["name"].strip() != "": v.name = request.params["name"] v.description = request.params["description"] v.save() redirect(url(controller="vrf", action="list")) return render("/vrf_add.html")
def test_list_vrf(self): """ We should be able to execute list_vrf as read-only user """ v = VRF.get(0) self.assertEqual(v.id, 0)
def list(self): """ List VRFs. """ c.vrfs = VRF.list() return render("/vrf_list.html")
pynipap.xmlrpc_uri = xmlrpc_uri if args.clear_vrfs: remove_confirmed = args.force if not remove_confirmed: res = raw_input("Are you sure you want to remove all VRFs? Note how all" + " prefixes in these VRFs WILL BE DELETED. The default VRF" + " will NOT be deleted nor will it be emptied. [y/N]") if len(res) > 0 and res.lower()[0] == 'y': remove_confirmed = True else: print "Aborted" if remove_confirmed: print "Removing: ", for v in VRF.list(): if v.id == 0: continue v.remove() sys.stdout.write(".") sys.stdout.flush() print " done!" if args.clear_pools: remove_confirmed = args.force if not remove_confirmed: res = raw_input("Are you sure you want to remove all pools? [y/N]") if len(res) > 0 and res.lower()[0] == 'y': remove_confirmed = True else: print "Operation aborted."
def edit_prefix(self, id): """ Edit a prefix. """ try: p = Prefix.get(int(id)) # extract attributes if 'prefix' in request.json: p.prefix = validate_string(request.json, 'prefix') if 'type' in request.json: p.type = validate_string(request.json, 'type') if 'description' in request.json: p.description = validate_string(request.json, 'description') if 'expires' in request.json: p.expires = validate_string(request.json, 'expires') if 'comment' in request.json: p.comment = validate_string(request.json, 'comment') if 'node' in request.json: p.node = validate_string(request.json, 'node') if 'status' in request.json: p.status = validate_string(request.json, 'status') if 'pool' in request.json: if request.json['pool'] is None: p.pool = None else: try: p.pool = Pool.get(int(request.json['pool'])) except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ }) if 'alarm_priority' in request.json: p.alarm_priority = validate_string(request.json, 'alarm_priority') if 'monitor' in request.json: if request.json['monitor'] == 'true': p.monitor = True else: p.monitor = False if 'country' in request.json: p.country = validate_string(request.json, 'country') if 'order_id' in request.json: p.order_id = validate_string(request.json, 'order_id') if 'customer_id' in request.json: p.customer_id = validate_string(request.json, 'customer_id') if 'vrf' in request.json: try: if request.json['vrf'] is None or len( unicode(request.json['vrf'])) == 0: p.vrf = None else: p.vrf = VRF.get(int(request.json['vrf'])) except ValueError: return json.dumps({ 'error': 1, 'message': "Invalid VRF ID '%s'" % request.json['vrf'] }) except NipapError, e: return json.dumps({ 'error': 1, 'message': e.args, 'type': type(e).__name__ })
def test_smart_search_vrf(self): """ We should be able to execute smart_search_vrf as read-only user """ v = VRF.smart_search('default') self.assertEqual(v['result'][0].id, 0)
curs_pg_new.execute(sql_log, (None, None, None, None, None, p.name, p.id, ar['timestamp'], ar['username'], ar['authenticated_as'], ar['authoritative_source'], ar['full_name'], ar['description'])) print "done" # Create VRFs from Schemas print "Creating VRFs from Schemas... ", sql = "SELECT * FROM ip_net_schema" curs_pg_old.execute(sql) vrfs = {} s_vrfs = {} for r in curs_pg_old: if r['vrf'] is None: continue if re.match('\d+:\d+', r['vrf'].strip()): v = VRF() v.rt = r['vrf'].strip() v.name = r['name'].strip() try: v.save() except NipapError, e: print "ERR: %s" % str(e) vrfs[v.rt] = v s_vrfs[r['id']] = v print "done" # Create VRFs from prefixes print "Creating VRFs from Prefixes... ", sql = "SELECT DISTINCT(vrf) FROM ip_net_plan WHERE vrf IS NOT NULL" curs_pg_old.execute(sql)
pynipap.xmlrpc_uri = "http://*****:*****@localhost:1337" o = AuthOptions({"authoritative_source": "nipap"}) # set to a string if you want the customerid populated DEFAULT_CUSTOMER = None # set this to true if any of the network blocks # are unclean and are really assignments or hosts. # use with caution. detect_hosts = False # this is for network blocks that are misrepresented # it might not be needed for your network hosts = [] for vrf in VRF.list(): if vrf.id == 0: DEFAULT_VRF = vrf break else: raise ValueError("No default VRF") def export_tags(obj): def f(): for name, attr in obj["extattrs"].items(): # skip inherited attributes if attr.get("inheritance_source"): continue yield name, attr["value"]
ar['full_name'], ar['description'])) print "done" # Create VRFs from Schemas print "Creating VRFs from Schemas... ", sql = "SELECT * FROM ip_net_schema" curs_pg_old.execute(sql) vrfs = {} s_vrfs = {} for r in curs_pg_old: if r['vrf'] is None: continue if re.match('\d+:\d+', r['vrf'].strip()): v = VRF() v.rt = r['vrf'].strip() v.name = r['name'].strip() try: v.save() except NipapError, e: print "ERR: %s" % str(e) vrfs[v.rt] = v s_vrfs[r['id']] = v print "done" # Create VRFs from prefixes print "Creating VRFs from Prefixes... ", sql = "SELECT DISTINCT(vrf) FROM ip_net_plan WHERE vrf IS NOT NULL" curs_pg_old.execute(sql) for r in curs_pg_old:
def edit_prefix(self, id): """ Edit a prefix. """ try: p = Prefix.get(int(id)) # extract attributes if 'prefix' in request.params: p.prefix = request.params['prefix'] if 'type' in request.params: p.type = request.params['type'].strip() if 'description' in request.params: if request.params['description'].strip() == '': p.description = None else: p.description = request.params['description'].strip() if 'expires' in request.params: if request.params['expires'].strip() == '': p.expires = None else: p.expires = request.params['expires'].strip(' "') if 'comment' in request.params: if request.params['comment'].strip() == '': p.comment = None else: p.comment = request.params['comment'].strip() if 'node' in request.params: if request.params['node'].strip() == '': p.node = None else: p.node = request.params['node'].strip() if 'status' in request.params: p.status = request.params['status'].strip() if 'pool' in request.params: if request.params['pool'].strip() == '': p.pool = None else: try: p.pool = Pool.get(int(request.params['pool'])) except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__}) if 'alarm_priority' in request.params: p.alarm_priority = request.params['alarm_priority'].strip() if 'monitor' in request.params: if request.params['monitor'] == 'true': p.monitor = True else: p.monitor = False if 'country' in request.params: if request.params['country'].strip() == '': p.country = None else: p.country = request.params['country'].strip() if 'order_id' in request.params: if request.params['order_id'].strip() == '': p.order_id = None else: p.order_id = request.params['order_id'].strip() if 'customer_id' in request.params: if request.params['customer_id'].strip() == '': p.customer_id = None else: p.customer_id = request.params['customer_id'].strip() if 'vrf' in request.params: try: if request.params['vrf'] is None or len(request.params['vrf']) == 0: p.vrf = None else: p.vrf = VRF.get(int(request.params['vrf'])) except ValueError: return json.dumps({'error': 1, 'message': "Invalid VRF ID '%s'" % request.params['vrf']}) except NipapError, e: return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})