diff --git a/cinder/tests/unit/volume/drivers/huawei/test_huawei_drivers.py b/cinder/tests/unit/volume/drivers/huawei/test_huawei_drivers.py index 47d7003ecdf..769a19cb503 100644 --- a/cinder/tests/unit/volume/drivers/huawei/test_huawei_drivers.py +++ b/cinder/tests/unit/volume/drivers/huawei/test_huawei_drivers.py @@ -901,6 +901,16 @@ FAKE_ISCSI_INITIATOR_RESPONSE = """ "RUNNINGSTATUS":"28", "TYPE":222, "USECHAP":"true" + }, + { + "ISFREE":"true", + "ID":"ini-1" + }, + { + "ISFREE":"false", + "ID":"ini-2", + "PARENTNAME":"Host2", + "PARENTID":"2" }] } """ @@ -1384,7 +1394,7 @@ MAP_COMMAND_TO_FAKE_RESPONSE['/iscsidevicename'] = ( FAKE_GET_ISCSI_DEVICE_RESPONSE) MAP_COMMAND_TO_FAKE_RESPONSE['/iscsi_initiator?range=[0-256]/GET'] = ( - FAKE_COMMON_SUCCESS_RESPONSE) + FAKE_ISCSI_INITIATOR_RESPONSE) MAP_COMMAND_TO_FAKE_RESPONSE['/iscsi_initiator/'] = ( FAKE_ISCSI_INITIATOR_RESPONSE) @@ -5139,6 +5149,17 @@ class HuaweiFCDriverTestCase(HuaweiTestBase): model_update['status'], "Consistency Group created failed") + def test_is_initiator_associated_to_host_raise(self): + self.assertRaises(exception.VolumeBackendAPIException, + self.driver.client.is_initiator_associated_to_host, + 'ini-2', '1') + + def test_is_initiator_associated_to_host_true(self): + ret = self.driver.client.is_initiator_associated_to_host('ini-1', '1') + self.assertFalse(ret) + ret = self.driver.client.is_initiator_associated_to_host('ini-2', '2') + self.assertTrue(ret) + class HuaweiConfTestCase(test.TestCase): def setUp(self): diff --git a/cinder/volume/drivers/huawei/huawei_driver.py b/cinder/volume/drivers/huawei/huawei_driver.py index a606966d822..714ab55778a 100644 --- a/cinder/volume/drivers/huawei/huawei_driver.py +++ b/cinder/volume/drivers/huawei/huawei_driver.py @@ -2044,7 +2044,8 @@ class HuaweiISCSIDriver(HuaweiBaseDriver, driver.ISCSIDriver): self.client.delete_lungroup_mapping_view(view_id, lungroup_id) self.client.delete_lungroup(lungroup_id) - if self.client.is_initiator_associated_to_host(initiator_name): + if self.client.is_initiator_associated_to_host(initiator_name, + host_id): self.client.remove_iscsi_from_host(initiator_name) hostgroup_name = constants.HOSTGROUP_PREFIX + host_id hostgroup_id = self.client.find_hostgroup(hostgroup_name) diff --git a/cinder/volume/drivers/huawei/rest_client.py b/cinder/volume/drivers/huawei/rest_client.py index 9b10d2f7f18..13b3c79cb43 100644 --- a/cinder/volume/drivers/huawei/rest_client.py +++ b/cinder/volume/drivers/huawei/rest_client.py @@ -507,7 +507,7 @@ class RestClient(object): added = self._initiator_is_added_to_array(initiator_name) if not added: self._add_initiator_to_array(initiator_name) - if not self.is_initiator_associated_to_host(initiator_name): + if not self.is_initiator_associated_to_host(initiator_name, host_id): self._associate_initiator_to_host(initiator_name, host_id) @@ -773,17 +773,25 @@ class RestClient(object): return True return False - def is_initiator_associated_to_host(self, ininame): + def is_initiator_associated_to_host(self, ininame, host_id): """Check whether the initiator is associated to the host.""" url = "/iscsi_initiator?range=[0-256]" result = self.call(url, None, "GET") self._assert_rest_result( result, _('Check initiator associated to host error.')) - if 'data' in result: - for item in result['data']: - if item['ID'] == ininame and item['ISFREE'] == "true": + for item in result.get('data'): + if item['ID'] == ininame: + if item['ISFREE'] == "true": return False + if item['PARENTID'] == host_id: + return True + else: + msg = (_("Initiator %(ini)s has been added to another " + "host %(host)s.") % {"ini": ininame, + "host": item['PARENTNAME']}) + LOG.error(msg) + raise exception.VolumeBackendAPIException(data=msg) return True def _add_initiator_to_array(self, initiator_name):