]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fix way of getting LUN id in Huawei driver
authorliuxinguo <liuxinguo@huawei.com>
Thu, 6 Aug 2015 04:08:54 +0000 (12:08 +0800)
committerWilson Liu <liuxinguo@huawei.com>
Mon, 21 Sep 2015 08:24:36 +0000 (16:24 +0800)
Get the LUN id first from the volume['provider_location'],
and only if volume['provider_location'] is None, query the
backend to get the LUN id.

Also change function name "mapping_hostgroup_and_lungroup"
to "do_mapping".

Closes-Bug: #1482030
Change-Id: Ie94ea2284c572e842a64f962cc10b1a2b8145d3c

cinder/tests/unit/test_huawei_drivers.py
cinder/volume/drivers/huawei/huawei_driver.py
cinder/volume/drivers/huawei/rest_client.py

index fae31b0acb2477984e3c8be4fefd50f5dfa94d40..9794c4b93433e7b3b8264eee1c5266796e07c929 100644 (file)
@@ -84,7 +84,8 @@ test_snap = {'name': 'volume-21ec7341-9256-497b-97d9-ef48edcf0635',
              'display_description': 'test volume',
              'volume_type_id': None,
              'provider_location': '11',
-             'volume': {"volume_id": '21ec7341-9256-497b-97d9-ef48edcf0635'}
+             'volume': {"volume_id": '21ec7341-9256-497b-97d9-ef48edcf0635'},
+             'volume': {'provider_location': '12'},
              }
 
 test_host = {'host': 'ubuntu001@backend001#OpenStack_Pool',
@@ -1227,6 +1228,8 @@ MAP_COMMAND_TO_FAKE_RESPONSE['/MAPPINGVIEW/CREATE_ASSOCIATE/PUT'] = (
 MAP_COMMAND_TO_FAKE_RESPONSE['/fc_initiator?ISFREE=true&'
                              'range=[0-8191]/GET'] = (
     FAKE_FC_INFO_RESPONSE)
+MAP_COMMAND_TO_FAKE_RESPONSE['/fc_initiator/10000090fa0d6754/GET'] = (
+    FAKE_FC_INFO_RESPONSE)
 
 MAP_COMMAND_TO_FAKE_RESPONSE['/fc_initiator/10000090fa0d6754/PUT'] = (
     FAKE_COMMON_SUCCESS_RESPONSE)
@@ -1251,6 +1254,9 @@ MAP_COMMAND_TO_FAKE_RESPONSE['/fc_port/GET'] = (
 MAP_COMMAND_TO_FAKE_RESPONSE['/fc_initiator/GET'] = (
     FAKE_GET_FC_PORT_RESPONSE)
 
+MAP_COMMAND_TO_FAKE_RESPONSE['/lun/associate/cachepartition/POST'] = (
+    FAKE_SYSTEM_VERSION_RESPONSE)
+
 MAP_COMMAND_TO_FAKE_RESPONSE['/fc_initiator?range=[0-100]/GET'] = (
     FAKE_GET_FC_PORT_RESPONSE)
 
@@ -1407,6 +1413,14 @@ class Huawei18000ISCSIDriverTestCase(test.TestCase):
         lun_info = self.driver.create_snapshot(test_snap)
         self.assertEqual(11, lun_info['provider_location'])
 
+        test_snap['volume']['provider_location'] = ''
+        lun_info = self.driver.create_snapshot(test_snap)
+        self.assertEqual(11, lun_info['provider_location'])
+
+        test_snap['volume']['provider_location'] = None
+        lun_info = self.driver.create_snapshot(test_snap)
+        self.assertEqual(11, lun_info['provider_location'])
+
     def test_delete_snapshot_success(self):
         self.driver.restclient.login()
         delete_flag = self.driver.delete_snapshot(test_snap)
@@ -1454,6 +1468,9 @@ class Huawei18000ISCSIDriverTestCase(test.TestCase):
     def test_create_volume_fail(self):
         self.driver.restclient.login()
         self.driver.restclient.test_fail = True
+        self.assertRaises(exception.VolumeBackendAPIException,
+                          self.driver.create_volume, test_volume)
+
         self.assertRaises(exception.VolumeBackendAPIException,
                           self.driver.create_volume, error_volume)
 
@@ -1759,6 +1776,14 @@ class Huawei18000FCDriverTestCase(test.TestCase):
         lun_info = self.driver.create_snapshot(test_snap)
         self.assertEqual(11, lun_info['provider_location'])
 
+        test_snap['volume']['provider_location'] = ''
+        lun_info = self.driver.create_snapshot(test_snap)
+        self.assertEqual(11, lun_info['provider_location'])
+
+        test_snap['volume']['provider_location'] = None
+        lun_info = self.driver.create_snapshot(test_snap)
+        self.assertEqual(11, lun_info['provider_location'])
+
     def test_delete_snapshot_success(self):
         self.driver.restclient.login()
         delete_flag = self.driver.delete_snapshot(test_snap)
index b7d08f80b791cd6983b9c0ec535732deff193d26..c0d25b935104efeff19954407bdd76ab86f216f0 100644 (file)
@@ -50,7 +50,7 @@ class HuaweiBaseDriver(driver.VolumeDriver):
 
     def __init__(self, *args, **kwargs):
         super(HuaweiBaseDriver, self).__init__(*args, **kwargs)
-        self.configuration = kwargs.get('configuration', None)
+        self.configuration = kwargs.get('configuration')
         if not self.configuration:
             msg = _('_instantiate_driver: configuration not found.')
             raise exception.InvalidInput(reason=msg)
@@ -136,7 +136,7 @@ class HuaweiBaseDriver(driver.VolumeDriver):
         Thirdly, remove the lun.
         """
         name = huawei_utils.encode_name(volume['id'])
-        lun_id = volume.get('provider_location', None)
+        lun_id = volume.get('provider_location')
         LOG.info(_LI('Delete volume: %(name)s, array lun id: %(lun_id)s.'),
                  {'name': name, 'lun_id': lun_id},)
         if lun_id:
@@ -313,7 +313,7 @@ class HuaweiBaseDriver(driver.VolumeDriver):
         pool_info = self.restclient.find_pool_info(pool_name, pools)
         src_volume_name = huawei_utils.encode_name(volume['id'])
         dst_volume_name = six.text_type(hash(src_volume_name))
-        src_id = volume.get('provider_location', None)
+        src_id = volume.get('provider_location')
 
         src_lun_params = self.restclient.get_lun_info(src_id)
 
@@ -396,7 +396,7 @@ class HuaweiBaseDriver(driver.VolumeDriver):
         """
         snapshotname = huawei_utils.encode_name(snapshot['id'])
 
-        snapshot_id = snapshot.get('provider_location', None)
+        snapshot_id = snapshot.get('provider_location')
         if snapshot_id is None:
             snapshot_id = self.restclient.get_snapshotid_by_name(snapshotname)
             if snapshot_id is None:
@@ -484,15 +484,7 @@ class HuaweiBaseDriver(driver.VolumeDriver):
              'oldsize': volume_size,
              'newsize': new_volume_size},)
 
-        lun_id = self.restclient.get_volume_by_name(volume_name)
-
-        if lun_id is None:
-            msg = (_(
-                "Can't find lun info on the array, lun name is: %(name)s.")
-                % {'name': volume_name})
-            LOG.error(msg)
-            raise exception.VolumeBackendAPIException(data=msg)
-
+        lun_id = self.restclient.get_lunid(volume, volume_name)
         luninfo = self.restclient.extend_volume(lun_id, new_volume_size)
 
         return {'provider_location': luninfo['ID'],
@@ -518,7 +510,7 @@ class HuaweiBaseDriver(driver.VolumeDriver):
             {'snapshot': snapshotname,
              'volume': volume_name},)
 
-        snapshot_id = snapshot.get('provider_location', None)
+        snapshot_id = snapshot.get('provider_location')
         if snapshot_id is None:
             snapshot_id = self.restclient.get_snapshotid_by_name(snapshotname)
 
@@ -566,7 +558,7 @@ class HuaweiBaseDriver(driver.VolumeDriver):
             return False
 
     def modify_lun(self, lun_id, change_opts):
-        if change_opts.get('partitionid', None):
+        if change_opts.get('partitionid'):
             old, new = change_opts['partitionid']
             old_id = old[0]
             old_name = old[1]
@@ -583,7 +575,7 @@ class HuaweiBaseDriver(driver.VolumeDriver):
                       "old_id": old_id, "old_name": old_name,
                       "new_id": new_id, "new_name": new_name})
 
