Skip to content

Commit

Permalink
(FEAT) IP4Map::match_single_ip()
Browse files Browse the repository at this point in the history
  • Loading branch information
cpainchaud committed Nov 18, 2024
1 parent e10d833 commit 2ed7993
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
23 changes: 15 additions & 8 deletions illumio_pylo/IPList.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .API.JsonPayloadTypes import IPListObjectJsonStructure
from illumio_pylo import log
from illumio_pylo import log, IP4Map
from .Helpers import *


Expand All @@ -20,6 +20,7 @@ def __init__(self, name: str, href: str, owner: 'pylo.IPListStore', description=
self.description = description
self.raw_json = None
self.raw_entries = {}
self._ip4map: IP4Map = None

def count_entries(self) -> int:
return len(self.raw_entries)
Expand Down Expand Up @@ -56,15 +57,21 @@ def load_from_json(self, json_input: IPListObjectJsonStructure):
self.raw_entries[entry] = entry

def get_ip4map(self) -> pylo.IP4Map:
new_map = pylo.IP4Map()
return self.ip4map

for entry in self.raw_entries:
if entry[0] == '!':
new_map.subtract_from_text(entry[1:], ignore_ipv6=True)
else:
new_map.add_from_text(entry, ignore_ipv6=True)
@property
def ip4map(self) -> pylo.IP4Map:
if self._ip4map is None:
new_map = pylo.IP4Map()
self._ip4map = new_map

for entry in self.raw_entries:
if entry[0] == '!':
new_map.subtract_from_text(entry[1:], ignore_ipv6=True)
else:
new_map.add_from_text(entry, ignore_ipv6=True)

return new_map
return self._ip4map

def get_raw_entries_as_string_list(self, separator=',') -> str:
return pylo.string_list_to_text(self.raw_entries.values(), separator=separator)
Expand Down
9 changes: 9 additions & 0 deletions illumio_pylo/IPMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ def contains(self, another_map: 'IP4Map') -> bool:
return True
return False

def match_single_ip(self, ip: str) -> bool:
ip_object = ipaddress.IPv4Address(ip)
for entry in self._entries:
if entry[start] <= int(ip_object) <= entry[end]:
return True
if entry[start] > int(ip_object):
return False
return False

def substract(self, another_map: 'IP4Map'):
affected_rows = 0
for entry in another_map._entries:
Expand Down

0 comments on commit 2ed7993

Please sign in to comment.