From 7e41cc7556c791f22d7227f06e2ca75baedd0089 Mon Sep 17 00:00:00 2001 From: Wilson Liu Date: Mon, 23 May 2016 18:09:00 +0800 Subject: [PATCH] Huawei: Check before add initiator Currently we just check whether the iSCSI initiator has been added to host, but not check whether the host is our expected host or other host. Closes-Bug: #1580933 Change-Id: I6ea891431e36d6536d6c77dedba41bc747883b08 --- .../drivers/huawei/test_huawei_drivers.py | 23 ++++++++++++++++++- cinder/volume/drivers/huawei/huawei_driver.py | 3 ++- cinder/volume/drivers/huawei/rest_client.py | 18 +++++++++++---- 3 files changed, 37 insertions(+), 7 deletions(-) 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 3f0bb952639..1851fc278ba 100644 --- a/cinder/tests/unit/volume/drivers/huawei/test_huawei_drivers.py +++ b/cinder/tests/unit/volume/drivers/huawei/test_huawei_drivers.py @@ -863,6 +863,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" }] } """ @@ -1340,7 +1350,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) @@ -4534,6 +4544,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 8648822cd8a..92132831cfd 100644 --- a/cinder/volume/drivers/huawei/huawei_driver.py +++ b/cinder/volume/drivers/huawei/huawei_driver.py @@ -1987,7 +1987,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 7bf18d298bf..c94b1fe59bf 100644 --- a/cinder/volume/drivers/huawei/rest_client.py +++ b/cinder/volume/drivers/huawei/rest_client.py @@ -497,7 +497,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) @@ -763,17 +763,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):