Skip to content

Commit

Permalink
fix: 修复代码逻辑问题 #7550
Browse files Browse the repository at this point in the history
  • Loading branch information
guohelu committed Oct 30, 2024
1 parent 365c3c9 commit ad1bcbf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
17 changes: 10 additions & 7 deletions gcloud/tests/utils/cmdb/test_business_host_topo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def setUp(self):
self.ip_list = "ip_list_token"
self.ip_str = "ip_str_token"
self.ip_strs = "ip1_str_token, ip2_str_token"
self.bk_host_id = "225"
self.bk_host_ids = "225,286"
self.host_id = "225"
self.host_ids = "225,286"
self.list_biz_hosts_topo_return = [
{
"host": {
Expand Down Expand Up @@ -303,7 +303,7 @@ def test_get_equal_host_list(self):
self.host_fields,
start="0",
limit="10",
bk_host_id=self.bk_host_id,
host_id=self.host_id,
)

self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
Expand All @@ -314,7 +314,7 @@ def test_get_equal_host_list(self):
"fields": self.host_fields,
"host_property_filter": {
"condition": "OR",
"rules": [{"field": "bk_host_id", "operator": "equal", "value": int(self.bk_host_id)}],
"rules": [{"field": "bk_host_id", "operator": "in", "value": [int(self.host_id)]}],
},
"page": {"start": 0, "limit": 10},
}
Expand All @@ -329,7 +329,7 @@ def test_get_many_equal_host_list(self):
self.host_fields,
start="0",
limit="10",
bk_host_id=self.bk_host_ids,
host_id=self.host_ids,
)

self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
Expand All @@ -341,8 +341,11 @@ def test_get_many_equal_host_list(self):
"host_property_filter": {
"condition": "OR",
"rules": [
{"field": "bk_host_id", "operator": "equal", "value": int(host_id)}
for host_id in self.bk_host_ids.split(",")
{
"field": "bk_host_id",
"operator": "in",
"value": [int(host_id) for host_id in self.host_ids.split(",")],
}
],
},
"page": {"start": 0, "limit": 10},
Expand Down
18 changes: 10 additions & 8 deletions gcloud/utils/cmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def get_business_host_topo(username, bk_biz_id, supplier_account, host_fields, i


def get_filter_business_host_topo(
username, bk_biz_id, supplier_account, host_fields, start=None, limit=None, ip_str=None, bk_host_id=None
username, bk_biz_id, supplier_account, host_fields, start=None, limit=None, ip_str=None, host_id=None
):
"""获取业务下所有主机信息
"""获取业务下所有符合条件的主机信息
:param username: 请求用户名
:type username: str
:param bk_biz_id: 业务 CC ID
Expand All @@ -112,8 +112,8 @@ def get_filter_business_host_topo(
:type limit: str
:param ip_str: 主机内网 IP
:type ip_str: str
:param bk_host_id: 主机 id
:type bk_host_id: str
:param host_id: 主机 id
:type host_id: str
:return: [
{
"host": {
Expand Down Expand Up @@ -144,11 +144,13 @@ def get_filter_business_host_topo(
params = {"bk_biz_id": bk_biz_id, "bk_supplier_account": supplier_account, "fields": list(host_fields or [])}

rules = []
if bk_host_id:
rules.extend(
[{"field": "bk_host_id", "operator": "equal", "value": int(host_id)} for host_id in bk_host_id.split(",")]
)
# 根据host_id进行精准匹配
if host_id:
host_id_list = [int(id) for id in host_id.split(",")]
rules.extend([{"field": "bk_host_id", "operator": "in", "value": host_id_list}])
# 根据ip_str进行模糊匹配
elif ip_str:
# 如果搜索IP是ipv4地址,就匹配bk_host_innerip字段,如果是ipv6地址就匹配bk_host_innerip_v6字段
rules.extend(
[
{
Expand Down
10 changes: 5 additions & 5 deletions pipeline_plugins/cmdb_ip_picker/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=

topo_modules_id = set()

ip_str = request.GET.get("ip_str", "")
start = request.GET.get("start", None)
limit = request.GET.get("limit", None)
bk_host_id = request.GET.get("bk_host_id", None)
start = request.POST.get("start", None)
limit = request.POST.get("limit", None)
ip_str = request.POST.get("ip_str", "")
host_id = request.POST.get("host_id", None)

# get filter module id
if request.GET.get("topo", None):
Expand All @@ -115,7 +115,7 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
return JsonResponse(result)

raw_host_info_list, count = cmdb.get_filter_business_host_topo(
request.user.username, bk_biz_id, bk_supplier_account, fields, start, limit, ip_str, bk_host_id
request.user.username, bk_biz_id, bk_supplier_account, fields, start, limit, ip_str, host_id
)

# map cloud_area_id to cloud_area
Expand Down

0 comments on commit ad1bcbf

Please sign in to comment.