def range_usage_ajax(request): start = request.GET.get('start', None) end = request.GET.get('end', None) start_ip_type, _ = resolve_ip_type(start) end_ip_type, _ = resolve_ip_type(end) errors = None if start_ip_type != end_ip_type or start_ip_type is None: errors = "Couldn't resolve ip_type" return render(request, 'range/range_usage_ajax.html', { 'errors': errors, }) rusage = range_usage(start, end, start_ip_type, get_objects=True) def translate_ip(ip_i, *args): return int_to_ip(ip_i, start_ip_type) return render( request, 'range/range_usage_ajax.html', { 'errors': errors, 'start': start, 'end': end, 'start_i': int_to_ip(start, start_ip_type), 'end_i': int_to_ip(end, end_ip_type), 'rusage': rusage, 'translate_ip': translate_ip })
def test1(self): ip_start = "10.0.0.10" ip_end = "10.0.0.19" ptr = PTR(name="foo.mz", ip_str="10.0.0.15", ip_type='4') ptr.full_clean() ptr.save() a = AddressRecord(label="foo", domain=self.domain, ip_str="10.0.0.16", ip_type='4') a.full_clean() a.save() istart, iend, ipf_q = start_end_filter(ip_start, ip_end, '4') addrs = list( AddressRecord.objects.filter(ipf_q).order_by('ip_lower').order_by( 'ip_upper')) ptrs = list( PTR.objects.filter(ipf_q).order_by('ip_lower').order_by( 'ip_upper')) sregs = list( StaticReg.objects.filter(ipf_q).order_by('ip_lower').order_by( 'ip_upper')) self.assertEqual(a.pk, addrs[0].pk) self.assertEqual(ptr.pk, ptrs[0].pk) self.assertFalse(sregs) range_details = range_usage(ip_start, ip_end, '4') self.assertEqual(8, range_details['unused']) self.assertEqual(2, range_details['used']) self.assertEqual([(int(istart), int(istart) + 4), (int(istart) + 7, int(istart) + 9)], range_details['free_ranges'])
def test1(self): ip_start = "10.0.0.10" ip_end = "10.0.0.19" ptr = PTR(name="foo.mz", ip_str="10.0.0.15", ip_type='4') ptr.full_clean() ptr.save() a = AddressRecord( label="foo", domain=self.domain, ip_str="10.0.0.16", ip_type='4') a.full_clean() a.save() istart, iend, ipf_q = start_end_filter(ip_start, ip_end, '4') addrs = list(AddressRecord.objects.filter(ipf_q). order_by('ip_lower').order_by('ip_upper')) ptrs = list(PTR.objects.filter(ipf_q). order_by('ip_lower').order_by('ip_upper')) intrs = list(StaticInterface.objects.filter(ipf_q). order_by('ip_lower').order_by('ip_upper')) self.assertEqual(a.pk, addrs[0].pk) self.assertEqual(ptr.pk, ptrs[0].pk) self.assertFalse(intrs) range_details = range_usage(ip_start, ip_end, '4') self.assertEqual(8, range_details['unused']) self.assertEqual(2, range_details['used']) self.assertEqual([(int(istart), int(istart) + 4), (int(istart) + 7, int(istart) + 9)], range_details['free_ranges'])
def range_usage_ajax(request): start = request.GET.get('start', None) end = request.GET.get('end', None) start_ip_type, _ = resolve_ip_type(start) end_ip_type, _ = resolve_ip_type(end) errors = None if start_ip_type != end_ip_type or start_ip_type is None: errors = "Couldn't resolve ip_type" return render(request, 'range/range_usage_ajax.html', { 'errors': errors, }) rusage = range_usage(start, end, start_ip_type, get_objects=True) def translate_ip(ip_i, *args): return int_to_ip(ip_i, start_ip_type) return render(request, 'range/range_usage_ajax.html', { 'errors': errors, 'start': start, 'end': end, 'start_i': int_to_ip(start, start_ip_type), 'end_i': int_to_ip(end, end_ip_type), 'rusage': rusage, 'translate_ip': translate_ip })
def test2(self): ip_start = "10.0.1.0" ip_end = "10.0.1.99" ptr = PTR(name="foo.mz", ip_str="10.0.1.3", ip_type="4") ptr.full_clean() ptr.save() a = AddressRecord(label="foo", domain=self.domain, ip_str="10.0.1.3", ip_type="4") a.full_clean() a.save() s = create_fake_host(hostname="foo.mozilla.com") s.save() sreg = StaticReg.objects.create(label="foo", domain=self.domain, ip_str="10.0.1.4", ip_type="4", system=s) istart, iend, ipf_q = start_end_filter(ip_start, ip_end, "4") addrs = list(AddressRecord.objects.filter(ipf_q).order_by("ip_lower").order_by("ip_upper")) ptrs = list(PTR.objects.filter(ipf_q).order_by("ip_lower").order_by("ip_upper")) sregs = list(StaticReg.objects.filter(ipf_q).order_by("ip_lower").order_by("ip_upper")) self.assertEqual(a.pk, addrs[0].pk) self.assertEqual(ptr.pk, ptrs[0].pk) self.assertEqual(sreg.pk, sregs[0].pk) range_details = range_usage(ip_start, ip_end, "4") self.assertEqual(98, range_details["unused"]) self.assertEqual(2, range_details["used"]) self.assertEqual( [(int(istart) + 0, int(istart) + 2), (int(istart) + 5, int(istart) + 99)], range_details["free_ranges"] )
def test0(self): ip_start = "10.0.0.0" ip_end = "10.0.0.9" ptr = PTR(name="foo.mz", ip_str="10.0.0.5", ip_type='4') ptr.full_clean() ptr.save() istart, iend, ipf_q = start_end_filter(ip_start, ip_end, '4') addrs = list( AddressRecord.objects.filter(ipf_q).order_by('ip_lower').order_by( 'ip_upper')) ptrs = list( PTR.objects.filter(ipf_q).order_by('ip_lower').order_by( 'ip_upper')) sregs = list( StaticReg.objects.filter(ipf_q).order_by('ip_lower').order_by( 'ip_upper')) self.assertFalse(addrs) self.assertEqual(ptr.pk, ptrs[0].pk) self.assertFalse(sregs) range_details = range_usage(ip_start, ip_end, '4') self.assertEqual(9, range_details['unused']) self.assertEqual(1, range_details['used']) self.assertEqual([(int(istart) + 0, int(istart) + 4), (int(istart) + 6, int(istart) + 9)], range_details['free_ranges'])
def test_start_end_the_same(self): # BUG 896662 ip_start = "10.0.0.0" ip_end = "10.0.0.0" PTR.objects.create(name="foo.mz", ip_str="10.0.0.0", ip_type="4") range_details = range_usage(ip_start, ip_end, "4") self.assertEqual(0, range_details["unused"]) self.assertEqual(1, range_details["used"])
def test_start_end_the_same(self): # BUG 896662 ip_start = "10.0.0.0" ip_end = "10.0.0.0" PTR.objects.create(name="foo.mz", ip_str="10.0.0.0", ip_type='4') range_details = range_usage(ip_start, ip_end, '4') self.assertEqual(0, range_details['unused']) self.assertEqual(1, range_details['used'])
def bulk_gather_vlan_pools(request): vlan_name = request.GET.get('vlan_name', None) vlan_number = request.GET.get('vlan_number', None) site_name = request.GET.get('site_name', None) ip_type = request.GET.get('ip_type', None) if not site_name: return HttpResponse(dumps({'errors': 'Site name was not provided'})) if not ip_type: return HttpResponse(dumps({'errors': 'IP type is required here.'})) if vlan_name and vlan_number: s = 'site=:{site_name} AND vlan=:{vlan_name},{vlan_number}'.format( site_name=site_name, vlan_name=vlan_name, vlan_number=vlan_number) elif vlan_name: s = 'site=:{site_name} AND vlan=:{vlan_name}'.format( site_name=site_name, vlan_name=vlan_name) elif vlan_number: s = 'site=:{site_name} AND vlan=:{vlan_number}'.format( site_name=site_name, vlan_number=vlan_number) else: return HttpResponse( dumps({'errors': 'Not enough vlan information was provided'})) q_map, errors = compile_to_q(s) if errors: return None, errors networks = Network.objects.filter(q_map['NET']).filter(ip_type=ip_type) if networks.count() > 1: return HttpResponse( dumps({ 'errors': "Using the search '{s}', too many networks were " "found. Please be more specific and specify a range.".format( s=s) })) if not networks.count(): return HttpResponse( dumps({ 'errors': "Using the search '{s}', no networks were " "found.".format(s=s) })) ranges = integrate_real_ranges(networks[0], calc_template_ranges(networks[0])) free_ranges = [] for r in ranges: if r['rtype'] == 'special purpose': continue free_ranges += range_usage(r['start'], r['end'], ip_type)['free_ranges'] return HttpResponse(dumps({'free_ranges': free_ranges}))
def test2(self): ip_start = "10.0.1.0" ip_end = "10.0.1.99" ptr = PTR(name="foo.mz", ip_str="10.0.1.3", ip_type='4') ptr.full_clean() ptr.save() a = AddressRecord(label="foo", domain=self.domain, ip_str="10.0.1.3", ip_type='4') a.full_clean() a.save() s = create_fake_host(hostname="foo.mozilla.com") s.save() sreg = StaticReg.objects.create(label="foo", domain=self.domain, ip_str="10.0.1.4", ip_type='4', system=s) istart, iend, ipf_q = start_end_filter(ip_start, ip_end, '4') addrs = list( AddressRecord.objects.filter(ipf_q).order_by('ip_lower').order_by( 'ip_upper')) ptrs = list( PTR.objects.filter(ipf_q).order_by('ip_lower').order_by( 'ip_upper')) sregs = list( StaticReg.objects.filter(ipf_q).order_by('ip_lower').order_by( 'ip_upper')) self.assertEqual(a.pk, addrs[0].pk) self.assertEqual(ptr.pk, ptrs[0].pk) self.assertEqual(sreg.pk, sregs[0].pk) range_details = range_usage(ip_start, ip_end, '4') self.assertEqual(98, range_details['unused']) self.assertEqual(2, range_details['used']) self.assertEqual([(int(istart) + 0, int(istart) + 2), (int(istart) + 5, int(istart) + 99)], range_details['free_ranges'])
def test0(self): ip_start = "10.0.0.0" ip_end = "10.0.0.9" ptr = PTR(name="foo.mz", ip_str="10.0.0.5", ip_type="4") ptr.full_clean() ptr.save() istart, iend, ipf_q = start_end_filter(ip_start, ip_end, "4") addrs = list(AddressRecord.objects.filter(ipf_q).order_by("ip_lower").order_by("ip_upper")) ptrs = list(PTR.objects.filter(ipf_q).order_by("ip_lower").order_by("ip_upper")) sregs = list(StaticReg.objects.filter(ipf_q).order_by("ip_lower").order_by("ip_upper")) self.assertFalse(addrs) self.assertEqual(ptr.pk, ptrs[0].pk) self.assertFalse(sregs) range_details = range_usage(ip_start, ip_end, "4") self.assertEqual(9, range_details["unused"]) self.assertEqual(1, range_details["used"]) self.assertEqual( [(int(istart) + 0, int(istart) + 4), (int(istart) + 6, int(istart) + 9)], range_details["free_ranges"] )
def range_usage_text(request): start = request.GET.get('start', None) end = request.GET.get('end', None) format = request.GET.get('format', 'human_readable') if not (start and end): return HttpResponse(json.dumps({ 'success': False, 'error_messages': 'Provide a start and end'})) get_objects = request.GET.get('get_objects', False) if start.find(':') > -1: ip_type = '6' else: ip_type = '4' try: usage_data = range_usage(start, end, ip_type, get_objects) except (ValidationError, ipaddr.AddressValueError), e: return HttpResponse( json.dumps({ 'error_messages': str(e), 'success': False }))
def test0(self): ip_start = "10.0.0.0" ip_end = "10.0.0.9" ptr = PTR(name="foo.mz", ip_str="10.0.0.5", ip_type='4') ptr.full_clean() ptr.save() istart, iend, ipf_q = start_end_filter(ip_start, ip_end, '4') addrs = list(AddressRecord.objects.filter(ipf_q). order_by('ip_lower').order_by('ip_upper')) ptrs = list(PTR.objects.filter(ipf_q). order_by('ip_lower').order_by('ip_upper')) intrs = list(StaticInterface.objects.filter(ipf_q). order_by('ip_lower').order_by('ip_upper')) self.assertFalse(addrs) self.assertEqual(ptr.pk, ptrs[0].pk) self.assertFalse(intrs) range_details = range_usage(ip_start, ip_end, '4') self.assertEqual(9, range_details['unused']) self.assertEqual(1, range_details['used']) self.assertEqual([(int(istart) + 0, int(istart) + 4), (int(istart) + 6, int(istart) + 9)], range_details['free_ranges'])
def test2(self): ip_start = "10.0.1.0" ip_end = "10.0.1.99" ptr = PTR(name="foo.mz", ip_str="10.0.1.3", ip_type='4') ptr.full_clean() ptr.save() a = AddressRecord(label="foo", domain=self.domain, ip_str="10.0.1.3", ip_type='4') a.full_clean() a.save() s = System() s.save() intr = StaticInterface( label="foo", domain=self.domain, ip_str="10.0.1.4", ip_type='4', system=s, mac="00:11:22:33:44:55") intr.full_clean() intr.save() istart, iend, ipf_q = start_end_filter(ip_start, ip_end, '4') addrs = list(AddressRecord.objects.filter(ipf_q). order_by('ip_lower').order_by('ip_upper')) ptrs = list(PTR.objects.filter(ipf_q). order_by('ip_lower').order_by('ip_upper')) intrs = list(StaticInterface.objects.filter(ipf_q). order_by('ip_lower').order_by('ip_upper')) self.assertEqual(a.pk, addrs[0].pk) self.assertEqual(ptr.pk, ptrs[0].pk) self.assertEqual(intr.pk, intrs[0].pk) range_details = range_usage(ip_start, ip_end, '4') self.assertEqual(98, range_details['unused']) self.assertEqual(2, range_details['used']) self.assertEqual([(int(istart) + 0, int(istart) + 2), (int(istart) + 5, int(istart) + 99)], range_details['free_ranges'])
def range_usage_text(request): start = request.GET.get('start', None) end = request.GET.get('end', None) format = request.GET.get('format', 'human_readable') if not (start and end): return HttpResponse( json.dumps({ 'success': False, 'error_messages': 'Provide a start and end' })) get_objects = request.GET.get('get_objects', False) if start.find(':') > -1: ip_type = '6' else: ip_type = '4' try: usage_data = range_usage(start, end, ip_type, get_objects) except (ValidationError, ipaddr.AddressValueError), e: return HttpResponse( json.dumps({ 'error_messages': str(e), 'success': False }))
def bulk_gather_vlan_pools(request): vlan_name = request.GET.get('vlan_name', None) vlan_number = request.GET.get('vlan_number', None) site_name = request.GET.get('site_name', None) ip_type = request.GET.get('ip_type', None) if not site_name: return HttpResponse(dumps({ 'errors': 'Site name was not provided' })) if not ip_type: return HttpResponse(dumps({ 'errors': 'IP type is required here.' })) if vlan_name and vlan_number: s = 'site=:{site_name} AND vlan=:{vlan_name},{vlan_number}'.format( site_name=site_name, vlan_name=vlan_name, vlan_number=vlan_number ) elif vlan_name: s = 'site=:{site_name} AND vlan=:{vlan_name}'.format( site_name=site_name, vlan_name=vlan_name ) elif vlan_number: s = 'site=:{site_name} AND vlan=:{vlan_number}'.format( site_name=site_name, vlan_number=vlan_number ) else: return HttpResponse(dumps({ 'errors': 'Not enough vlan information was provided' })) q_map, errors = compile_to_q(s) if errors: return None, errors networks = Network.objects.filter(q_map['NET']).filter(ip_type=ip_type) if networks.count() > 1: return HttpResponse(dumps({ 'errors': "Using the search '{s}', too many networks were " "found. Please be more specific and specify a range.".format(s=s) })) if not networks.count(): return HttpResponse(dumps({ 'errors': "Using the search '{s}', no networks were " "found.".format(s=s) })) ranges = integrate_real_ranges( networks[0], calc_template_ranges(networks[0]) ) free_ranges = [] for r in ranges: if r['rtype'] == 'special purpose': continue free_ranges += range_usage( r['start'], r['end'], ip_type )['free_ranges'] return HttpResponse(dumps({ 'free_ranges': free_ranges }))