-        if change_opts.get('cacheid', None):
+        if change_opts.get('cacheid'):
             old, new = change_opts['cacheid']
             old_id = old[0]
             old_name = old[1]
@@ -600,7 +592,7 @@ class HuaweiBaseDriver(driver.VolumeDriver):
                       'old_id': old_id, "old_name": old_name,
                       'new_id': new_id, "new_name": new_name})
 
-        if change_opts.get('policy', None):
+        if change_opts.get('policy'):
             old_policy, new_policy = change_opts['policy']
             self.restclient.change_lun_smarttier(lun_id, new_policy)
             LOG.info(_LI("Retype LUN(id: %(lun_id)s) smarttier policy from "
@@ -609,7 +601,7 @@ class HuaweiBaseDriver(driver.VolumeDriver):
                       'old_policy': old_policy,
                       'new_policy': new_policy})
 
-        if change_opts.get('qos', None):
+        if change_opts.get('qos'):
             old_qos, new_qos = change_opts['qos']
             old_qos_id = old_qos[0]
             old_qos_value = old_qos[1]
@@ -654,7 +646,7 @@ class HuaweiBaseDriver(driver.VolumeDriver):
             'LUNType': None,
         }
 
-        lun_id = volume.get('provider_location', None)
+        lun_id = volume.get('provider_location')
         old_opts = self.get_lun_specs(lun_id)
 
         new_specs = new_type['extra_specs']
