]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Use function capsulation in Huawei driver
authorWilson Liu <liuxinguo@huawei.com>
Sun, 13 Sep 2015 13:26:08 +0000 (21:26 +0800)
committerWilson Liu <liuxinguo@huawei.com>
Thu, 24 Sep 2015 07:05:13 +0000 (15:05 +0800)
Use a function _get_id_from_result() instead of lots of
duplicated code.

Closes-Bug: #1495231
Change-Id: I8ae7d27cb977110109fa26bd6b3ddcbc7298fbc1

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

index 17e91969093cd6e1578339a5385c6f9a579add5c..a86257a6fb63858d1c44cfc8a7c57600ee507da3 100644 (file)
@@ -1063,6 +1063,7 @@ MAP_COMMAND_TO_FAKE_RESPONSE['/lun/expand/PUT'] = (
 MAP_COMMAND_TO_FAKE_RESPONSE['/lungroup/associate?ID=12&ASSOCIATEOBJTYPE=11'
                              '&ASSOCIATEOBJID=12/DELETE'] = (
     FAKE_COMMON_SUCCESS_RESPONSE)
+
 # mock snapshot info map
 MAP_COMMAND_TO_FAKE_RESPONSE['/snapshot'] = (
     FAKE_CREATE_SNAPSHOT_INFO_RESPONSE)
@@ -1229,6 +1230,7 @@ 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)
 
@@ -2115,6 +2117,34 @@ class Huawei18000FCDriverTestCase(test.TestCase):
         lun_info = self.driver.create_volume(test_volume)
         self.assertEqual('1', lun_info['provider_location'])
 
+    def test_get_id_from_result(self):
+        self.driver.restclient.login()
+        result = {}
+        name = 'test_name'
+        key = 'NAME'
+        re = self.driver.restclient._get_id_from_result(result, name, key)
+        self.assertIsNone(re)
+
+        result = {'data': {}}
+        re = self.driver.restclient._get_id_from_result(result, name, key)
+        self.assertIsNone(re)
+
+        result = {'data': [{'COUNT': 1, 'ID': '1'},
+                           {'COUNT': 2, 'ID': '2'}]}
+
+        re = self.driver.restclient._get_id_from_result(result, name, key)
+        self.assertIsNone(re)
+
+        result = {'data': [{'NAME': 'test_name1', 'ID': '1'},
+                           {'NAME': 'test_name2', 'ID': '2'}]}
+        re = self.driver.restclient._get_id_from_result(result, name, key)
+        self.assertIsNone(re)
+
+        result = {'data': [{'NAME': 'test_name', 'ID': '1'},
+                           {'NAME': 'test_name2', 'ID': '2'}]}
+        re = self.driver.restclient._get_id_from_result(result, name, key)
+        self.assertEqual('1', re)
+
     def create_fake_conf_file(self):
         """Create a fake Config file
 
index f04814806a3e9e79794a59754673aefd0384ec6e..3c90a9378a0c2d1b3317b676ce9319b8b95c5bb4 100644 (file)
@@ -224,7 +224,7 @@ class RestClient(object):
     def _get_id_from_result(self, result, name, key):
         if 'data' in result:
             for item in result['data']:
-                if name == item[key]:
+                if name == item.get(key):
                     return item['ID']
 
     def get_volume_by_name(self, name):
@@ -368,10 +368,8 @@ class RestClient(object):
         result = self.call(url, None, "GET")
         self._assert_rest_result(result, _('Check portgroup associate error.'))
 
-        if 'data' in result:
-            for item in result['data']:
-                if view_id == item['ID']:
-                    return True
+        if self._get_id_from_result(result, view_id, 'ID'):
+            return True
         return False
 
     def do_mapping(self, lun_id, hostgroup_id, host_id, tgtportgroup_id=None):
@@ -531,10 +529,8 @@ class RestClient(object):
         result = self.call(url, None, "GET")
         self._assert_rest_result(result, _('Check lungroup associate error.'))
 
-        if 'data' in result:
-            for item in result['data']:
-                if view_id == item['ID']:
-                    return True
+        if self._get_id_from_result(result, view_id, 'ID'):
+            return True
         return False
 
     def hostgroup_associated(self, view_id, hostgroup_id):
@@ -544,10 +540,8 @@ class RestClient(object):
         result = self.call(url, None, "GET")
         self._assert_rest_result(result, _('Check hostgroup associate error.'))
 
-        if 'data' in result:
-            for item in result['data']:
-                if view_id == item['ID']:
-                    return True
+        if self._get_id_from_result(result, view_id, 'ID'):
+            return True
         return False
 
     def find_host_lun_id(self, host_id, lun_id):
@@ -636,10 +630,8 @@ class RestClient(object):
         result = self.call(url, None, "GET")
         self._assert_rest_result(result, _('Check hostgroup associate error.'))
 
-        if 'data' in result:
-            for item in result['data']:
-                if host_id == item['ID']:
-                    return True
+        if self._get_id_from_result(result, host_id, 'ID'):
+            return True
 
         return False
 
@@ -693,10 +685,8 @@ class RestClient(object):
         self._assert_rest_result(result,
                                  _('Check initiator added to array error.'))
 
-        if 'data' in result:
-            for item in result['data']:
-                if item['ID'] == ininame:
-                    return True
+        if self._get_id_from_result(result, ininame, 'ID'):
+            return True
         return False
 
     def is_initiator_associated_to_host(self, ininame):
@@ -1419,10 +1409,7 @@ class RestClient(object):
         result = self.call(url, None, "GET")
         self._assert_rest_result(result, _('Get partition by name error.'))
 
-        if 'data' in result:
-            for item in result['data']:
-                if name == item['NAME']:
-                    return item['ID']
+        return self._get_id_from_result(result, name, 'NAME')
 
     def get_partition_info_by_id(self, partition_id):
 
@@ -1453,10 +1440,7 @@ class RestClient(object):
         result = self.call(url, None, "GET")
         self._assert_rest_result(result, _('Get cache by name error.'))
 
-        if 'data' in result:
-            for item in result['data']:
-                if name == item['NAME']:
-                    return item['ID']
+        return self._get_id_from_result(result, name, 'NAME')
 
     def get_cache_info_by_id(self, cacheid):
         url = "/SMARTCACHEPARTITION/" + cacheid