483b06
From 8d88b50c3f79e054a039d123fdaf6aa3a5339135 Mon Sep 17 00:00:00 2001
483b06
From: Martin Basti <mbasti@redhat.com>
483b06
Date: Wed, 14 Jun 2017 14:47:23 +0200
483b06
Subject: [PATCH] refactor CheckedIPAddress class
483b06
483b06
Make methods without side effects (setting mask)
483b06
483b06
https://pagure.io/freeipa/issue/4317
483b06
483b06
Reviewed-By: David Kupka <dkupka@redhat.com>
483b06
---
483b06
 ipapython/ipautil.py | 29 ++++++++++++++++++++++-------
483b06
 1 file changed, 22 insertions(+), 7 deletions(-)
483b06
483b06
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
483b06
index 647ee833ae33f246de6d6b13703fac6e20eef7bc..2c020e3ecbf4d8b969511a6dd9b36ee955ba1f15 100644
483b06
--- a/ipapython/ipautil.py
483b06
+++ b/ipapython/ipautil.py
483b06
@@ -62,6 +62,12 @@ PROTOCOL_NAMES = {
483b06
     socket.SOCK_DGRAM: 'udp'
483b06
 }
483b06
 
483b06
+InterfaceDetails = collections.namedtuple(
483b06
+    'InterfaceDetails', [
483b06
+        'name',  # interface name
483b06
+        'ifnet'  # network details of interface
483b06
+    ])
483b06
+
483b06
 
483b06
 class UnsafeIPAddress(netaddr.IPAddress):
483b06
     """Any valid IP address with or without netmask."""
483b06
@@ -161,9 +167,12 @@ class CheckedIPAddress(UnsafeIPAddress):
483b06
             raise ValueError("cannot use multicast IP address {}".format(addr))
483b06
 
483b06
         if match_local:
483b06
-            if not self.get_matching_interface():
483b06
+            intf_details = self.get_matching_interface()
483b06
+            if not intf_details:
483b06
                 raise ValueError('no network interface matches the IP address '
483b06
                                  'and netmask {}'.format(addr))
483b06
+            else:
483b06
+                self.set_ip_net(intf_details.ifnet)
483b06
 
483b06
         if self._net is None:
483b06
             if self.version == 4:
483b06
@@ -193,7 +202,8 @@ class CheckedIPAddress(UnsafeIPAddress):
483b06
 
483b06
     def get_matching_interface(self):
483b06
         """Find matching local interface for address
483b06
-        :return: Interface name or None if no interface has this address
483b06
+        :return: InterfaceDetails named tuple or None if no interface has
483b06
+        this address
483b06
         """
483b06
         if self.version == 4:
483b06
             family = netifaces.AF_INET
483b06
@@ -204,7 +214,6 @@ class CheckedIPAddress(UnsafeIPAddress):
483b06
                 "Unsupported address family ({})".format(self.version)
483b06
             )
483b06
 
483b06
-        iface = None
483b06
         for interface in netifaces.interfaces():
483b06
             for ifdata in netifaces.ifaddresses(interface).get(family, []):
483b06
 
483b06
@@ -218,11 +227,17 @@ class CheckedIPAddress(UnsafeIPAddress):
483b06
                 ))
483b06
 
483b06
                 if ifnet.ip == self:
483b06
-                    iface = interface
483b06
-                    self._net = ifnet
483b06
-                    break
483b06
+                    return InterfaceDetails(interface, ifnet)
483b06
 
483b06
-        return iface
483b06
+    def set_ip_net(self, ifnet):
483b06
+        """Set IP Network details for this address. IPNetwork is valid only
483b06
+        locally, so this should be set only for local IP addresses
483b06
+
483b06
+        :param ifnet: netaddr.IPNetwork object with information about IP
483b06
+        network where particula address belongs locally
483b06
+        """
483b06
+        assert isinstance(ifnet, netaddr.IPNetwork)
483b06
+        self._net = ifnet
483b06
 
483b06
 
483b06
 def valid_ip(addr):
483b06
-- 
483b06
2.9.4
483b06