@@ -873,11 +865,11 @@ class Huawei18000ISCSIDriver(HuaweiBaseDriver, driver.ISCSIDriver):
                                                host_id)
         hostgroup_id = self.restclient.add_host_into_hostgroup(host_id)
 
+        lun_id = self.restclient.get_lunid(volume, volume_name)
+
         # Mapping lungroup and hostgroup to view.
-        lun_id = self.restclient.mapping_hostgroup_and_lungroup(volume_name,
-                                                                hostgroup_id,
-                                                                host_id,
-                                                                portgroup_id)
+        self.restclient.do_mapping(volume_name, hostgroup_id,
+                                   host_id, portgroup_id)
 
         hostlun_id = self.restclient.find_host_lun_id(host_id, lun_id)
 
@@ -919,7 +911,7 @@ class Huawei18000ISCSIDriver(HuaweiBaseDriver, driver.ISCSIDriver):
         """Delete map between a volume and a host."""
         initiator_name = connector['initiator']
         volume_name = huawei_utils.encode_name(volume['id'])
-        lun_id = volume.get('provider_location', None)
+        lun_id = volume.get('provider_location')
         host_name = connector['host']
         lungroup_id = None
 
@@ -1042,6 +1034,8 @@ class Huawei18000FCDriver(HuaweiBaseDriver, driver.FibreChannelDriver):
             {'wwpns': wwns,
              'volume': volume_name},)
 
+        lun_id = self.restclient.get_lunid(volume, volume_name)
+
         host_name_before_hash = None
         host_name = connector['host']
         if host_name and (len(host_name) > constants.MAX_HOSTNAME_LENGTH):
@@ -1091,9 +1085,7 @@ class Huawei18000FCDriver(HuaweiBaseDriver, driver.FibreChannelDriver):
 
         # Add host into hostgroup.
         hostgroup_id = self.restclient.add_host_into_hostgroup(host_id)
