Skip to content

Commit d0c7abd

Browse files
authored
Hotfix release master (#7594)
* feat: 新增IP选择器后端搜索和分页 #7550 * fix: 修复返回数据格式错误 #7550 * fix: 修复前后端联调BUG #7550 * fix: 修复代码逻辑问题 #7550 * fix: 修复请求为POST请求 #7550 * fix: 修复模糊匹配规则问题 #7550 * fix: 修复数据类型错误 #7550
1 parent c770f05 commit d0c7abd

File tree

3 files changed

+151
-31
lines changed

3 files changed

+151
-31
lines changed

gcloud/tests/utils/cmdb/test_business_host_topo.py

Lines changed: 105 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def setUp(self):
3333
self.ip_list = "ip_list_token"
3434
self.ip_str = "ip_str_token"
3535
self.ip_strs = "ip1_str_token, ip2_str_token"
36+
self.host_id_str = "225"
37+
self.host_id_strs = "225,286"
3638
self.list_biz_hosts_topo_return = [
3739
{
3840
"host": {
@@ -111,7 +113,8 @@ def setUp(self):
111113
},
112114
],
113115
},
114-
]
116+
],
117+
"count": 2,
115118
},
116119
}
117120
self.get_business_host_topo_expect_return = [
@@ -142,12 +145,42 @@ def setUp(self):
142145
],
143146
},
144147
]
148+
self.get_filter_business_host_topo_expect_return = (
149+
[
150+
{
151+
"host": {
152+
"bk_cloud_id": 0,
153+
"bk_host_id": 1,
154+
"bk_host_innerip": "127.0.0.1",
155+
"bk_mac": "",
156+
"bk_os_type": None,
157+
},
158+
"set": [{"bk_set_id": 11, "bk_set_name": "set1"}],
159+
"module": [{"bk_module_id": 56, "bk_module_name": "m1"}],
160+
},
161+
{
162+
"host": {
163+
"bk_cloud_id": 0,
164+
"bk_host_id": 3,
165+
"bk_host_innerip": "127.0.0.3",
166+
"bk_mac": "",
167+
"bk_os_type": None,
168+
},
169+
"set": [{"bk_set_id": 10, "bk_set_name": "空闲机池"}, {"bk_set_id": 11, "bk_set_name": "set1"}],
170+
"module": [
171+
{"bk_module_id": 54, "bk_module_name": "空闲机"},
172+
{"bk_module_id": 55, "bk_module_name": "空闲机1"},
173+
{"bk_module_id": 56, "bk_module_name": "m1"},
174+
],
175+
},
176+
],
177+
2,
178+
)
145179

146180
def tearDown(self):
147181
self.get_client_by_user_patcher.stop()
148182

149183
def test__list_biz_hosts_topo_return_empty(self):
150-
151184
mock_batch_request = MagicMock(return_value=[])
152185
with patch("gcloud.utils.cmdb.batch_request", mock_batch_request):
153186
hosts_topo = get_business_host_topo(self.username, self.bk_biz_id, self.supplier_account, self.host_fields)
@@ -202,15 +235,16 @@ def test__get_contains_with_ip_list(self):
202235
ip_str=self.ip_str,
203236
)
204237

205-
self.assertEqual(hosts_topo, self.get_business_host_topo_expect_return)
238+
self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
206239
self.mock_client.cc.list_biz_hosts_topo.assert_called_once_with(
207240
{
208241
"bk_biz_id": self.bk_biz_id,
209242
"bk_supplier_account": self.supplier_account,
210243
"fields": self.host_fields,
211244
"host_property_filter": {
212245
"condition": "OR",
213-
"rules": [{"field": "bk_host_innerip", "operator": "contains", "value": self.ip_str}],
246+
"rules": [{"field": "bk_host_innerip_v6", "operator": "contains", "value": self.ip_str}]
247+
+ [{"field": "bk_host_innerip", "operator": "contains", "value": self.ip_str}],
214248
},
215249
"page": {"start": 0, "limit": 10},
216250
},
@@ -228,7 +262,7 @@ def test__get_many_contains_with_ip_list(self):
228262
ip_str=self.ip_strs,
229263
)
230264

