diff --git a/gcloud/tests/utils/cmdb/test_business_host_topo.py b/gcloud/tests/utils/cmdb/test_business_host_topo.py index 6fde2641d..a00354649 100644 --- a/gcloud/tests/utils/cmdb/test_business_host_topo.py +++ b/gcloud/tests/utils/cmdb/test_business_host_topo.py @@ -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": { @@ -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) @@ -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}, } @@ -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) @@ -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}, diff --git a/gcloud/utils/cmdb.py b/gcloud/utils/cmdb.py index 4e4a30ec9..2a01e1923 100644 --- a/gcloud/utils/cmdb.py +++ b/gcloud/utils/cmdb.py @@ -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 @@ -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": { @@ -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( [ { diff --git a/pipeline_plugins/cmdb_ip_picker/query.py b/pipeline_plugins/cmdb_ip_picker/query.py index e893512bb..e2d552bb5 100644 --- a/pipeline_plugins/cmdb_ip_picker/query.py +++ b/pipeline_plugins/cmdb_ip_picker/query.py @@ -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): @@ -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