def _list_zones(self): """ Requesting all the DNS zones under a specific cloud. """ from mist.api.dns.models import Zone # Fetch zones from libcloud connection. pr_zones = self._list_zones__fetch_zones() zones = [] new_zones = [] for pr_zone in pr_zones: try: zone = Zone.objects.get(cloud=self.cloud, zone_id=pr_zone.id) except Zone.DoesNotExist: log.info("Zone: %s/domain: %s not in the database, creating.", pr_zone.id, pr_zone.domain) zone = Zone(cloud=self.cloud, owner=self.cloud.owner, zone_id=pr_zone.id) new_zones.append(zone) zone.deleted = None zone.domain = pr_zone.domain zone.type = pr_zone.type zone.ttl = pr_zone.ttl zone.extra = pr_zone.extra try: zone.save() except me.ValidationError as exc: log.error("Error updating %s: %s", zone, exc.to_dict()) raise BadRequestError({ 'msg': str(exc), 'errors': exc.to_dict() }) except me.NotUniqueError as exc: log.error("Zone %s not unique error: %s", zone, exc) raise ZoneExistsError() zones.append(zone) self.cloud.owner.mapper.update(new_zones) # Delete any zones in the DB that were not returned by the provider # meaning they were deleted otherwise. Zone.objects( cloud=self.cloud, id__nin=[z.id for z in zones], deleted=None).update(set__deleted=datetime.datetime.utcnow()) # Format zone information. return zones
def list_zones(self): """ This is the public method to call when requesting all the DNS zones under a specific cloud. """ # TODO: Adding here for circular dependency issue. Need to fix this. from mist.api.dns.models import Zone # Fetch zones from libcloud connection. pr_zones = self._list_zones__fetch_zones() zones = [] new_zones = [] for pr_zone in pr_zones: # FIXME: We are using the zone_id and owner instead of the # cloud_id to search for existing zones because providers # allow access to the same zone from multiple clouds so # we can end up adding the same zone many times under # different clouds. try: zones_q = Zone.objects(owner=self.cloud.owner, zone_id=pr_zone.id, deleted=None) for zone in zones_q: if zone.cloud.ctl.provider == self.cloud.ctl.provider: break else: raise Zone.DoesNotExist except Zone.DoesNotExist: log.info("Zone: %s/domain: %s not in the database, creating.", pr_zone.id, pr_zone.domain) zone = Zone(cloud=self.cloud, owner=self.cloud.owner, zone_id=pr_zone.id) new_zones.append(zone) zone.domain = pr_zone.domain zone.type = pr_zone.type zone.ttl = pr_zone.ttl zone.extra = pr_zone.extra try: zone.save() except me.ValidationError as exc: log.error("Error updating %s: %s", zone, exc.to_dict()) raise BadRequestError({ 'msg': exc.message, 'errors': exc.to_dict() }) except me.NotUniqueError as exc: log.error("Zone %s not unique error: %s", zone, exc) raise ZoneExistsError() zones.append(zone) self.cloud.owner.mapper.update(new_zones) # Delete any zones in the DB that were not returned by the provider # meaning they were deleted otherwise. Zone.objects( cloud=self.cloud, id__nin=[z.id for z in zones], deleted=None).update(set__deleted=datetime.datetime.utcnow()) # Format zone information. return zones