def _deactivate(self, names, tag=None): for name in names: if self._domains.get(name): addrs = self._domains.remove(name, tag) if addrs: for addr in addrs: log.info('removed %s -> %s', name.idna(), addr)
def _deactivate(self, names, tag=None): for name in names: if self._domains.get(name): addrs = self._domains.remove(name, tag) if addrs: for addr in addrs: log.info('removed %s -> %s', name.idna(), addr)
def test_domain(self): """ test suffix domain functionality: """ reg = Registry(TEST_DOMAIN) self.assertIsNone( reg._mappings.get(PREFIX_NAME + TEST_KEY + "." + TEST_DOMAIN)) self.assertIsNone( reg._mappings.get(PREFIX_NAME + TEST_KEY2 + "." + TEST_DOMAIN)) log.info("Adding mapping %s => %s", TEST_KEY, TEST_NAMES) reg.add(PREFIX_NAME + TEST_KEY, TEST_NAMES) log.debug('_mapping %s' % reg._mappings) self.assertIsNotNone( reg._mappings.get(PREFIX_NAME + TEST_KEY + "." + TEST_DOMAIN)) self.assertIsNone( reg._mappings.get(PREFIX_NAME + TEST_KEY2 + "." + TEST_DOMAIN)) log.info("Removing mapping %s => %s", TEST_KEY, TEST_NAMES) reg.rename(TEST_KEY, TEST_KEY2) self.assertIsNone( reg._mappings.get(PREFIX_NAME + TEST_KEY + "." + TEST_DOMAIN)) self.assertIsNotNone( reg._mappings.get(PREFIX_NAME + TEST_KEY2 + "." + TEST_DOMAIN))
def activate(self, container): 'Activate all rules associated with this container' desc = self._desc(container) with self._lock: self._active[container.id] = container mapping = self._get_mapping_by_container(container) if mapping: log.info('setting %s as active' % desc) key, names = mapping.key, mapping.names self._activate(names, container.addr, tag=key)
def activate(self, container): 'Activate all rules associated with this container' desc = self._desc(container) with self._lock: self._active[container.id] = container mapping = self._get_mapping_by_container(container) if mapping: log.info('setting %s as active' % desc) key, names = mapping.key, mapping.names self._activate(names, container.addr, tag=key)
def deactivate(self, container): 'Deactivate all rules associated with this container' with self._lock: old_container = self._active.get(container.id) if old_container is None: return del self._active[container.id] # since this container is active, get the old address so we can log # exactly which names/addresses are being deactivated desc = self._desc(container) mapping = self._get_mapping_by_container(container) if mapping: log.info('setting %s as inactive' % desc) self._deactivate(mapping.names, tag=mapping.key)
def deactivate(self, container): 'Deactivate all rules associated with this container' with self._lock: old_container = self._active.get(container.id) if old_container is None: return del self._active[container.id] # since this container is active, get the old address so we can log # exactly which names/addresses are being deactivated desc = self._desc(container) mapping = self._get_mapping_by_container(container) if mapping: log.info('setting %s as inactive' % desc) self._deactivate(mapping.names, tag=mapping.key)
def test_registry_mappings(self): """ test add / remove functionality: this is to map a container name/id to a set of domain names """ reg = Registry() log.info("Adding mapping %s => %s", TEST_KEY, TEST_NAMES) reg.add(PREFIX_NAME + TEST_KEY, TEST_NAMES) self.assertIsNotNone(reg._mappings.get(PREFIX_NAME + TEST_KEY)) log.info("Removing mapping %s => %s", TEST_KEY, TEST_NAMES) reg.remove(PREFIX_NAME + TEST_KEY) self.assertIsNone(reg._mappings.get(PREFIX_NAME + TEST_KEY))
def test_registry_mappings_get(self): """ test get functionality: this is to get a list of dns domain mapped to the specified key (container id / name) """ reg = Registry() log.info("Removing mapping %s => %s", TEST_KEY, TEST_NAMES) names = reg.get(PREFIX_NAME + TEST_KEY) self.assertEqual([], names) log.info("Adding mapping %s => %s", TEST_KEY, TEST_NAMES) reg.add(PREFIX_NAME + TEST_KEY, TEST_NAMES) names = reg.get(PREFIX_NAME + TEST_KEY) self.assertEqual(TEST_NAMES, names)
def test_registry_resolve(self): """ test resolve method """ reg = Registry() log.info("Adding %s", TEST_ADDR) reg._activate(TEST_NAMES2, TEST_ADDR, TEST_KEY) for name in TEST_NAMES: addrs = reg.resolve(name) log.debug('resolve %s result %s', name, addrs) self.assertIsNotNone(addrs) self.assertEqual(addrs, [TEST_ADDR]) addrs = reg.resolve("foo.it") self.assertIsNone(addrs)
def test_registry_domains_static(self): """ test activate_static / deactivate_static functionality: this is to map a domain name to a particular address through the REST Api """ reg = Registry() for domainName in TEST_NAMES: self.assertIsNone(reg._domains.get(domainName)) log.info("Adding static %s", TEST_ADDR) reg.activate_static(TEST_NAMES2[0], TEST_ADDR) self.assertIsNotNone(reg._domains.get(TEST_NAMES[0])) log.info("Removing static %s", TEST_ADDR) reg.deactivate_static(TEST_NAMES2[0], TEST_ADDR) log.debug(reg.dump()) self.assertIsNone(reg._domains.get(TEST_NAMES[0]))
def test_mapping_rename(self): """ test rename functionality: this is to update mapping when a container change name """ reg = Registry() self.assertIsNone(reg._mappings.get(PREFIX_NAME + TEST_KEY)) self.assertIsNone(reg._mappings.get(PREFIX_NAME + TEST_KEY2)) log.info("Adding mapping %s => %s", TEST_KEY, TEST_NAMES) reg.add(PREFIX_NAME + TEST_KEY, TEST_NAMES) # log.debug('_mapping %s' % reg._mappings) self.assertIsNotNone(reg._mappings.get(PREFIX_NAME + TEST_KEY)) self.assertIsNone(reg._mappings.get(PREFIX_NAME + TEST_KEY2)) log.info("Removing mapping %s => %s", TEST_KEY, TEST_NAMES) reg.rename(TEST_KEY, TEST_KEY2) self.assertIsNone(reg._mappings.get(PREFIX_NAME + TEST_KEY)) self.assertIsNotNone(reg._mappings.get(PREFIX_NAME + TEST_KEY2))
def test_registry_domains(self): """ test activate / deactivate functionality: this is to map a domain name to a particular address you can inspect the DNS tree with print(reg.dump()) """ reg = Registry() for domainName in TEST_NAMES: self.assertIsNone(reg._domains.get(domainName)) log.info("Adding %s", TEST_ADDR) reg._activate(TEST_NAMES2, TEST_ADDR, TEST_KEY) for domainName in TEST_NAMES: self.assertIsNotNone(reg._domains.get(domainName)) log.info("Removing %s", TEST_ADDR) reg._deactivate(TEST_NAMES2, TEST_ADDR, TEST_KEY) log.debug(reg.dump()) for domainName in TEST_NAMES: log.debug("name %s", domainName) self.assertIsNone(reg._domains.get(domainName))
def test_registry_active(self): """ test activate / deactivate functionality: this is to activate / deactivate all mapping related to the specified container """ reg = Registry() foo = object() foo.id = TEST_ID foo.name = TEST_NAME foo.addrs = TEST_ADDRS container = Container(foo) self.assertIsNone(reg._active.get(container.id)) log.info("Activating container %s", container.name) reg.activate(container) self.assertIsNotNone(reg._active.get(container.id)) log.info("Deactivating container %s", container.name) reg.deactivate(container) self.assertIsNone(reg._active.get(container.id))
def _activate(self, names, addr, tag=None): for name in names: self._domains.put(name, addr, tag) log.info('added %s -> %s key=%s', name.idna(), addr, tag)
def _activate(self, names, addr, tag=None): for name in names: self._domains.put(name, addr, tag) log.info('added %s -> %s key=%s', name.idna(), addr, tag)