From a8c7efd0c6df604e37f7003917beee06f481c03b Mon Sep 17 00:00:00 2001 From: Wilson Liu Date: Mon, 22 Feb 2016 13:03:26 +0800 Subject: [PATCH] Huawei: Check before delete host Currently we delete the host without checking whether the host already belongs to a host group. If a host already belongs to a hostgroup, an error will occur. So we should do the check before delete it. Closed-Bug: #1548183 Change-Id: Ibfa0bb4930e6d932c332aacb19ae81f9daad55ce --- cinder/tests/unit/test_huawei_drivers.py | 18 ++++++++++++++++++ cinder/volume/drivers/huawei/huawei_driver.py | 3 ++- cinder/volume/drivers/huawei/rest_client.py | 8 ++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/cinder/tests/unit/test_huawei_drivers.py b/cinder/tests/unit/test_huawei_drivers.py index e599fad50..350b9616e 100644 --- a/cinder/tests/unit/test_huawei_drivers.py +++ b/cinder/tests/unit/test_huawei_drivers.py @@ -3523,6 +3523,24 @@ class HuaweiFCDriverTestCase(test.TestCase): data = {"NAME": new_name, "DESCRIPTION": des} mock_call.assert_called_once_with(url, data, "PUT") + @mock.patch.object(rest_client.RestClient, 'call', + return_value={"data": {}}) + def test_is_host_associated_to_hostgroup_no_data(self, mock_call): + res = self.driver.client.is_host_associated_to_hostgroup('1') + self.assertFalse(res) + + @mock.patch.object(rest_client.RestClient, 'call', + return_value={"data": {'ISADD2HOSTGROUP': 'true'}}) + def test_is_host_associated_to_hostgroup_true(self, mock_call): + res = self.driver.client.is_host_associated_to_hostgroup('1') + self.assertTrue(res) + + @mock.patch.object(rest_client.RestClient, 'call', + return_value={"data": {'ISADD2HOSTGROUP': 'false'}}) + def test_is_host_associated_to_hostgroup_false(self, mock_call): + res = self.driver.client.is_host_associated_to_hostgroup('1') + self.assertFalse(res) + 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 8ad8c80b3..aa3324628 100644 --- a/cinder/volume/drivers/huawei/huawei_driver.py +++ b/cinder/volume/drivers/huawei/huawei_driver.py @@ -1665,7 +1665,8 @@ class HuaweiFCDriver(HuaweiBaseDriver, driver.FibreChannelDriver): self.client.get_host_fc_initiators(host_id)) iqns_in_host = ( self.client.get_host_iscsi_initiators(host_id)) - if not wwns_in_host and not iqns_in_host: + if not (wwns_in_host or iqns_in_host or + self.client.is_host_associated_to_hostgroup(host_id)): self.client.remove_host(host_id) msg = _('No FC initiator can be added to host.') diff --git a/cinder/volume/drivers/huawei/rest_client.py b/cinder/volume/drivers/huawei/rest_client.py index 2023b5553..3d6a1b4eb 100644 --- a/cinder/volume/drivers/huawei/rest_client.py +++ b/cinder/volume/drivers/huawei/rest_client.py @@ -2094,3 +2094,11 @@ class RestClient(object): msg = _('Set pair secondary access error.') self._assert_rest_result(result, msg) + + def is_host_associated_to_hostgroup(self, host_id): + url = "/host/" + host_id + result = self.call(url, None, "GET") + data = result.get('data') + if data is not None: + return data.get('ISADD2HOSTGROUP') == 'true' + return False -- 2.45.2