231-
self.assertEqual(hosts_topo, self.get_business_host_topo_expect_return)
265+
self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
232266
self.mock_client.cc.list_biz_hosts_topo.assert_called_once_with(
233267
{
234268
"bk_biz_id": self.bk_biz_id,
@@ -237,8 +271,12 @@ def test__get_many_contains_with_ip_list(self):
237271
"host_property_filter": {
238272
"condition": "OR",
239273
"rules": [
240-
{"field": "bk_host_innerip", "operator": "contains", "value": self.ip_str}
241-
for self.ip_str in self.ip_strs.split(",")
274+
{"field": "bk_host_innerip_v6", "operator": "contains", "value": ip_str}
275+
for ip_str in self.ip_strs.split(",")
276+
]
277+
+ [
278+
{"field": "bk_host_innerip", "operator": "contains", "value": ip_str}
279+
for ip_str in self.ip_strs.split(",")
242280
],
243281
},
244282
"page": {"start": 0, "limit": 10},
@@ -251,12 +289,70 @@ def test__get_with_page_list(self):
251289
self.username, self.bk_biz_id, self.supplier_account, self.host_fields, start=0, limit=10
252290
)
253291

254-
self.assertEqual(hosts_topo, self.get_business_host_topo_expect_return)
292+
self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
255293
self.mock_client.cc.list_biz_hosts_topo.assert_called_once_with(
256294
{
257295
"bk_biz_id": self.bk_biz_id,
258296
"bk_supplier_account": self.supplier_account,
259297
"fields": self.host_fields,
260298
"page": {"start": 0, "limit": 10},
261-
},
299+
}
300+
)
301+
302+
def test_get_equal_host_list(self):
303+
self.mock_client.cc.list_biz_hosts_topo = MagicMock(return_value=self.list_biz_hosts_page_topo_return)
304+
hosts_topo = get_filter_business_host_topo(
305+
self.username,
306+
self.bk_biz_id,
307+
self.supplier_account,
308+
self.host_fields,
309+
start=0,
310+
limit=10,
311+
host_id_str=self.host_id_str,
312+
)
313+
314+
self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
315+
self.mock_client.cc.list_biz_hosts_topo.assert_called_once_with(
316+
{
317+
"bk_biz_id": self.bk_biz_id,
318+
"bk_supplier_account": self.supplier_account,
319+
"fields": self.host_fields,
320+
"host_property_filter": {
321+
"condition": "OR",
322+
"rules": [{"field": "bk_host_id", "operator": "in", "value": [int(self.host_id_str)]}],
323+
},
324+
"page": {"start": 0, "limit": 10},
325+
}
326+
)
327+
328+
def test_get_many_equal_host_list(self):
329+
self.mock_client.cc.list_biz_hosts_topo = MagicMock(return_value=self.list_biz_hosts_page_topo_return)
330+
hosts_topo = get_filter_business_host_topo(
331+
self.username,
332+
self.bk_biz_id,
333+
self.supplier_account,
334+
self.host_fields,
335+
start=0,
336+
limit=10,
337+
host_id_str=self.host_id_strs,
338+
)
339+
340+
self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
341+
self.mock_client.cc.list_biz_hosts_topo.assert_called_once_with(
342+
{
343+
"bk_biz_id": self.bk_biz_id,
344+
"bk_supplier_account": self.supplier_account,
345+
"fields": self.host_fields,
346+
"host_property_filter": {
347+
"condition": "OR",
348+
"rules": [
349+
{
350+
"field": "bk_host_id",
351+
"operator": "in",
352+
"value": [int(host_id) for host_id in self.host_id_strs.split(",")],
353+
}
354+
],
355+
},
356+
"page": {"start": 0, "limit": 10},
357+
}
262358
)

