def _get_or_create_cdn(cls, cdn_id, create=True): try: return CDN.get(CDN.id == cdn_id), False except DoesNotExist: if create: return CDN.create(id=cdn_id), True return None, None
def get_cdns(context, request): page = request.params.get('page', 0) num_per_page = request.params.get('num_per_page', 5) if num_per_page <= 0: result = CDN.select() else: result = CDN.select().paginate(page, num_per_page) cdns = [serialize_cdn(cdn) for cdn in result] request.reply(cdns)
def _get_or_bootstrap_cdn(self, cdn_id): try: return CDN.get(CDN.id == cdn_id) except DoesNotExist: try: cdn_data = self.bootstrapper.lookup_cdn(id) cdn = CDN(**cdn_data) cdn.save(force_insert=True) return cdn except BootstrapError: raise DoesNotExist
def cdn_list(self): """ List CDNs registered with CacheBrowser """ cdns = CDN.select() for cdn in cdns: self.send_line("%*s: %s" % (15, cdn.id, ' '.join(cdn.addresses)))
def test_database(self): initialize_database(":memory:", reset=True) cdn1 = CDN.create(id="cdn1", name="Sample CDN", edge_server="1.2.3.4") cdn2 = CDN.create(id="cdn2", name="Sample CDN2", edge_server="1.2.3.5") host1 = Host.create(hostname="first.host", cdn=cdn1) host2 = Host.create(hostname="second.host", cdn=cdn2) host3 = Host.create(hostname="third.host", cdn=cdn1) self.assertEqual([host1, host2, host3], list(Host.select())) self.assertEqual([host1, host3], Host.select().where(Host.cdn == cdn1)) self.assertEqual([host1, host3], Host.select().join(CDN).where(CDN.id == 'cdn1')) self.assertEqual(host2, Host.get(Host.cdn == cdn2)) self.assertEqual([], CDN.select().where(CDN.id == "doesntexist")) with self.assertRaises(DoesNotExist): CDN.get(CDN.id == "doesntexist")
def add_cdn(context, request): cdn = CDN() cdn.id = request.params.get('id', '') cdn.name = request.params.get('name', '') cdn.edge_server = request.params.get('edge_server', True) cdn.save(force_insert=True) request.reply()
def _get_or_bootstrap_host(self, hostname): try: return Host.get(Host.hostname == hostname) except DoesNotExist: try: host_data = self.bootstrapper.lookup_host(hostname) except BootstrapError: raise DoesNotExist host = Host(**host_data) try: host.cdn = self._get_or_bootstrap_cdn(host.cdn_id) except DoesNotExist: host.cdn = CDN.create(id=host.cdn_id, valid=False) host.save(force_insert=True) return host
def test_bootstrap(self): self.bootstrapper.bootstrap('the.host') self.assertTrue(Host.select().where(Host.hostname == 'the.host').exists()) self.assertTrue(CDN.select().where(CDN.id == 'thecdn').exists())
def test_unavailable_host__host_in_db__available_cdn__cdn_in_db_with_no_edge(self): cdn = CDN.create(id="thecdn") Host.create(hostname="host.db", cdn=cdn) self.bootstrapper.bootstrap('host.db')
def test_unavailable_host__host_in_db__unavailable_cdn__cdn_in_db_with_no_edge(self): cdn = CDN.create(id="missingcdn") Host.create(hostname="host.db", cdn=cdn) with self.assertRaises(CDNNotAvailableError): self.bootstrapper.bootstrap('missingcdn.host')