def test_integration1(self): create_fake_zone("wee.wee.mozilla.com", "") res, error = compile_to_django("wee.wee.mozilla.com") self.assertFalse(error) self.assertEqual(len(res['SOA']), 1) self.assertEqual(len(res['NS']), 1) self.assertEqual(len(res['DOMAIN']), 1) create_fake_zone("wee1.wee.mozilla.com", "") res, error = compile_to_django("wee1.wee.mozilla.com") self.assertFalse(error) self.assertEqual(len(res['SOA']), 1) self.assertEqual(len(res['NS']), 1) self.assertEqual(len(res['DOMAIN']), 1) res, error = compile_to_django("wee1.wee.mozilla.com OR " "wee.wee.mozilla.com") self.assertFalse(error) self.assertEqual(len(res['SOA']), 2) self.assertEqual(len(res['NS']), 2) self.assertEqual(len(res['DOMAIN']), 2) res, error = compile_to_django("wee1.wee.mozilla.com type=:SOA") self.assertFalse(error) self.assertEqual(len(res['SOA']), 1) self.assertEqual(len(res['NS']), 0) self.assertEqual(len(res['DOMAIN']), 0) res, error = compile_to_django( "wee1.wee.mozilla.com type=:NS OR " "wee.wee.mozilla.com type=:DOMAIN") self.assertFalse(error) self.assertEqual(len(res['SOA']), 0) self.assertEqual(len(res['NS']), 1) self.assertEqual(len(res['DOMAIN']), 1)
def test_integration5_ip(self): root_domain = create_fake_zone("wee5.wee.mozilla.com", "") create_fake_zone("10.in-addr.arpa", "") res, error = compile_to_django("10.in-addr.arpa OR " "wee5.wee.mozilla.com") self.assertFalse(error) self.assertEqual(len(res['SOA']), 2) self.assertEqual(len(res['NS']), 2) self.assertEqual(len(res['DOMAIN']), 2) ptr = PTR(name="host1.wee2.wee.mozilla.com", ip_str="10.0.0.1", ip_type="4") ptr.save() addr = AddressRecord(label="host1", domain=root_domain, ip_str="10.0.0.1", ip_type="4") addr.save() res, error = compile_to_django(ptr.ip_str) self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 1) res, error = compile_to_django("10.0.0.2") self.assertFalse(error) self.assertEqual(len(res['PTR']), 0) self.assertEqual(len(res['A']), 0) res, error = compile_to_django("ip=:10.0.0.1") self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 1)
def test_integration4_ip(self): create_fake_zone("wee3.wee.mozilla.com", "") create_fake_zone("1.2.ip6.arpa", "") res, error = compile_to_django("1.2.ip6.arpa") self.assertFalse(error) self.assertEqual(len(res['SOA']), 1) self.assertEqual(len(res['NS']), 1) self.assertEqual(len(res['DOMAIN']), 1) ptr = PTR(name="host1.wee2.wee.mozilla.com", ip_str="2111:0::", ip_type="6") ptr.save() res, error = compile_to_django(ptr.ip_str) self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 0) res, error = compile_to_django("2111:0:0::") self.assertFalse(error) self.assertEqual(len(res['PTR']), 0) self.assertEqual(len(res['A']), 0) res, error = compile_to_django("ip=:2111:0:0::") self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 0) res, error = compile_to_django("ip=:2111:1:0::") self.assertFalse(error) self.assertEqual(len(res['PTR']), 0) self.assertEqual(len(res['A']), 0)
def test_system_field_search_notes_fuzzy(self): res, error = compile_to_django( 'sys.notes~bar'.format() ) self.assertFalse(error) self.assertEqual(1, res['SYS'].count()) self.assertEqual(self.hostname, res['SYS'][0].hostname)
def test_system_field_search_null_system_rack(self): res, error = compile_to_django( 'sys.system_rack=Null' ) self.assertFalse(error) self.assertEqual(1, res['SYS'].count()) self.assertEqual(self.hostname, res['SYS'][0].hostname)
def test_system_field_search_hostname(self): res, error = compile_to_django( "sys.hostname={0}".format(self.hostname) ) self.assertFalse(error) self.assertEqual(1, res['SYS'].count()) self.assertEqual(self.hostname, res['SYS'][0].hostname)
def _search(request, response): search = request_to_search(request) errors = handle_shady_search(search) if errors: return errors obj_map, error_resp = compile_to_django(search) if not obj_map: return HttpResponse(response(**{'error_messages': error_resp})) obj_counts = {} total_objects = 0 try: # We might have to catch shitty regular expressions for type_, q in obj_map.iteritems(): obj_counts[type_] = q.count() if q else 0 total_objects += obj_counts[type_] except OperationalError as why: return HttpResponse(response(**{'error_messages': str(why)})) results = { 'meta': { 'counts': obj_counts, 'total_objects': total_objects, 'search': search, }, 'objects': obj_map } return HttpResponse(response(**results))
def test_integration5_ip(self): root_domain = create_fake_zone("wee5.wee.mozilla.com", "") create_fake_zone("10.in-addr.arpa", "") res, error = compile_to_django("10.in-addr.arpa OR " "wee5.wee.mozilla.com") self.assertFalse(error) self.assertEqual(len(res['SOA']), 2) self.assertEqual(len(res['NS']), 2) self.assertEqual(len(res['DOMAIN']), 2) PTR.objects.create(name="host1.wee2.wee.mozilla.com", ip_str="10.0.0.1", ip_type="4") AddressRecord.objects.create(label="host1", domain=root_domain, ip_str="10.0.0.1", ip_type="4") PTR.objects.create(name="host2.wee2.wee.mozilla.com", ip_str="10.0.0.5", ip_type="4") AddressRecord.objects.create(label="host2", domain=root_domain, ip_str="10.0.0.5", ip_type="4") vlan1 = Vlan.objects.create(name='vlan-foo', number=1) vlan2 = Vlan.objects.create(name='vlan-foo', number=2) site1 = Site.objects.create(full_name='foo-site') site2 = Site.objects.create(full_name='bar-site') Network.objects.create(network_str='10.0.0.0/30', ip_type='4', vlan=vlan1, site=site1) Network.objects.create(network_str='10.0.0.4/30', ip_type='4', vlan=vlan2, site=site2) # Don't associate networks and vlans yet res, error = compile_to_django('ip=:10.0.0.5') self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 1) self.assertEqual(len(res['NET']), 1) self.assertEqual(len(res['SITE']), 1) self.assertEqual(len(res['VLAN']), 1)
def test_integration5_ip(self): root_domain = create_fake_zone("wee5.wee.mozilla.com", "") create_fake_zone("10.in-addr.arpa", "") res, error = compile_to_django("10.in-addr.arpa OR " "wee5.wee.mozilla.com") self.assertFalse(error) self.assertEqual(len(res['SOA']), 2) self.assertEqual(len(res['NS']), 2) self.assertEqual(len(res['DOMAIN']), 2) PTR.objects.create( name="host1.wee2.wee.mozilla.com", ip_str="10.0.0.1", ip_type="4" ) AddressRecord.objects.create( label="host1", domain=root_domain, ip_str="10.0.0.1", ip_type="4" ) PTR.objects.create( name="host2.wee2.wee.mozilla.com", ip_str="10.0.0.5", ip_type="4" ) AddressRecord.objects.create( label="host2", domain=root_domain, ip_str="10.0.0.5", ip_type="4" ) vlan1 = Vlan.objects.create(name='vlan-foo', number=1) vlan2 = Vlan.objects.create(name='vlan-foo', number=2) site1 = Site.objects.create(full_name='foo-site') site2 = Site.objects.create(full_name='bar-site') Network.objects.create( network_str='10.0.0.0/30', ip_type='4', vlan=vlan1, site=site1 ) Network.objects.create( network_str='10.0.0.4/30', ip_type='4', vlan=vlan2, site=site2 ) # Don't associate networks and vlans yet res, error = compile_to_django('ip=:10.0.0.5') self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 1) self.assertEqual(len(res['NET']), 1) self.assertEqual(len(res['SITE']), 1) self.assertEqual(len(res['VLAN']), 1)
def test_integration3_zone(self): root_domain = create_fake_zone("wee3.wee.mozilla.com", "") res, error = compile_to_django("zone=:wee3.wee.mozilla.com") self.assertFalse(error) self.assertEqual(len(res['SOA']), 1) self.assertEqual(len(res['NS']), 1) cn = CNAME(label="host1", domain=root_domain, target="whop.whop") cn.save() res, error = compile_to_django("zone=:wee3.wee.mozilla.com host1") self.assertFalse(error) self.assertEqual(len(res['SOA']), 0) self.assertEqual(len(res['NS']), 0) self.assertEqual(len(res['CNAME']), 1) res, error = compile_to_django("zone=:wee3.wee.mozilla.com " "type=:CNAME") self.assertFalse(error) self.assertEqual(len(res['SOA']), 0) self.assertEqual(len(res['NS']), 0) self.assertEqual(len(res['CNAME']), 1)
def mark_systems(search, key, value, extra=None): print "" print "-----------------------------------" print "Systems getting Key: {0} Value: {1}".format(key, value) systems = compile_to_django(search)[0]['SYS'].filter(~Q( system_status=decomm)) for s in systems: if extra: extra(s, key, value) else: print s.hostname system_map[s.pk] = True KeyValue.objects.get_or_create(obj=s, key=key, value=value)
def mark_systems(search, key, value, extra=None): print "" print "-----------------------------------" print "Systems getting Key: {0} Value: {1}".format(key, value) systems = compile_to_django(search)[0]['SYS'].filter( ~Q(system_status=decomm) ) for s in systems: if extra: extra(s, key, value) else: print s.hostname system_map[s.pk] = True KeyValue.objects.get_or_create(obj=s, key=key, value=value)
def ajax_csv_exporter(request): search = request.GET.get('search', None) if not search: return HttpResponse('What do you want?!?', status=400) obj_map, error_resp = compile_to_django(search) if not obj_map: return HttpResponse( json.dumps({'error_messages': error_resp}), status=400 ) attr_ignore = ('notes', 'licenses') systems = obj_map['SYS'] system_fields = [ field.name for field in System._meta.fields if field.name not in attr_ignore ] queue = cStringIO.StringIO() queue.write(','.join(system_fields) + '\n') out = csv.writer( queue, dialect='excel', lineterminator='\n' ) try: for system in systems: row = [] for field in system_fields: value = getattr(system, field) if field == 'licenses': value = re.escape(value) row.append(str(value)) out.writerow(row) except DatabaseError as why: return HttpResponse( json.dumps({'error_messages': str(why)}), status=400 ) queue.seek(0) return HttpResponse(json.dumps({'csv_content': queue.readlines()}))
def delete_zone_helper(domain_name): if not domain_name: return { 'success': False, 'message': 'Which zone do you want to delete?' } if domain_name in ('mozilla.com', 'mozilla.net', 'mozilla.org', 'allizom.org'): raise ValidationError('Go home.') zone_objs = compile_to_django("zone=:{0}".format(domain_name)) rdtypes = ('CNAME', 'MX', 'A', 'SRV', 'PTR', 'SSHFP', 'NS', 'TXT') for rdtype in rdtypes: zone_objs[0][rdtype].delete() soa = zone_objs[0]['SOA'][0] root_domain = soa.root_domain def maybe_delete_domain(d): domain_status = [] for cd in d.domain_set.all(): if cd.soa == soa: domain_status.append(maybe_delete_domain(cd)) else: domain_status.append(False) if reduce(lambda x, y: x and y, domain_status, True): d.delete() return True else: d.soa = None d.save() return False maybe_delete_domain(root_domain) # XXX Replace this when Inventory has celery Task.schedule_zone_rebuild(soa) soa.delete() return {'success': True, 'message': 'success'}
def delete_zone_helper(domain_name): if not domain_name: return { 'success': False, 'message': 'Which zone do you want to delete?' } if domain_name in ('mozilla.com', 'mozilla.net', 'mozilla.org', 'allizom.org'): raise ValidationError('Go home.') zone_objs = compile_to_django("zone=:{0}".format(domain_name)) rdtypes = ('CNAME', 'MX', 'A', 'SRV', 'PTR', 'SSHFP', 'NS', 'TXT', 'SREG') for rdtype in rdtypes: zone_objs[0][rdtype].delete() soa = zone_objs[0]['SOA'][0] root_domain = soa.root_domain def maybe_delete_domain(d): domain_status = [] for cd in d.domain_set.all(): if cd.soa == soa: domain_status.append(maybe_delete_domain(cd)) else: domain_status.append(False) if reduce(lambda x, y: x and y, domain_status, True): d.delete() return True else: d.soa = None d.save() return False maybe_delete_domain(root_domain) soa.delete() return {'success': True, 'message': 'success'}
def ajax_csv_exporter(request): search = request.GET.get('search', None) if not search: return HttpResponse('What do you want?!?', status=400) obj_map, error_resp = compile_to_django(search) if not obj_map: return HttpResponse(json.dumps({'error_messages': error_resp}), status=400) attr_ignore = ('notes', 'licenses') systems = obj_map['SYS'] system_fields = [ field.name for field in System._meta.fields if field.name not in attr_ignore ] queue = cStringIO.StringIO() queue.write(','.join(system_fields) + '\n') out = csv.writer(queue, dialect='excel', lineterminator='\n') try: for system in systems: row = [] for field in system_fields: value = getattr(system, field) if field == 'licenses': value = re.escape(value) row.append(str(value)) out.writerow(row) except DatabaseError as why: return HttpResponse(json.dumps({'error_messages': str(why)}), status=400) queue.seek(0) return HttpResponse(json.dumps({'csv_content': queue.readlines()}))
def _search(request, response): search = request_to_search(request) errors = handle_shady_search(search) if errors: return errors try: obj_map, error_resp = compile_to_django(search) except ValidationError as why: return HttpResponse(response(**{'error_messages': str(why)})) if not obj_map: return HttpResponse(response(**{'error_messages': error_resp})) obj_counts = {} total_objects = 0 try: # We might have to catch shitty regular expressions # or other things MySQL doesn't like for type_, q in obj_map.iteritems(): obj_counts[type_] = q.count() if q else 0 total_objects += obj_counts[type_] except DatabaseError as why: return HttpResponse(response(**{'error_messages': str(why)})) except Warning as why: return HttpResponse(response(**{'error_messages': str(why)})) format = request.GET.get('format', '') results = { 'format': format, 'meta': { 'counts': obj_counts, 'total_objects': total_objects, 'search': search, }, 'objects': obj_map } return HttpResponse(response(**results))
def test_integration2(self): root_domain = create_fake_zone("wee2.wee.mozilla.com", "") res, error = compile_to_django("wee2.wee.mozilla.com") self.assertFalse(error) self.assertEqual(len(res['SOA']), 1) self.assertEqual(len(res['NS']), 1) self.assertEqual(len(res['DOMAIN']), 1) create_fake_zone("1.1.ip6.arpa", "") res, error = compile_to_django("1.1.ip6.arpa") self.assertFalse(error) self.assertEqual(len(res['SOA']), 1) self.assertEqual(len(res['NS']), 1) self.assertEqual(len(res['DOMAIN']), 1) ptr = PTR(name="host1.wee2.wee.mozilla.com", ip_str="1111::", ip_type="6") ptr.save() addr = AddressRecord(label="host1", domain=root_domain, ip_str="11::", ip_type="6") addr.save() res, error = compile_to_django("host1.wee2.wee.mozilla.com") self.assertFalse(error) self.assertEqual(len(res['A']), 1) self.assertEqual(len(res['PTR']), 1) res, error = compile_to_django("host1.wee2.wee.mozilla.com type=:A") self.assertFalse(error) self.assertEqual(len(res['A']), 1) self.assertEqual(len(res['PTR']), 0) res, error = compile_to_django("host1.wee2.wee.mozilla.com type=:PTR") self.assertFalse(error) self.assertEqual(len(res['A']), 0) self.assertEqual(len(res['PTR']), 1) res, error = compile_to_django("host1.wee2.wee.mozilla.com type=:A " "type=:PTR") self.assertFalse(error) self.assertEqual(len(res['A']), 0) self.assertEqual(len(res['PTR']), 0)
def test_system_field_search_hostname(self): res, error = compile_to_django("sys.hostname={0}".format( self.hostname)) self.assertFalse(error) self.assertEqual(1, res['SYS'].count()) self.assertEqual(self.hostname, res['SYS'][0].hostname)
def test_bad_equality(self): res, error = compile_to_django("ip~10.0.0.1") self.assertTrue(error)
def test_system_field_search_null_system_rack(self): res, error = compile_to_django('sys.system_rack=Null') self.assertFalse(error) self.assertEqual(1, res['SYS'].count()) self.assertEqual(self.hostname, res['SYS'][0].hostname)
def test_integration5_ip(self): root_domain = create_fake_zone("wee5.wee.mozilla.com", "") create_fake_zone("10.in-addr.arpa", "") res, error = compile_to_django("10.in-addr.arpa OR " "wee5.wee.mozilla.com") self.assertFalse(error) self.assertEqual(len(res['SOA']), 2) self.assertEqual(len(res['NS']), 2) self.assertEqual(len(res['DOMAIN']), 2) PTR.objects.create(name="host1.wee2.wee.mozilla.com", ip_str="10.0.0.1", ip_type="4") AddressRecord.objects.create(label="host1", domain=root_domain, ip_str="10.0.0.1", ip_type="4") PTR.objects.create(name="host2.wee2.wee.mozilla.com", ip_str="10.0.0.5", ip_type="4") AddressRecord.objects.create(label="host2", domain=root_domain, ip_str="10.0.0.5", ip_type="4") vlan1 = Vlan.objects.create(name='vlan-foo', number=1) vlan2 = Vlan.objects.create(name='vlan-foo', number=2) net1 = Network.objects.create(network_str='10.0.0.0/30', ip_type='4') net2 = Network.objects.create(network_str='10.0.0.4/30', ip_type='4') # Don't associate networks and vlans yet res, error = compile_to_django('vlan=:vlan-foo') self.assertFalse(error) self.assertEqual(len(res['PTR']), 0) self.assertEqual(len(res['A']), 0) res, error = compile_to_django('vlan=:vlan-foo,1') self.assertFalse(error) self.assertEqual(len(res['PTR']), 0) self.assertEqual(len(res['A']), 0) res, error = compile_to_django('vlan=:1,vlan-foo') self.assertFalse(error) self.assertEqual(len(res['PTR']), 0) self.assertEqual(len(res['A']), 0) net1.vlan = vlan1 net1.save() net2.vlan = vlan2 net2.save() res, error = compile_to_django('vlan=:vlan-foo') self.assertFalse(error) self.assertEqual(len(res['PTR']), 2) self.assertEqual(len(res['A']), 2) res, error = compile_to_django('vlan=:vlan-foo,1') self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 1) res, error = compile_to_django('vlan=:1,vlan-foo') self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 1) res, error = compile_to_django('vlan=:vlan-foo,2') self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 1) res, error = compile_to_django('vlan=:2,vlan-foo') self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 1) res, error = compile_to_django('vlan=:1') self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 1) vlan2.name = 'vlan-bar' vlan2.save() res, error = compile_to_django('vlan=:vlan-foo') self.assertFalse(error) self.assertEqual(len(res['PTR']), 1) self.assertEqual(len(res['A']), 1)
def test_system_compile_inequality_search(self): res, error = compile_to_django( #'sys.warranty_end<2014-01-01 AND sys.warranty_start>=2014-01-01' 'sys.warranty_end<2014-01-01 AND sys.warranty_start>2014-01-01') self.assertFalse(error)
def test_system_compile_inequality_search(self): res, error = compile_to_django( #'sys.warranty_end<2014-01-01 AND sys.warranty_start>=2014-01-01' 'sys.warranty_end<2014-01-01 AND sys.warranty_start>2014-01-01' ) self.assertFalse(error)
def test_system_field_search_notes_fuzzy(self): res, error = compile_to_django('sys.notes~bar'.format()) self.assertFalse(error) self.assertEqual(1, res['SYS'].count()) self.assertEqual(self.hostname, res['SYS'][0].hostname)