gcloud/utils/cmdb.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ def get_business_host_topo(username, bk_biz_id, supplier_account, host_fields, i
9393
return host_info_list
9494

9595

96-
def get_filter_business_host_topo(username, bk_biz_id, supplier_account, host_fields, start, limit, ip_str=None):
97-
"""获取业务下所有主机信息
96+
def get_filter_business_host_topo(
97+
username, bk_biz_id, supplier_account, host_fields, start=None, limit=None, ip_str=None, host_id_str=None
98+
):
99+
"""获取业务下所有符合条件的主机信息
98100
:param username: 请求用户名
99101
:type username: str
100102
:param bk_biz_id: 业务 CC ID
@@ -109,6 +111,8 @@ def get_filter_business_host_topo(username, bk_biz_id, supplier_account, host_fi
109111
:type limit: int
110112
:param ip_str: 主机内网 IP
111113
:type ip_str: str
114+
:param host_id_str: 主机 id
115+
:type host_id_str: str
112116
:return: [
113117
{
114118
"host": {
@@ -137,16 +141,29 @@ def get_filter_business_host_topo(username, bk_biz_id, supplier_account, host_fi
137141
"""
138142
client = get_client_by_user(username)
139143
params = {"bk_biz_id": bk_biz_id, "bk_supplier_account": supplier_account, "fields": list(host_fields or [])}
140-
if ip_str:
141-
rules = [{"field": "bk_host_innerip", "operator": "contains", "value": ip} for ip in ip_str.split(",")]
142-
params["host_property_filter"] = {"condition": "OR", "rules": rules}
143144

144-
params["page"] = {"start": start, "limit": limit}
145-
data = client.cc.list_biz_hosts_topo(params)
146-
if not data["result"]:
147-
raise Exception(_("查询主机列表失败, 请确认业务[{}]是否存在!".format(bk_biz_id)))
145+
rules = []
146+
# 根据host_id_str进行精准匹配
147+
if host_id_str:
148+
host_id_list = [int(host_id) for host_id in host_id_str.split(",")]
149+
rules.extend([{"field": "bk_host_id", "operator": "in", "value": host_id_list}])
150+
# 根据ip_str进行模糊匹配
151+
elif ip_str:
152+
rules.extend([{"field": "bk_host_innerip_v6", "operator": "contains", "value": ip} for ip in ip_str.split(",")])
153+
rules.extend([{"field": "bk_host_innerip", "operator": "contains", "value": ip} for ip in ip_str.split(",")])
154+
if rules:
155+
params["host_property_filter"] = {"condition": "OR", "rules": rules}
148156

149-
result = data["data"]["info"]
157+
count = None
158+
if start is not None and limit is not None:
159+
params["page"] = {"start": start, "limit": limit}
160+
data = client.cc.list_biz_hosts_topo(params)
161+
if not data["result"]:
162+
raise Exception(_("查询主机列表失败, 请确认业务[{}]是否存在!".format(bk_biz_id)))
163+
count = data["data"]["count"]
164+
result = data["data"]["info"]
165+
else:
166+
result = batch_request(client.cc.list_biz_hosts_topo, params)
150167

151168
host_info_list = []
152169
for host_topo in result:
@@ -160,7 +177,7 @@ def get_filter_business_host_topo(username, bk_biz_id, supplier_account, host_fi
160177

161178
host_info_list.append(host_info)
162179

163-
return host_info_list
180+
return host_info_list, count
164181

165182

166183
def get_business_host(username, bk_biz_id, supplier_account, host_fields, ip_list=None, bk_cloud_id=None):

pipeline_plugins/cmdb_ip_picker/query.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,19 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
8080
if settings.ENABLE_IPV6 or settings.ENABLE_GSE_V2:
8181
# IPV6环境下或者开启了GSE 2.0 版本
8282
default_host_fields.append("bk_agent_id")
83-
fields = set(default_host_fields + json.loads(request.GET.get("fields", "[]")))
83+
fields = set(default_host_fields + json.loads(request.POST.get("fields", "[]")))
8484
client = get_client_by_user(request.user.username)
8585

8686
topo_modules_id = set()
8787

88-
ip_str = request.GET.get("ip_str", "")
89-
start = request.GET.get("start", None)
90-
limit = request.GET.get("limit", None)
88+
start = request.POST.get("start", None)
89+
limit = request.POST.get("limit", None)
90+
ip_str = request.POST.get("ip_str", "")
91+
host_id_str = request.POST.get("host_id_str", None)
9192

9293
# get filter module id
93-
if request.GET.get("topo", None):
94-
topo = json.loads(request.GET.get("topo"))
94+
if request.POST.get("topo", None):
95+
topo = json.loads(request.POST.get("topo"))
9596
topo_result = get_cmdb_topo_tree(request.user.username, bk_biz_id, bk_supplier_account)
9697
if not topo_result["result"]:
9798
return JsonResponse(topo_result)
@@ -114,11 +115,13 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
114115
return JsonResponse(result)
115116

116117
if start and limit:
117-
raw_host_info_list = cmdb.get_filter_business_host_topo(
118-
request.user.username, bk_biz_id, bk_supplier_account, fields, int(start), int(limit), ip_str
118+
raw_host_info_list, count = cmdb.get_filter_business_host_topo(
119+
request.user.username, bk_biz_id, bk_supplier_account, fields, int(start), int(limit), ip_str, host_id_str
119120
)
120121
else:
121-
raw_host_info_list = cmdb.get_business_host_topo(request.user.username, bk_biz_id, bk_supplier_account, fields)
122+
raw_host_info_list, count = cmdb.get_filter_business_host_topo(
123+
request.user.username, bk_biz_id, bk_supplier_account, fields, ip_str=ip_str, host_id_str=host_id_str
124+
)
122125

123126
# map cloud_area_id to cloud_area
124127
cloud_area_dict = {}
@@ -207,7 +210,7 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
207210
host["agent"] = agent_info.get("bk_agent_alive", -1)
208211

209212
# search host lock status
210-
if request.GET.get("search_host_lock", None):
213+
if request.POST.get("search_host_lock", None):
211214
bk_host_id_list = [host_detail["bk_host_id"] for host_detail in data]
212215
host_lock_status_result = client.cc.search_host_lock({"id_list": bk_host_id_list})
213216
if not host_lock_status_result["result"]:
@@ -219,7 +222,11 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
219222
host_lock_status = host_lock_status_data.get(host_detail["bk_host_id"])
220223
if host_lock_status is not None:
221224
host_detail["bk_host_lock_status"] = host_lock_status
222-
result = {"result": True, "code": NO_ERROR, "data": data}
225+
226+
if count:
227+
result = {"result": True, "code": NO_ERROR, "data": {"data": data, "count": count}}
228+
else:
229+
result = {"result": True, "code": NO_ERROR, "data": data}
223230
return JsonResponse(result)
224231

225232

0 commit comments

Comments
 (0)