From: Wilson Liu Date: Sun, 13 Sep 2015 13:26:08 +0000 (+0800) Subject: Use function capsulation in Huawei driver X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=8d5cd04f08e944c195341785d10217f36bc1dd5c;p=openstack-build%2Fcinder-build.git Use function capsulation in Huawei driver Use a function _get_id_from_result() instead of lots of duplicated code. Closes-Bug: #1495231 Change-Id: I8ae7d27cb977110109fa26bd6b3ddcbc7298fbc1 --- diff --git a/cinder/tests/unit/test_huawei_drivers.py b/cinder/tests/unit/test_huawei_drivers.py index 17e919690..a86257a6f 100644 --- a/cinder/tests/unit/test_huawei_drivers.py +++ b/cinder/tests/unit/test_huawei_drivers.py @@ -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 diff --git a/cinder/volume/drivers/huawei/rest_client.py b/cinder/volume/drivers/huawei/rest_client.py index f04814806..3c90a9378 100644 --- a/cinder/volume/drivers/huawei/rest_client.py +++ b/cinder/volume/drivers/huawei/rest_client.py @@ -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