def add_default_records(self, endpoint=None): # sanity check if not self.domain_id: raise Exception("Cannot add default records when domain_id is not set") # don't duplicate SOA record try: existing_soa = Record.get(Record.domain_id == self.domain_id, Record.type == RecordType().set("SOA")) except DoesNotExist: # create SOA record try: default_soa = DefaultRecord.get(DefaultRecord.type == RecordType().set("SOA")) soa = Record() soa.domain_id = self.domain_id soa.host = default_soa.host.replace("DOMAIN", self.domain) soa.val = default_soa.val soa.ttl = default_soa.ttl soa.type = default_soa.type # replace uses of DOMAIN soa.save() if endpoint is not None: endpoint.dns_log(soa.domain_id, "added soa") except DoesNotExist: # no default SOA record set! pass # create all other records default_records = DefaultRecord.select().where(DefaultRecord.type != RecordType().set("SOA")) for record in default_records: new = Record() new.domain_id = self.domain_id new.distance = record.distance new.host = record.host.replace("DOMAIN", self.domain) new.val = record.val.replace("DOMAIN", self.domain) new.distance = record.distance new.port = record.port new.ttl = record.ttl new.type = record.type new.weight = record.weight new.save() if endpoint is not None: endpoint.dns_log( new.domain_id, ("added " + RecordType().get(new.type) + " with host " + new.host + " and value " + new.val), )
def test_soa_record(self): domain = Domain() domain.domain = 'example.com' model = Record() # joined domain on domain_id model.domain_id = domain model.type = 'S' model.host = 'hostmaster.example.com:ns1.example.com' model.val = '16384:2048:1048576:2560:' model.ttl = '86400' expected = ( "Zexample.com" ":ns1.example.com" ":hostmaster.example.com" ":" ":16384" ":2048" ":1048576" ":2560" ":86400\n" ) self.assertEquals( self.export.data_line_from_model(model), expected )
def add_default_soa_record(self, endpoint=None): """Returns None if already exists, DoesNotExist bubbles up when no default soa exists""" # sanity check if not self.domain_id: raise Exception( "Cannot add default records when domain_id is not set") # don't duplicate SOA record try: existing_soa = Record.get(Record.domain_id == self.domain_id, Record.type == RecordType().set("SOA")) return None except DoesNotExist: # create SOA record, let DoesNotExist bubble up default_soa = DefaultRecord.get( DefaultRecord.type == RecordType().set("SOA")) soa = Record() soa.domain_id = self.domain_id soa.host = default_soa.host.replace("DOMAIN", self.domain) soa.val = default_soa.val soa.ttl = default_soa.ttl soa.type = default_soa.type # replace uses of DOMAIN soa.save() if endpoint is not None: endpoint.dns_log(soa.domain_id, "added soa") return soa
def test_cname_validation_success(self): record = Record() record.type = 'C' record.domain_id = 1 record.host = 'foobar.com' record.val = 'www.example.com' self.assertIsNone(record.validate())
def test_cname_validation_fail(self): record = Record() record.type = 'C' record.domain_id = 1 record.host = 'foobar.com' record.val = 'www.example.com ' with self.assertRaises(RecordValueException) as cm: record.validate() self.assertEquals('Invalid cname value: www.example.com ', cm.exception.message)
def test_cname_validation_fail(self): record = Record() record.type = 'C' record.domain_id = 1 record.host = 'foobar.com' record.val = 'www.example.com ' with self.assertRaises(RecordValueException) as cm: record.validate() self.assertEquals( 'Invalid cname value: www.example.com ', cm.exception.message )
def test_soa_record(self): domain = Domain() domain.domain = 'example.com' model = Record() # joined domain on domain_id model.domain_id = domain model.type = 'S' model.host = 'hostmaster.example.com:ns1.example.com' model.val = '16384:2048:1048576:2560:' model.ttl = '86400' expected = ("Zexample.com" ":ns1.example.com" ":hostmaster.example.com" ":" ":16384" ":2048" ":1048576" ":2560" ":86400\n") self.assertEquals(self.export.data_line_from_model(model), expected)
def add_default_records(self, endpoint=None, skipSoa=False): # sanity check if not self.domain_id: raise Exception( "Cannot add default records when domain_id is not set") if not skipSoa: try: soa = self.add_default_soa_record(endpoint) except DoesNotExist: # no default SOA record set! pass # create all other records default_records = DefaultRecord.select().where( DefaultRecord.type != RecordType().set("SOA")) for record in default_records: new = Record() new.domain_id = self.domain_id new.distance = record.distance new.host = record.host.replace("DOMAIN", self.domain) new.val = record.val.replace("DOMAIN", self.domain) new.distance = record.distance new.port = record.port new.ttl = record.ttl new.type = record.type new.weight = record.weight new.save() if endpoint is not None: endpoint.dns_log( new.domain_id, ("added " + RecordType().get(new.type) + " with host " + new.host + " and value " + new.val))