Skip to content

Commit 22d99c2

Browse files
committed
add _cidr_addr() method to convert addresses to "x.x.x.x/xx" CIDR notation
1 parent 0c6c0d7 commit 22d99c2

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

ip_pool/ip_pool.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ def new_address(self, hostname: str, cidr=True) -> IPv4Address:
5353
self._hostnames[hostname] = addr
5454
self._save()
5555
if cidr:
56-
addr_str = f"{addr}/{self.prefixlen}"
57-
addr = IPv4Interface(addr_str)
56+
addr = self._cidr_addr(addr)
5857
return addr
5958

6059
def address_for(self, hostname: str, cidr=True) -> IPv4Address:
@@ -63,8 +62,7 @@ def address_for(self, hostname: str, cidr=True) -> IPv4Address:
6362
except KeyError:
6463
raise IPAddressPoolException(f"No address for hostname {hostname}")
6564
if cidr:
66-
addr_str = f"{addr}/{self.prefixlen}"
67-
addr = IPv4Interface(addr_str)
65+
addr = self._cidr_addr(addr)
6866
return addr
6967

7068
def release_address(self, hostname):
@@ -77,6 +75,11 @@ def release_address(self, hostname):
7775
self._ipaddr_pool.sort()
7876
self._save()
7977

78+
def _cidr_addr(self, addr: IPv4Address):
79+
addr_str = f"{addr}/{self.prefixlen}"
80+
addr = IPv4Interface(addr_str)
81+
return addr
82+
8083
def _to_dict(self):
8184
_dict = {
8285
"ip_version": self._ip_version,

0 commit comments

Comments
 (0)