]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Ensure hostnames are converted to IP for comparison.
authorBob Callaway <bob.callaway@netapp.com>
Fri, 10 Jan 2014 16:15:07 +0000 (11:15 -0500)
committerBob Callaway <bob.callaway@netapp.com>
Fri, 17 Jan 2014 18:37:49 +0000 (13:37 -0500)
Also switch to using getaddrinfo for IPv6 compliance
vs gethostbyname.

Change-Id: I1a194eae514a65bff56488571c05457bfa660c02
Closes-Bug: 1267667

cinder/tests/test_netapp_nfs.py
cinder/volume/drivers/netapp/nfs.py

index e909b5d095971622ed71e6c49ad181f0205ad2e7..471ecdeaa7bf6171e36002ccd3aa9c930380b2e1 100644 (file)
@@ -20,7 +20,6 @@ import mox
 from mox import IgnoreArg
 from mox import IsA
 import os
-import socket
 
 from cinder import context
 from cinder import exception
@@ -752,8 +751,8 @@ class NetappDirectCmodeNfsDriverTestCase(test.TestCase):
     def test_check_share_in_use_incorrect_host(self):
         drv = self._driver
         mox = self.mox
-        mox.StubOutWithMock(socket, 'gethostbyname')
-        socket.gethostbyname(IgnoreArg()).AndRaise(Exception())
+        mox.StubOutWithMock(drv, '_resolve_hostname')
+        drv._resolve_hostname(IgnoreArg()).AndRaise(Exception())
         mox.ReplayAll()
         share = drv._check_share_in_use('incorrect:8989', '/dir')
         mox.VerifyAll()
@@ -764,9 +763,9 @@ class NetappDirectCmodeNfsDriverTestCase(test.TestCase):
         drv = self._driver
         mox = self.mox
         drv._mounted_shares = ['127.0.0.1:/dir/share']
-        mox.StubOutWithMock(socket, 'gethostbyname')
+        mox.StubOutWithMock(drv, '_resolve_hostname')
         mox.StubOutWithMock(drv, '_share_match_for_ip')
-        socket.gethostbyname(IgnoreArg()).AndReturn('10.22.33.44')
+        drv._resolve_hostname(IgnoreArg()).AndReturn('10.22.33.44')
         drv._share_match_for_ip(
             '10.22.33.44', ['127.0.0.1:/dir/share']).AndReturn('share')
         mox.ReplayAll()
index 79bb1d99251e26d9573c7c41c22f2f7c396e582e..f43a8b07c7c99c82db566b4de769301639a93ede 100644 (file)
@@ -547,7 +547,7 @@ class NetAppNFSDriver(nfs.NfsDriver):
         try:
             if conn:
                 host = conn.split(':')[0]
-                ipv4 = socket.gethostbyname(host)
+                ip = self._resolve_hostname(host)
                 share_candidates = []
                 for sh in self._mounted_shares:
                     sh_exp = sh.split(':')[1]
@@ -556,7 +556,7 @@ class NetAppNFSDriver(nfs.NfsDriver):
                 if share_candidates:
                     LOG.debug(_('Found possible share matches %s'),
                               share_candidates)
-                    return self._share_match_for_ip(ipv4, share_candidates)
+                    return self._share_match_for_ip(ip, share_candidates)
         except Exception:
             LOG.warn(_("Unexpected exception while short listing used share."))
         return None
@@ -604,6 +604,12 @@ class NetAppNFSDriver(nfs.NfsDriver):
         """Checks if share is compatible with volume to host it."""
         raise NotImplementedError()
 
+    def _resolve_hostname(self, hostname):
+        """Resolves hostname to IP address."""
+        res = socket.getaddrinfo(hostname, None)[0]
+        family, socktype, proto, canonname, sockaddr = res
+        return sockaddr[0]
+
 
 class NetAppDirectNfsDriver (NetAppNFSDriver):
     """Executes commands related to volumes on NetApp filer."""
@@ -798,7 +804,8 @@ class NetAppDirectCmodeNfsDriver (NetAppDirectNfsDriver):
         net_if_iter.add_new_child('max-records', '10')
         query = NaElement('query')
         net_if_iter.add_child_elem(query)
-        query.add_node_with_children('net-interface-info', **{'address': ip})
+        query.add_node_with_children('net-interface-info',
+                                     **{'address': self._resolve_hostname(ip)})
         result = self._invoke_successfully(net_if_iter)
         if result.get_child_content('num-records') and\
                 int(result.get_child_content('num-records')) >= 1:
@@ -808,7 +815,7 @@ class NetAppDirectCmodeNfsDriver (NetAppDirectNfsDriver):
             _('No interface found on cluster for ip %s')
             % (ip))
 
-    def _get_verver_ips(self, vserver):
+    def _get_vserver_ips(self, vserver):
         """Get ips for the vserver."""
         result = na_utils.invoke_api(
             self._client, api_name='net-interface-get-iter',
@@ -934,13 +941,13 @@ class NetAppDirectCmodeNfsDriver (NetAppDirectNfsDriver):
             LOG.warn(_("No shares found hence skipping ssc refresh."))
             return
         mnt_share_vols = set()
-        vs_ifs = self._get_verver_ips(self.vserver)
+        vs_ifs = self._get_vserver_ips(self.vserver)
         for vol in vols['all']:
             for sh in self._mounted_shares:
                 host = sh.split(':')[0]
                 junction = sh.split(':')[1]
-                ipv4 = socket.gethostbyname(host)
-                if (self._ip_in_ifs(ipv4, vs_ifs) and
+                ip = self._resolve_hostname(host)
+                if (self._ip_in_ifs(ip, vs_ifs) and
                         junction == vol.id['junction_path']):
                     mnt_share_vols.add(vol)
                     vol.export['path'] = sh