def test_mixed_operations(self): with self.assertPdnsZoneUpdate(self.full_domain.name, self.ADDITIONAL_TEST_DATA), PDNSChangeTracker(): self._create_rr_sets(self.ADDITIONAL_TEST_DATA, self.full_domain) rr_sets = [ RRset.objects.get(type=type_, subname=subname) for (type_, subname, _) in self.ADDITIONAL_TEST_DATA.keys() ] with self.assertPdnsZoneUpdate(self.full_domain.name, rr_sets), PDNSChangeTracker(): for rr_set in rr_sets: rr_set.ttl = 1 rr_set.save() data = {} for key in [('A', '_asdf', 123), ('AAAA', '*', 100023), ('A', 'foo', 1010)]: data[key] = self.TEST_DATA[key].copy() with self.assertPdnsZoneUpdate(self.full_domain.name, data), PDNSChangeTracker(): data[('A', '_asdf', 123)].append('9.9.9.9') rr_set = RRset.objects.get(domain=self.full_domain, type='A', subname='_asdf') RR(content='9.9.9.9', rrset=rr_set).save() data[('AAAA', '*', 100023)].append('::9') rr_set = RRset.objects.get(domain=self.full_domain, type='AAAA', subname='*') RR(content='::9', rrset=rr_set).save() data[('A', 'foo', 1010)] = [] RRset.objects.get(domain=self.full_domain, type='A', subname='foo').delete()
def _set_all_record_contents(rrset: RRset, record_contents): """ Updates this RR set's resource records, discarding any old values. To do so, two large select queries and one query per changed (added or removed) resource record are needed. Changes are saved to the database immediately. :param rrset: the RRset at which we overwrite all RRs :param record_contents: set of strings """ # Remove RRs that we didn't see in the new list removed_rrs = rrset.records.exclude( content__in=record_contents) # one SELECT for rr in removed_rrs: rr.delete() # one DELETE query # Figure out which entries in record_contents have not changed unchanged_rrs = rrset.records.filter( content__in=record_contents) # one SELECT unchanged_content = [ unchanged_rr.content for unchanged_rr in unchanged_rrs ] added_content = filter(lambda c: c not in unchanged_content, record_contents) rrs = [RR(rrset=rrset, content=content) for content in added_content] RR.objects.bulk_create(rrs) # One INSERT
def create_rr_set(cls, domain, records, **kwargs): if isinstance(domain, str): domain = Domain.objects.get(name=domain) domain.save() rr_set = RRset(domain=domain, **kwargs) rr_set.save() for r in records: RR(content=r, rrset=rr_set).save() return rr_set
def api_do(self): rr_set = RRset( domain=Domain.objects.get(name=self.domain_name), type='NS', subname='', ttl=settings.DEFAULT_NS_TTL, ) rr_set.save() rrs = [RR(rrset=rr_set, content=ns) for ns in settings.DEFAULT_NS] RR.objects.bulk_create(rrs) # One INSERT
def _create_rr_sets(cls, data, domain): rr_sets = [] rrs = {} for (type_, subname, ttl), rr_contents in data.items(): rr_set = RRset(domain=domain, subname=subname, type=type_, ttl=ttl) rr_sets.append(rr_set) rrs[(type_, subname)] = this_rrs = [] rr_set.save() for content in rr_contents: rr = RR(content=content, rrset=rr_set) this_rrs.append(rr) rr.save() return rr_sets, rrs
def _sync_domain(domain): domain.rrset_set.all().delete() rrsets = [] rrs = [] for rrset_data in pdns.get_rrset_datas(domain): if rrset_data['type'] in RR_SET_TYPES_AUTOMATIC: continue records = rrset_data.pop('records') rrset = RRset(**rrset_data) rrsets.append(rrset) rrs.extend([RR(rrset=rrset, content=record) for record in records]) RRset.objects.bulk_create(rrsets) RR.objects.bulk_create(rrs)
def test_create_multiple_in_full_rr_set(self): with self.assertPdnsFullRRSetUpdate(), PDNSChangeTracker(): for content in self.ALT_CONTENT_VALUES: RR(content=content, rrset=self.full_rr_set).save()
def test_create_in_simple_rr_set(self): with self.assertPdnsSimpleRRSetUpdate(), PDNSChangeTracker(): RR(content=self.CONTENT_VALUES[1], rrset=self.simple_rr_set).save()
def test_create_in_empty_rr_set(self): with self.assertPdnsEmptyRRSetUpdate(), PDNSChangeTracker(): RR(content=self.CONTENT_VALUES[0], rrset=self.empty_rr_set).save()