-        lun_id = self.restclient.mapping_hostgroup_and_lungroup(volume_name,
-                                                                hostgroup_id,
-                                                                host_id)
+        self.restclient.do_mapping(volume_name, hostgroup_id, host_id)
         host_lun_id = self.restclient.find_host_lun_id(host_id, lun_id)
 
         # Return FC properties.
@@ -1115,7 +1107,7 @@ class Huawei18000FCDriver(HuaweiBaseDriver, driver.FibreChannelDriver):
         """Delete map between a volume and a host."""
         wwns = connector['wwpns']
         volume_name = huawei_utils.encode_name(volume['id'])
-        lun_id = volume.get('provider_location', None)
+        lun_id = volume.get('provider_location')
         host_name = connector['host']
         left_lunnum = -1
         lungroup_id = None
index 9aec9400b616a5ea30f42136f1667c433a760cd5..b8e7ba8a6f1316c6b846d7943b8b8e76e97fca11 100644 (file)
@@ -253,12 +253,8 @@ class RestClient(object):
             {'snapshot': snapshot_name,
              'volume': volume_name})
 
-        lun_id = self.get_volume_by_name(volume_name)
-        if lun_id is None:
-            msg = (_("Can't find lun info on the array, "
-                     "lun name is: %(name)s.") % {'name': volume_name})
-            LOG.error(msg)
-            raise exception.VolumeBackendAPIException(data=msg)
+        volume = snapshot['volume']
+        lun_id = self.get_lunid(volume, volume_name)
 
         url = "/snapshot"
         data = json.dumps({"TYPE": "27",
@@ -274,6 +270,16 @@ class RestClient(object):
 
         return result['data']
 
+    def get_lunid(self, volume, volume_name):
+        lun_id = (volume.get('provider_location') or
+                  self.get_volume_by_name(volume_name))
+        if not lun_id:
+            msg = (_("Can't find lun info on the array, "
+                     "lun name is: %(name)s.") % {'name': volume_name})
+            LOG.error(msg)
+            raise exception.VolumeBackendAPIException(data=msg)
+        return lun_id
+
     def check_snapshot_exist(self, snapshot_id):
         url = "/snapshot/%s" % snapshot_id
         data = json.dumps({"TYPE": "27",
@@ -374,17 +380,15 @@ class RestClient(object):
                     return True
         return False
 
-    def mapping_hostgroup_and_lungroup(self, volume_name, hostgroup_id,
-                                       host_id, tgtportgroup_id=None):
+    def do_mapping(self, lun_id, hostgroup_id, host_id, tgtportgroup_id=None):
         """Add hostgroup and lungroup to mapping view."""
         lungroup_name = constants.LUNGROUP_PREFIX + host_id
         mapping_view_name = constants.MAPPING_VIEW_PREFIX + host_id
         lungroup_id = self._find_lungroup(lungroup_name)
-        lun_id = self.get_volume_by_name(volume_name)
         view_id = self.find_mapping_view(mapping_view_name)
 
         LOG.info(_LI(
-            'mapping_hostgroup_and_lungroup, lun_group: %(lun_group)s, '
+            'do_mapping, lun_group: %(lun_group)s, '
             'view_id: %(view_id)s, lun_id: %(lun_id)s.'),
             {'lun_group': lungroup_id,
              'view_id': view_id,
@@ -424,8 +428,6 @@ class RestClient(object):
                     'view. Remove lun from lungroup now.'))
                 self.remove_lun_from_lungroup(lungroup_id, lun_id)
 
-        return lun_id
-
     def ensure_initiator_added(self, xml_file_path, initiator_name, host_id):
         added = self._initiator_is_added_to_array(initiator_name)
         if not added:
@@ -1364,7 +1366,7 @@ class RestClient(object):
         return result['data']['IOCLASSID']
 
     def get_lungroupids_by_lunid(self, lun_id):
-        """Get lungroup id by lun id."""
+        """Get lungroup ids by lun id."""
         url = ("/lungroup/associate?TYPE=256"
                "&ASSOCIATEOBJTYPE=11&ASSOCIATEOBJID=%s" % lun_id)