def _delete_record(self, identifier=None, rtype=None, name=None, content=None): """ Delete record(s) matching the provided params. If there is no match, do nothing. """ ids = [] if identifier: ids.append(identifier) elif not identifier and rtype and name: records = self._list_records(rtype, name, content) if records: ids = [record["id"] for record in records] if ids: LOGGER.debug("delete_records: %s", ids) with localzone.manage(self.filename, self.origin, autosave=True) as zone: # TODO: Remove this monkeypatch once upstream Class is fixed. _patch_zone(zone) for hashid in ids: zone.remove_record(hashid) LOGGER.debug("delete_record: %s", hashid) return True
def list_records(self, type=None, name=None, content=None): """ Return a list of records matching the supplied params. If no params are provided, then return all zone records. If no records are found, return an empty list. """ if name: name = self._relative_name(name) if not type: type = "ANY" filter = {"rdtype": type, "name": name, "content": content} with localzone.manage(self.filename, self.origin, autosave=True) as z: records = z.find_record(**filter) # pylint: disable=no-member result = [] for record in records: rdict = { "type": record.rdtype, "name": self._full_name(record.name), "ttl": record.ttl, "content": record.content, "id": record.hashid, } if rdict["type"] == "TXT": rdict["content"] = rdict["content"].replace('"', "") result.append(rdict) logger.debug("list_records: %s", result) return result
def delete_record(self, identifier=None, type=None, name=None, content=None): """ Delete record(s) matching the provided params. If there is no match, do nothing. """ ids = [] if identifier: ids.append(identifier) elif not identifier and type and name: records = self.list_records(type, name, content) if len(records) > 0: ids = [record["id"] for record in records] if len(ids) > 0: logger.debug("delete_records: %s", ids) with localzone.manage(self.filename, self.origin, autosave=True) as z: for hashid in ids: z.remove_record(hashid) # pylint: disable=no-member logger.debug("delete_record: %s", hashid) return True
def _list_records(self, rtype=None, name=None, content=None): """ Return a list of records matching the supplied params. If no params are provided, then return all zone records. If no records are found, return an empty list. """ if name: name = self._relative_name(name) if not rtype: rtype = "ANY" filter_query = {"rdtype": rtype, "name": name, "content": content} with localzone.manage(self.filename, self.origin, autosave=True) as zone: # TODO: Remove this monkeypatch once upstream Class is fixed. _patch_zone(zone) records = zone.find_record(**filter_query) result = [] for record in records: rdict = { "type": record.rdtype, "name": self._full_name(record.name), "ttl": record.ttl, "content": record.content, "id": record.hashid, } if rdict["type"] == "TXT": rdict["content"] = rdict["content"].replace('"', "") result.append(rdict) LOGGER.debug("list_records: %s", result) return result
def test_zone_update_record(): with localzone.manage(ZONEFILE, ORIGIN) as z: record = z.update_record(HASHID, "192.0.2.100") assert record.hashid == "117e047a" assert record.name == "@" assert record.rdtype == "A" assert record.content == "192.0.2.100" assert record.ttl == 3600
def create_record(self, type, name, content): """ Create a resource record. If a record already exists with the same content, do nothing. """ result = False name = self._relative_name(name) ttl = None # TODO: shoud assert that this is an int if self.ttl: ttl = self.ttl with localzone.manage(self.filename, self.origin, autosave=True) as z: if z.add_record(name, type, content, ttl=ttl): # pylint: disable=no-member result = True logger.debug("create_record: %s", result) return result
def _create_record(self, rtype, name, content): """ Create a resource record. If a record already exists with the same content, do nothing. """ result = False name = self._relative_name(name) ttl = None # TODO: shoud assert that this is an int if self.ttl: ttl = self.ttl with localzone.manage(self.filename, self.origin, autosave=True) as zone: # TODO: Remove this monkeypatch once upstream Class is fixed. _patch_zone(zone) if zone.add_record(name, rtype, content, ttl=ttl): result = True LOGGER.debug("create_record: %s", result) return result
def update_record(self, identifier, type=None, name=None, content=None): """ Update a record. Returns `False` if no matching record is found. """ result = False # TODO: some providers allow content-based updates without supplying an # ID, and therefore `identifier` is here optional. If we don't receive # an ID, look it up. if not identifier and type and name: records = self.list_records(type, name) if len(records) == 1: identifier = records[0]["id"] if identifier and content: with localzone.manage(self.filename, self.origin, autosave=True) as z: if z.update_record(identifier, content): # pylint: disable=no-member result = True logger.debug("update_record: %s", result) return result
def _update_record(self, identifier, rtype=None, name=None, content=None): """ Update a record. Returns `False` if no matching record is found. """ result = False # TODO: some providers allow content-based updates without supplying an # ID, and therefore `identifier` is here optional. If we don't receive # an ID, look it up. if not identifier and rtype and name: records = self._list_records(rtype, name) if len(records) == 1: identifier = records[0]["id"] if identifier and content: with localzone.manage(self.filename, self.origin, autosave=True) as zone: # TODO: Remove this monkeypatch once upstream Class is fixed. _patch_zone(zone) if zone.update_record(identifier, content): result = True LOGGER.debug("update_record: %s", result) return result
def test_zone_remove_record_not_found(): with localzone.manage(ZONEFILE, ORIGIN) as z: with pytest.raises(KeyError): z.remove_record("deadbeef")
def test_zone_remove_record(): with localzone.manage(ZONEFILE, ORIGIN) as z: z.remove_record(HASHID) with pytest.raises(KeyError): z.get_record(HASHID)
def test_zone_add_record_no_content(): with localzone.manage(ZONEFILE, ORIGIN) as z: with pytest.raises((AttributeError, DNSSyntaxError)): z.add_record("test", "txt", None)
def test_zone_add_record_unknown_type(): with localzone.manage(ZONEFILE, ORIGIN) as z: with pytest.raises(UnknownRdatatype): z.add_record("test", "err", "testing")
def test_zone_add_record(): with localzone.manage(ZONEFILE, ORIGIN) as z: record = z.add_record("test", "txt", "testing") assert record.hashid == "28c9e108"
def test_zone_update_record_not_found(): with localzone.manage(ZONEFILE, ORIGIN) as z: with pytest.raises(KeyError): z.update_record("deadbeef", "eat mor chikin")
def test_manage(): with localzone.manage(ZONEFILE, ORIGIN) as z: assert z.filename == ZONEFILE assert z.ttl == TTL assert len(z.records) == 16