From b2816f3b4ab605ed3c40dcacabd183f7cbe8b12d Mon Sep 17 00:00:00 2001 From: Wilson Liu Date: Tue, 16 Feb 2016 11:24:23 +0800 Subject: [PATCH] Match the ip more accurately in Huawei driver Currently we didn't match the ip in the iqn accurately, now we will fix this in this patch. Closes-Bug: #1545914 Change-Id: I32232fcc6cad733b0dd8fa8c323297f6fbac3137 --- cinder/tests/unit/test_huawei_drivers.py | 39 +++++++++++++++++++++ cinder/volume/drivers/huawei/rest_client.py | 10 ++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/cinder/tests/unit/test_huawei_drivers.py b/cinder/tests/unit/test_huawei_drivers.py index 6ea717e6b..961a9eea2 100644 --- a/cinder/tests/unit/test_huawei_drivers.py +++ b/cinder/tests/unit/test_huawei_drivers.py @@ -3040,6 +3040,45 @@ class HuaweiISCSIDriverTestCase(test.TestCase): portg = self.driver.client.get_tgt_port_group('test_portg') self.assertIsNone(portg) + def test_get_tgt_iqn_from_rest_match(self): + match_res = { + 'data': [{ + 'TYPE': 249, + 'ID': '0+iqn.2006-08.com: 210048cee9d: 111.111.111.19,t,0x01' + }, { + 'TYPE': 249, + 'ID': '0+iqn.2006-08.com: 210048cee9d: 111.111.111.191,t,0x01' + }], + 'error': { + 'code': 0 + } + } + ip = '111.111.111.19' + expected_iqn = 'iqn.2006-08.com: 210048cee9d: 111.111.111.19' + self.mock_object(rest_client.RestClient, 'call', + mock.Mock(return_value=match_res)) + iqn = self.driver.client._get_tgt_iqn_from_rest(ip) + self.assertEqual(expected_iqn, iqn) + + def test_get_tgt_iqn_from_rest_mismatch(self): + match_res = { + 'data': [{ + 'TYPE': 249, + 'ID': '0+iqn.2006-08.com: 210048cee9d: 111.111.111.191,t,0x01' + }, { + 'TYPE': 249, + 'ID': '0+iqn.2006-08.com: 210048cee9d: 111.111.111.192,t,0x01' + }], + 'error': { + 'code': 0 + } + } + ip = '111.111.111.19' + self.mock_object(rest_client.RestClient, 'call', + mock.Mock(return_value=match_res)) + iqn = self.driver.client._get_tgt_iqn_from_rest(ip) + self.assertIsNone(iqn) + class FCSanLookupService(object): diff --git a/cinder/volume/drivers/huawei/rest_client.py b/cinder/volume/drivers/huawei/rest_client.py index 90e85eae9..1a3e005d8 100644 --- a/cinder/volume/drivers/huawei/rest_client.py +++ b/cinder/volume/drivers/huawei/rest_client.py @@ -15,6 +15,7 @@ import ast import json +import re import six import socket import time @@ -1244,11 +1245,14 @@ class RestClient(object): if result['error']['code'] != 0: LOG.warning(_LW("Can't find target iqn from rest.")) return target_iqn - + ip_pattern = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') if 'data' in result: for item in result['data']: - if target_ip in item['ID']: - target_iqn = item['ID'] + ips = re.findall(ip_pattern, item['ID']) + for ip in ips: + if target_ip == ip: + target_iqn = item['ID'] + break if not target_iqn: LOG.warning(_LW("Can't find target iqn from rest.")) -- 2.45.2