def test_create_record(cloud): """ Test function for creating a DNS record """ global __zone_id__ global __record_id__ global __num_records__ zone = Zone.objects.get(owner=cloud.owner, id=__zone_id__) kwargs = {} kwargs['name'] = 'blog.test.io' kwargs['type'] = 'A' kwargs['data'] = '1.2.3.4' kwargs['ttl'] = 172800 print "**** Create type %s DNS record with name %s" % \ (kwargs['type'], kwargs['name']) record = Record.add(owner=cloud.owner, **kwargs) print "**** DNS record created succesfully" __record_id__ = record.id __num_records__ = 1 print "Num records %d" % __num_records__
def test_delete_record(cloud): """ Testing deleting a particular records from a DNS zone """ global __zone_id__ global __record_id__ # zone = Zone.objects.get(owner=cloud.owner, id=__zone_id__) # record = Record.objects.get(id=__record_id__) # try: # record.ctl.delete_record() # except Exception: # print "can't delete record: %s" % record.record_id # print "**** Record deleted successfully" zones = Zone.objects(owner=cloud.owner) print "We got %d zones" % len(zones) for zone in zones: records = Record.objects(zone=zone, type='A') for record in records: try: record.ctl.delete_record() except Exception: print "can't delete record: %s" % record.record_id print "**** Record deleted successfully"
def list_records(self, zone): """ Public method to return a list of records under a specific zone. """ # Fetch records from libcloud connection. pr_records = self._list_records__fetch_records(zone.zone_id) # TODO: Adding here for circular dependency issue. Need to fix this. from mist.api.dns.models import Record, RECORDS records = [] new_records = [] for pr_record in pr_records: dns_cls = RECORDS[pr_record.type] try: record = Record.objects.get(zone=zone, record_id=pr_record.id, deleted=None) except Record.DoesNotExist: log.info("Record: %s not in the database, creating.", pr_record.id) if pr_record.type not in RECORDS: log.error("Unsupported record type '%s'", pr_record.type) continue record = dns_cls(record_id=pr_record.id, zone=zone) new_records.append(record) # We need to check if any of the information returned by the # provider is different than what we have in the DB record.name = pr_record.name or "" record.type = pr_record.type record.ttl = pr_record.ttl record.extra = pr_record.extra self._list_records__postparse_data(pr_record, record) try: record.save() except me.ValidationError as exc: log.error("Error updating %s: %s", record, exc.to_dict()) raise BadRequestError({ 'msg': exc.message, 'errors': exc.to_dict() }) except me.NotUniqueError as exc: log.error("Record %s not unique error: %s", record, exc) raise RecordExistsError() # There's a chance that we have received duplicate records as for # example for Route NS records, so skip adding it to the list if we # already have it for rec in records: if rec.record_id == record.record_id: records.remove(rec) break records.append(record) self.cloud.owner.mapper.update(new_records) # Then delete any records that are in the DB for this zone but were not # returned by the list_records() method meaning the were deleted in the # DNS provider. Record.objects( zone=zone, id__nin=[r.id for r in records], deleted=None).update(set__deleted=datetime.datetime.utcnow()) # Format zone information. return records