Insert a CIDR range
(self, base, mask, source="manual", notes=None)
| 770 | return self.all_ips_collection.replace_one({"ip": ip}, record, upsert=True) |
| 771 | |
| 772 | def insert_ipv4_range(self, base, mask, source="manual", notes=None): |
| 773 | """ |
| 774 | Insert a CIDR range |
| 775 | """ |
| 776 | |
| 777 | result = self.ip_zones_collection.find_one({"zone": base + "/" + mask}) |
| 778 | |
| 779 | if result is None: |
| 780 | new_entry = {} |
| 781 | new_entry["updated"] = datetime.now() |
| 782 | new_entry["created"] = datetime.now() |
| 783 | new_entry["status"] = "unconfirmed" |
| 784 | new_entry["sources"] = [{"source": source, "updated": datetime.now()}] |
| 785 | new_entry["zone"] = base + "/" + mask |
| 786 | if notes is None: |
| 787 | new_entry["notes"] = [] |
| 788 | else: |
| 789 | new_entry["notes"] = [notes] |
| 790 | |
| 791 | self._mongo_connector.perform_insert(self.ip_zones_collection, new_entry) |
| 792 | else: |
| 793 | found = False |
| 794 | for entry in result["sources"]: |
| 795 | if entry["source"] == source: |
| 796 | found = True |
| 797 | |
| 798 | if found: |
| 799 | self.ip_zones_collection.update_one( |
| 800 | {"_id": ObjectId(result["_id"])}, |
| 801 | {"$set": {"updated": datetime.now()}}, |
| 802 | ) |
| 803 | self.ip_zones_collection.update_one( |
| 804 | { |
| 805 | "_id": ObjectId(result["_id"]), |
| 806 | "sources.source": source, |
| 807 | }, |
| 808 | { |
| 809 | "$set": { |
| 810 | "sources.$.updated": datetime.now(), |
| 811 | } |
| 812 | }, |
| 813 | ) |
| 814 | else: |
| 815 | sources = {"source": source, "updated": datetime.now()} |
| 816 | self.ip_zones_collection.update_one( |
| 817 | {"_id": ObjectId(result["_id"])}, |
| 818 | {"$set": {"updated": datetime.now()}}, |
| 819 | ) |
| 820 | self.ip_zones_collection.update_one( |
| 821 | {"_id": ObjectId(result["_id"])}, {"$push": {"sources": sources}} |
| 822 | ) |
| 823 | |
| 824 | if notes is not None: |
| 825 | found = False |
| 826 | if "notes" in result: |
| 827 | for entry in result["notes"]: |
| 828 | if entry == notes: |
| 829 | found = True |
nothing calls this directly
no test coverage detected